Fixed Values, Literals

This page was translated by a robot.

A fixed value is an unchangeable value written in the source code, i.e. a value that does not come from a macro, variable or other input structure. In many references, fixed values ​​are denoted by the term literals . The C and C++ languages ​​allow the following values ​​to be written directly into the program code:

Boolean: A boolean value is the same as a logical value. There are basically only two options. Examples: true, false

Integer: An integer value is colloquially the same as an integer. It can be positive or negative and can be represented in C as a decimal, hexadecimal or octal number. Examples: 0, 1, 42, 1000000000, 0xffff, 0755

Floating point: A floating point number is a number with a decimal point (colloquially: with a comma). It can also be written in exponential notation, the zero in front of the decimal point can be omitted. Examples: 3.14159, 12345.67890, .707107, 1e-5, 6.022e23

Character: A character (letter) is an ASCII character , which is usually encoded as a 1-byte value. Examples: 'A', 'x', '\n', '\0', '\x65'

String: A string is interpreted as a sequence of ASCII characters with an automatically appended '\0'. Examples: "Hello World", ""

Pointer: A pointer is a pointer to an address in memory. With the exception of the null pointer, these values ​​should not be written down explicitly, only using the pointer arithmetic built into the language.

Details

While variables store values ​​in the processor's memory or register, fixed values ​​are built directly into the program code and are usually written directly to the appropriate machine instructions by the compiler and never change their place. Since these values ​​are in the program code itself, they cannot (usually) be changed.

The term literal is very common in English and in compiler construction in general, but has not really gained a foothold in the German programming language. Much more often, literals are simply referred to casually as values . The author has decided to almost completely dispense with the term literal and instead to use the phrase fixed values ​​written in the source code.

Boolean Values

A boolean value is also called a truth value and can only have two possible states: trueand false. In theory, one bit is enough to store these two states, but in C and C++ they are implemented as integer values: In C, the value 0is falseinterpreted as and all other values ​​as true. It was only in C++ that the type booland the keywords were specified falseusing the 1-byte integer 0and trueas a 1-byte integer not equal 0to (usually 1).

Integer Values

Integer values ​​are whole numbers. If an integer value is written into the code as a fixed value, it is always positive. However, any integer value can be negated using the negative operator . -An integer value can also be explicitly marked as positive using the positive operator .+

 1234
-1234
+1234

The number itself can be written as a decimal number (with the digits 0 to 9), as a hexadecimal number (digits 0to 9and letters ato for Ato F) with a leading 0xor 0Xas an octal number (digits 0to 7) with a leading 0.

9999
0xffff
0755

The compiler determines the number of bytes required based on the expected type at the position of the value

0xff;
0x00ff;
0x000000ff;
unsigned char  a = 0xff;
unsigned short b = 0xff;
unsigned int   c = 0xff;

On the author's system it is also possible to hspecify hexadecimal numbers with a trailing (only lower case), whereby the first digit must be a number from 0 to 9. unsignedTo mark an integer as a value , it can be appended with a uor . UThere is no corresponding suffix for signedvalues. The suffixes ior Iand jor Jare used for complex numbers when the appropriate standard library is included.

0f352h
255u
4i

A few compilers also allow the specification of binary values ​​with a preceding 0bor 0Btrailing small b. Examples would be 0b01001011or 11001100b. However, this is not possible on the author's system. A corresponding counterpart for octal numbers is not known to the author.

To indicate a large number, it is also possible to append a L( long) or LL( ) to the value. long longThe lowercase counterparts land llmean the same thing, but on this page the uppercase letters are preferred for better readability. This type specification forces the compiler to use the specified number of bytes for the value. However, the specification Lis rarely found anymore, since nowadays a intvalue always comprises 4 bytes ( intand longare the same in ISO-C++). However, the addition Lmay be necessary in programs compiled using older C dialects. However, the statement LLis still used today.

1234L
0xffLL

In principle, the suffixes can appear in any order and mixed up. On the author's system, however, the suffix must be at the end h, but it can (unnecessarily) appear any number of times.

4f321LLuihhhhhhh

Floating Point Values

