Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

Constants C programming Class 8

Constants  C programming


Constants can have di erent types and representations. This section presents various constant types by example. First, an integer constant 1234 is of type int. An constant of type long int is su xed by an L, 1234L; (integer constants too big for int are implicitly taken as long). An unsigned int is su xed by a U, 1234U, and UL specifies unsigned long.

Integer constants may also be specified by octal (base 8) or hexadecimal (base 16) values, rather than decimal (base 10). Octal numbers are preceded by a 0 and hex by 0x. Thus, 1234 in decimal is equivalent to 02322 and 0x4D2. It is important to remember that these three constants represent exactly the same value (0101 1101 0010 in binary). For example, the following code

int x = 1234, y = 02322, z = 0x4D2;

printf("%d\t%o\t%x\n", x, x, x);

printf("%d\t%d\t%d\n", x, y, z);

2 To be strictly correct, only const and volatile are actually type qualifiers. We call short, long, signed, and unsigned “qualifiers” here because they behave like qualifiers—they alter the characteristics of plain types. However, they are actually type specifiers; (the basic types int, double, char, etc are also type specifiers).

prints

1234 2322 4d2

1234 1234 1234

Notice that C does not provide a direct binary representation.However, hex form is practically very useful because it breaks binary in blocks of four bits (see Section 12.1).

Floating-point constants are specified by a decimal point after a number. For example, 1. and 1.3 are of type double, 3.14f and 2.f are of type float, and 7.L is of type long double. Floating-point numbers can also be written using scientific notation, such as 1.65e-2 (which is equivalent to 0.0165). Constant expressions, such as 3+7+9.2, are evaluated at compile-time and replaced by a single constant value, 19.2. Thus, constant expressions incur no runtime overhead.

Character constants, such as ’a’, ’\n’, ’7’, are specified by single quotes. Character constants are noteworthy because they are, in fact, not of type char, but of int. Thus, sizeof(’Z’) will equal 4 on a 32-bit machine, not one. Most platforms represent characters using the ASCII character set, which associates the integers 0 to 127 with specific characters (e.g., the character ’T’ is represented by the integer 84). Tables of the ASCII character set are readily found (see, for example, [HS95, page 421]).

There are some characters that can not be displayed directly, but are marked by an "escape sequence"”. It is important to recognise that these escape characters still represent single characters. A selection of key escape characters are the following: \0 for NUL (used to terminate character strings), \n for newline, \t for tab, \v for vertical tab, \\ for backslash, \’ for single quotes, \" for double quotes, and \b for backspace.

String constants, such as "This is a string" are delimited by quotes (note, the quotes are not actually part of the string constant). They are implicitly appended with a terminating ’\0’ character. Thus, in memory, the above string constant would comprise the following character sequence: This is a string\0.


Note. It is important to di erentiate between a character constant (e.g., ’X’) and a NUL terminated string constant (e.g., "X"). The latter is the concatenation of two characters X\0. Also keep in mind that sizeof ('X') four (on 32-bit machine) while sizeof ("X") is two.

Share:

No comments:

Post a Comment

Follow On YouTube