What are the character constants on C.

3.2. Constants

Integer constants

A integrity-Constant is an integer that can be positive, negative, or zero. Three spellings for integrity-Constants are allowed: decimal, octal and hexadecimal.

A decimal integrity-Constant is a combination of digits of 0 to 9, where the first cannot be zero. The values

are valid, decimal integrityConstants.

Octals integrityConstants consist of octal digits, i.e. the digits of 0 to 7. The first character of an octal integrityConstants must be a zero. Examples of valid octals integrity-Constants:

One hexadecimal integrity-Constant is a combination of hexadecimal digits (0 to 9 and a to f), with 0x or. 0X must begin. The letters a to f stand for the decimal values 10 to 15.

are examples of hexadecimal integrityConstants.

Octal and hexadecimal notations are relatively rare and are most likely to be used in system programming.

In general, the compiler assigns the smallest possible data type to a numeric, integer constant. But as soon as their value exceeds the range of the data type integrity can be included, the compiler automatically makes a modification to the type long int (for modification of the elementary data types see Section 3.3.4.). The programmer is free to define the data type by adding suffixes L., U or UL intervene and force a certain data type. Integer-Constants can be changed to long, unsigned or unsigned long in this way.

Examples:

Floating point constants

Numbers with decimal places can be represented as floating point constants. There are two different spellings; the first and simplest is a sequence of decimal digits that contain a decimal point (not a comma!).

Examples:

The decimal point does not necessarily have to be within the number, but can also be at the beginning or at the end. The second floating point number could also be used as a 13. be written, the third as.0. For the sake of legibility, however, the first variant is preferable. However, it is important to note that the decimal point must be written in the second and third examples, otherwise the compiler will determine the type of this constant integrity assigns.

The second possible representation of floating point numbers is in the exponential notation. This form combines an integer or a floating point number with a power of 10. In one of many possible variants, the four example numbers from above in the exponential notation would look like this:

While the C compiler assigns the smallest possible data type to integer constants, it always assumes that floating point numbers are of the double type. As with whole numbers, C allows floating point numbers to influence the assignment of the data type by adding a suffix. The ending F ‘ or. f informs the compiler that the floating point number at hand is of type float. The letter appended to a floating point number l (also in upper or lower case) indicates that the data type is long double.

Character constants

A character constant is a single character written within single quotation marks (apostrophes). Character constants do not have the data type char, as one might expect, but rather int. We could do that with the help of an adapted version of our sizeof.c-Program without further ado:

Examples of character constants are

Character constants are stored internally as an integer value; it is simply the value assigned to the character within the character set. Our example '2' is represented internally by 50 when using the ASCII character set, because this is the ASCII value of '2'. Instead of '2', you could also work with the numerical value 50: '2' is better, however, because it does not depend on the character set used and thus increases the portability of the program.

In order to display characters in a program that would be difficult or impossible to enter, there are so-called escape sequences (strictly speaking, they are backslash sequences because they are characterized by a leading backslash). For example, it would not be possible to use the printf () function to output a line feed if there was not a corresponding escape sequence: Inserting the ASCII character with the value 10 (by pressing the (␍) key) creates a line feed in the source code and then an error message from the compiler, instead of ensuring that the cursor jumps to the next line during program execution.

The following escape sequences are defined:

characterimportance
\aAlarm bell
\nLine feed (under DOS causes \n a line feed combined with a carriage return.)
\ r Carriage return
\t horizontal tab
\v vertical tab
\b Backspace (backspace)
\f Form feed
\ ' single quotation mark
\" double quotation mark
\\ Backslash
\? Question mark

Escape sequences are not only suitable for displaying special characters: in principle, it is possible to enter any character in this form. The leading backslash must be followed by the octal or hexadecimal value of the character. When using the ASCII character set, 'X' can either be written in octal as '\ 130' or in hexadecimal as '\ x58'.

String constants

A character string constant is a sequence of any characters enclosed in double quotation marks. For example, in our "Hello world" program, the argument to printf (), "Hello world \ n", was a string constant. Such a string constant (from the English "string" for "character string") may also be empty: It is then simply represented by 2 consecutive quotation marks (""). The quotation marks are not part of the character string, they only limit it.

The same escape sequences can be used in character strings that were already allowed for the representation of character constants. The final \n in "Hello world \ n"As expected," causes printf () to move the cursor to the beginning of the next line after it prints "Hello world".

The backslash at the end of the line is used to distribute long strings over several lines. Please note that the line feed must take place immediately after the backslash.

Two neighboring character string constants are combined into a character string when compiled:

In retrospect, when comparing character constants and string constants, the question arises as to why one could not display all individual characters as a character string and do without character constants. This is countered by the fact that in C character strings are generally terminated internally with a binary zero ('\ 0') - that is, the character with the ASCII value 0. That is why "X" and 'X'not the same: the former is a zero-terminated string constant (consisting of'X ' and '\0’), the second a character constant with the value 'X'.

Definition of constants with the help of the preprocessor and the keyword const

Constants can be created using the preprocessor instruction #define to be agreed:

causes the preprocessor to process all occurrences of TRUE to be exchanged for 1.

Finally, with the help of the const modifier, the value of a variable can be defined as immutable and thus a constant.

Both variants are discussed in more detail in the sections about the preprocessor (Chapter 12) and about modifying the elementary data types (Chapter 3.3.).