Floating point values ​​are numbers with a decimal point (colloquially: with a comma). In the High German language area, the term floating point is more common. They are sometimes also called real numbers because they try to cover the set of real numbers, but they are encoded as floating point numbers according to IEEE 754 or IEEE 854 on all common systems, which is why this term is used on this page. In English they are called floating point numbers. They are written in the decimal system, with the dot.serves as a decimal point. If there is a zero value before the decimal point, this value can be omitted. Likewise, a zero value after the decimal point can be omitted. Omission of both sides at the same time is not allowed. The decimal point must be written in any case, otherwise the value will be interpreted as an integer value.

0.1234 == .1234
9876.0 == 9876.

A floating point value can also be written using exponential representation consisting of a positive or negative mantissa with an optional decimal point and a positive or negative integer exponent, with the eor Eserving as a separator. Note: In contrast to other languages, the separator dor Dis not allowed in C. If the mantissa is an integer, the decimal point can be omitted.

1.234e56
1e9

Three floating point types specified in C: float, doubleand long double. A floating point value as described above has the standard type double. The for suffix forces Fa floatvalue, the lor suffix forces La long doublevalue. The suffixes ior Iand jor Jare allowed just like with the integer values ​​and are also used with floating point values ​​for complex numbers if the corresponding standard library is linked.

3.1415f
3.1415
3.1415l
1.i

There is also the option of writing floating point numbers as hexadecimal values. The syntax is 0xM.EpPwhere represents Ma positive or negative integer hexadecimal mantissa, Ea positive integer hexadecimal exponent, and Pa multiplication of the number by a positive or negative decimal exponent of two. The actual value of the floating point number is calculated by (M + E/16^(hexdigits(E))) * 2^P. Thus, for example 0x12.34p5, the calculation (0x12 + 0x34/256) * 2^5represents the number 582.5. The use of hexadecimal floating point numbers is no longer common today.

Characters

A character in C is basically a 1-byte integer represented as an ASCII character . Such a character is always written in single quotation marks ' '. Some dialects of C also understand multi-characters, where the single quotes can contain 2 to 8 characters, accordingly the character strings then denote a 2-byte, 4-byte or 8-byte number.

char  x = 'A';
short y = 'AA';

Some characters can or must be escaped using the backslash character \as an escape character. C specifies the following escape characters, with the most important ones highlighted:

null             0x00
alert            0x07
backspace        0x08
tab              0x09
linefeed         0x0a
vertical tab     0x0b
form feed        0x0c
carriage return  0x0d
escape           0x1b
"                0x22
'                0x27
?                0x3f
\                0x5c
octal
hexadecimal
\0
\a
\b
\t
\n
\v
\f
\r
\e (not all Compilers)
\"
\'
\?
\\
\nnn (example: \101 = 'A')
\xnn (example: \x51 = 'A')

The question mark is specified as an escape character because certain characters in the C language can be circumscribed using so-called trigraphs . Although these are still built into the compilers today, they are no longer deliberately used. More details can be found on the Characters used in C page.

Strings

A string is understood in C as a sequence of ASCII characters . These characters are " "written in double quotes, and the escape characters mentioned above are also valid. The compiler automatically appends a null byte to the end of a string \0, which means that the string's actual storage space consumption is 1 byte larger than the number of characters it contains.





Hello World
#include <stdio.h>

int main(){
  char* str = "Hello World";
  printf(str);
  return 0;
}

This way of storing strings with the null byte appended is peculiar to C and is often referred to as a null-terminated string, or sometimes a C-like string. The standard library stringprovides many functions to work with these strings.

It should be noted that strings written in the program code represent a special case of the language syntax, which is why they have their own name string literals . String literals store their content in an array that is normally accessible to the programmer, but must not be modified. In C++, string literals are explicitly constmarked as and the compiler warns against misuse.

After being translated by the compiler, a literal string is in the binary as a data block. When the program is started, this data is implicitly copied byte for byte by the runtime system when initializing string arrays. This means that in this case the string will end up existing twice in memory. Only if a string is addressed literally using a pointer is it not copied.

Pointer

Basically, there is only one single address that should be used as a fixed value: the null pointer. Nevertheless: pointers are addresses of the memory and according to common opinion are understood as integers, numbered byte by byte from 0 to the last address. It is thus possible to assign any integer to a pointer. In the past, this caused some computers to crash, but today the operating systems have protective devices to protect the memory from unwanted address assignments. However, the languages ​​C and C++ offer to assign addresses directly and in particular to manipulate them using pointer arithmetic ..

Next Chapter: Whitespaces