• Non ci sono risultati.

TYPE OF CONSTANTS

N/A
N/A
Protected

Academic year: 2021

Condividi "TYPE OF CONSTANTS"

Copied!
13
0
0

Testo completo

(1)

PROGRAMMAZIONE I

A.A. 2017/2018

(2)

LITERALS

(3)

LITERALS

A literal is a token that denotes a fixed value, which may be an integer, a floating-point number, a character, or a string.

A literal’s type is determined by its value and its notation.

(4)

INTEGER CONSTANTS

An integer constant can be expressed as an ordinary decimal numeral, or as a numeral in octal or

hexadecimal notation.

You must specify the intended notation by a prefix.

übase-8 number system?

ü0, 1, 2, 3, 4, 5, 7 -> 8n x+8n-1 + … + 81 + 80

A decimal constant begins with a nonzero digit

A number that begins with a leading zero is interpreted as an octal constant. 047 = ?

ü39 = (4 x 81 + 7 x 80)

A hexadecimal constant begins with the prefix 0x or 0X.

Hexadecimal digits A to F can be upper or lowercase.

ü0xff, 0Xff, 0xFF, and 0XFF = 15 x 161 + 15 x 160 = 255

(5)

TYPE OF CONSTANTS

The type of a constant is determined at the same time as its value is defined.

Integer constants such as the examples just mentioned usually have the type int. If larger:

üThe compiler assigns it the first type in a hierarchy that is large enough to represent the value.

For example, if an int is 4 byte, the decimal constant

50000 has the type long, since the greatest possible int

value is 32,767, or 215– 1.

You can also influence the types of constants in your programs explicitly by using suffixes.

ü512U (unsigned int), 0Xf0fUL (unsigned long), 0777ll (long long), 123uLL (unsigned long long)

(6)

EXAMPLES

int a= 512U;

int b= 1LL;

int c= 7UL;

Uppercase or lowercase is the same, e.g., ll or LL

(7)

FLOATING-POINT CONSTANTS

Floating-point constants can be written either in decimal or in hexadecimal notation.

A floating-point constant consists of a sequence of decimal digits containing a decimal point.

You may also multiply the value by a power of 10, as in scientific notation: the power of 10 is represented simply by an exponent, introduced by the letter e or E.

ü2.34E5 (2.34 x 105), 67e-12 (67.0 x 10-12)

The decimal point can also be the first or last character.

Thus 10. and .234E6 are permissible numerals.

üHowever, the numeral 10 with no decimal point would be an integer constant, not a floating-point constant.

(8)

FORCING THE TYPE OF FLOATS

The default type of a floating-point constant is double.

You can also append the suffix F or f to assign a constant the type float, or the suffix L or l to give a constant the type long double

(9)

HEXADECIMAL FLOATING-POINT CONSTANTS

A hexadecimal floating-point constant consists of the

prefix 0x or 0X, a sequence of hexadecimal digits with an optional (hexa)decimal point, and an exponent to base two. The exponent is a decimal numeral introduced by the letter p or P.

ü0xa.fP-10 is equal to the number (10 + 15 x 16-1) × 2–10 ünot 2–16

Hexadecimal floating-point constants have the default type double

(10)

CHARACTER CONSTANTS

A character constant consists of one or more characters enclosed in single quotation marks.

ü'a’ 'XY’ '0’ '*'

To represent ’, \ and newline

ü'\'' '\\' '\n'

Character constants have the type int, unless they are explicitly defined as wide characters, with type wchar_t, by the prefix L.

If a character constant contains one character that can be represented in a single byte.

The value of character constants that consist of more than one character can vary from one compiler to

another.

(11)

ESCAPE SEQUENCES

(12)

ESCAPE SEQUENCES

In octal, if ‘A’ = 65, printf(“\’A\’ in octal is %c”, )‘\101’

‘A’ in octal is A

(13)

STRING LITERALS

A string literal consists of a sequence of characters

(and/or escape sequences) enclosed in double quotation marks.

ü"Hello world!\n"

Like character constants, string literals may contain all the characters in the source character set.

A string literal is a static array of char that contains

character codes followed by a string terminator, the null character \0.

The empty string "" occupies exactly one byte in memory, which holds the terminating null character.

char helloWorld[128] = ”Hello World\!n";

printf(“Print the string: %s\n", helloWorld);

Riferimenti

Documenti correlati

This is an open access article under the CC BY-NC-ND license (http:// creativecommons.org/licenses/by-nc-nd/4.0/)... a) Ultrasound image of the femoral area showing a mass with

For the CS/RGM combination, increasing nitrogen supply strongly increased the amino acid concentration both in the skin and in the pulp in 2013 and 2014, although this effect was

PhytomicsQC and Herbalome. The sensitivity and versatility of LC- MS led to the development of the PhytomicsQC concept, which combines chemical analysis, bioresponse

Andamento del campo elettrico e del potenziale elettrico all’interno ed all’esterno di una distribuzione sferica di carica: calcolo della capacità di un condensatore

In the last few years, deep learning has been tremendously successful in many important applications of machine learning. However, our theoretical understanding of deep learning,

A)11 thin films deposition and characterization: The first part describes the deposition of FST thin films with the PLD. Starting from the optimized parameters for the

We propose a methodology where an evolutionary algorithm evolves the parameters of different malicious agents, determining their types and mobility patterns in order to mini- mize

S6 UTSA-16 structural stability upon cycling.... The bomb was then placed in a furnace that was heated from room temperature to 393 K in half an hour, kept at 393 K for 2 days,