• A2.5.2. Character constants. String constants

    I/O

    Output functions:

    %d is a signed integer

    %u - unsigned integer

    %f-real number

    %s-character

    Input functions:

    24 C language data types.

    long int counter;

    Arithmetic constants.

    or in hexadecimal:

    \a bell signal

    \\ backslash

    \b return to step (downhole)

    \f page feed

    \n new line

    \r carriage return

    \? question mark

    \" single quote

    \" double quote

    \ooo octal code

    \xhh hexadecimal code

    Be careful to remember that a character constant and a string containing a single character are not the same thing: "x" is not the same as "x". The entry "x" denotes an integer value equal to the code of the letter x from the standard character set, and the entry "x" is a character array that contains one character (the letter x) and "\0".

    Variables and their descriptions.

    In C, any variable must be declared before it is used; Typically, all variables are declared at the beginning of the function, before the first instruction to be executed. The declaration describes the properties of the variables. It consists of a type name and a list of variables, for example:

    int fahr, celsius;

    int lower, upper, step;

    In its declaration, a variable can be initialized, like this:

    char esc = "\\" ;

    Any variable in a declaration can be given a const qualifier to indicate that its value will not change further.

    const double e = 2.71828182845905;

    const char msg = "warning: ";

    When applied to an array, the const qualifier indicates that none of its elements will change. The const hint can also be applied to an array argument to indicate that the function does not modify the array:

    int strlen(const char);

    The response to an attempt to modify a variable marked with the const qualifier depends on the compiler implementation.

    Unary operations in C.

    The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1. The unusual thing about the ++ and -- operators is that they can be used both as prefix (by placing in front of a variable: ++n) and as postfix (by placing after the variable: n++) operators. In both cases, the value of n is incremented by 1, but the expression ++n increments n before its value is used, and n++ after.

    Bitwise operations in C.

    C has six operators for manipulating bits. They can only be applied to integer operands, i.e. to operands char types, short, int and long, signed and unsigned.

    & - bitwise AND.

    | - bitwise OR.

    ^ - bitwise exclusive OR.<< - сдвиг влево.

    >> - shift to the right.

    ~ - bitwise negation (unary).

    The & operator (bitwise AND) is often used to reset a group of bits to zero. For example,

    n = n & 0177 resets all bits in n except the lowest seven.

    Operator | (bitwise OR) is used to set bits; so, x = x! SET_ON sets units in those bits of x that correspond to units in SET_ON.

    The bitwise operators & and | should be distinguished from logical operators&& and ||, which when evaluated from left to right produce a truth value. For example, if x is 1 and y is 2, then x & y will give a zero, and x && y will give a one.

    The ^ (bitwise exclusive OR) operator will set each bit to 1 if the corresponding bits of the operands have different values, and 0 when they are the same.

    Operators<< и >> shift left or right its left operand by the number of bit positions specified by the right operand, which must be non-negative. Yes, x<< 2 сдвигает значение х влево на 2 позиции, заполняя освобождающиеся биты нулями, что эквивалентно умножению х на 4. Сдвиг вправо беззнаковой величины всегда сопровождается заполнением освобождающихся разрядов нулями. Сдвиг вправо знаковой величины на одних машинах происходит с распространением знака ("арифметический сдвиг"), на других - с заполнением освобождающихся разрядов нулями ("логический сдвиг").

    The unary operator ~ “inverts” an integer bitwise, that is, it turns each one bit into a zero bit and vice versa. For example, x = x & ~077 resets the last 6 digits in x.

    Conditional statements in C.

    Conditional if statement

    Syntax:

    a) shortened form

    if (<выр>) <оператор1>;

    b) full form

    if (<выр>) <оператор1>;

    else<оператор2>;

    Expression<выр>can be arithmetic, logical or relational. If the value<выр>is not equal to zero, then it is executed<оператор1>.

    In full form if<выр>equals zero, then<оператор2>.

    Conditional triple operation (? :)

    Syntax:

    <выр1> ? <выр2> : <выр3>;

    The value is calculated first<выр1>.

    If it is not zero (true), then the value is calculated<выр2>. Meaning<выр3>not calculated.

    If the value<выр1>is zero (false), then the value is calculated<выр3>. Calculating the value<выр2>is not produced.

    Sign = x<0 ? -1: 1;

    35. Switch operator.

    Switches

    Syntax:

    switch(<выр>)

    case<константа_1> : <операторы_1>;

    case<константа_2> : <операторы_2>;

    case<константа_L> : <операторы_L>;

    default:<операторы>;

    When entering a switch, the value is calculated<выр>. If it matches one of the constants, then the statements specified in the corresponding case branch are executed. For example, if<выр>==<константа_L>, then they are fulfilled<операторы_L>.

    If the value<выр>does not match any of the constants, then the operators specified after default are executed.

    If among the operators of the executing branch there is no transition operator (break, goto, return, exit()), then the operators of the next branch are executed.

    If a break operator is encountered among the operators of the executing branch, then control is transferred to the operator following the switch. If a goto statement is encountered, control is transferred to the specified label.

    The default branch can be located anywhere in the group of switch branches.

    switch (operand) (

    case MULTIPLY: x *= y; break;

    case DIVIDE: x /= y; break;

    case ADD: x += y; break;

    case SUBTRACT: x -= y; break;

    case INCREMENT2: x++;

    case INCREMENT1: x++; break;

    case MOD: printf("Not done\n"); break;

    default: printf("Bug!\n");

    If operand == MULTIPLY, then x *= y will be executed; and switch processing (and program execution) will end.

    If operand == INCREMENT2, then x++ will be executed; x++; and the switch processing will complete.

    If operand is EXPONENT, ROOT or MOD, then printf("Not done\n"); break;

    36. Loop operators in C.

    Parametric For Loop

    Syntax:

    for ([<выр1>]; [<выр2>]; [<выр3>]) <оператор>;

    <оператор>is repeated until<выр2>will not accept the value 0 (“false”).

    BEFORE the FIRST iteration of the loop, the value is calculated<выр1>. Typically this is used to initialize a loop counter. Then the value is calculated<выр2>.

    AFTER EACH iteration the value is calculated<выр3>. This is usually used to increment the loop counter. Then the value is calculated<выр2>.

    <выр1>And<выр3>can consist of several expressions separated by commas.

    All loop header parameters are optional, but both ';' delimiters must be present. That is, the forms are acceptable

    For(<выр1>;;) <оператор>;

    For(;<выр2>;) <оператор>;

    For(;;<выр3>) <оператор>;

    For(<выр1>;;<выр3>) <оператор>;

    If missing<выр2>, then its value is considered equal to 1 (“true”).

    Loop with precondition while

    Syntax:

    while (<выр>) <оператор>;

    <оператор> <выр> <выр>is calculated BEFORE each iteration.

    Loop with do ... while postcondition

    Syntax:

    do<оператор>while (<выр>);

    <оператор>re-executed until the value<выр>remains non-zero. Meaning<выр>is calculated AFTER each iteration.

    Design of functions.

    39. Structure of a C program. Varieties of variables.

    void main() /* main program */

    int x,y,z; /* declaration of variables of integer type */

    x=5; y=6; /* assignment operators */

    printf("sum=%d\n",z); /* standard function for outputting from the library. */

    Variables are needed where it is necessary to store any information in the computer's memory, for example, intermediate results of calculations, etc. Almost no program can do without them. A variable is an area of ​​RAM with an assigned name through which this area can be accessed. The name can be up to 247 characters in length and must always begin with a letter of the Latin alphabet or an underscore, which may be followed by other letters, numbers and underscores. Character case matters.

    Variables can store different data, and their type depends on this. There are quite a few different types of variables in C++, they are divided into integer, fractional, logical and character. There is also a separate type for strings, but that is a separate discussion.

    Integer types include types such as int, short (short int), and long. Variables of type int occupy 32 bits in memory. Short variables are twice as “shorter” as the name suggests. They occupy 16 bits of memory. Variables of type long, respectively, are twice as long and occupy 64 bits in memory.

    In addition to the listed integer types, there are their unsigned varieties. As you may have noticed, integer variables can take on both positive and negative values. Unsigned integer variables can only take positive or zero values. To make a variable unsigned, you must precede the type with the keyword unsigned, separated by a space.

    Fractional types include float and double - single and double precision fractional numbers, respectively. Variables of the first type occupy 32 bits in memory, the second type - 64.

    There is only one logical type, and it is called bool. As the name suggests, this type is designed to store the logical values ​​"true" (true) or "false" (false).

    The character type is also the only one. It is called char and can store one character, more precisely, its number in ASCII encoding, in this respect this type is close to integer types. The range of values ​​of the char type is integers from -128 to 127 (from 0 to 255 for unsigned char), that is, all characters of the Latin and national alphabets, numbers, punctuation marks and special characters such as line break, etc. It should be remembered that single characters in C++ are enclosed in apostrophes.

    Strings as a separate type appeared only in C++; in regular C, strings were literally arrays of characters, and working with them was in many ways similar to working with arrays.

    Address arithmetic.

    may be different:

    result of type int*

    Pointers and functions.

    Pointers and dynamic memory allocation were briefly introduced in Section 2.2.

    A pointer is an object that contains the address of another object and allows indirect

    manipulate this object. Typically pointers are used to work with

    dynamically created objects to build related data structures, such as

    as linked lists and hierarchical trees, and for passing large

    objects - arrays and class objects - as parameters.

    Each pointer is associated with some data type, and their internal

    representation is independent of the internal type: and the size of memory occupied by the object

    type pointer, and their range of values ​​is the same5. The difference is how

    the compiler perceives the addressed object. Pointers to different types can have

    the same value, but the memory area where the corresponding types are located,

    may be different:

    memory 1000-1003 (in a 32-bit system);

    memory 1000-1007 (in a 32-bit system).

    When we apply the address operator (&) to an object of type int, we get

    result of type int*

    As your programs increase in size and complexity, you should break them down into small, easily manageable pieces called functions. Each function in your program should perform a specific task. For example, if you are writing a payment program, you might create one function that determines the number of hours an employee works, a second function that determines overtime pay, a third function that prints, etc. If the program needs to perform a specific task, it calls the appropriate function , providing this function with the information it will need during processing.

    Format input/output.

    Two functions: printf for output and scanf for input (next section) allow you to convert numerical values

    to symbolic representation and back. They also allow format strings to be generated and interpreted. Function

    printf(control, arg1, arg2, ...)

    converts the arguments to text form according to the formats specified in the control line and prints

    result to standard output. The control string contains two types of objects: ordinary characters, which are simply copied

    are sent to the output stream, and conversion specifications, each of which causes the next argument to be converted and printed.

    ment printf.

    Each conversion specification begins with a "%" character and ends with a conversion character (the letter that defines

    defining the type of transformation). Between the "%" and the conversion symbol there may be:

    A minus sign that causes the converted argument to be aligned to the left edge of the field.

    A string of numbers specifying the minimum field width. The converted number will be printed in a field of at least this width, and if necessary, a wider one. If the converted argument has fewer characters than the specified field width, then it will be padded on the left (or right if left-justified was specified) with padding characters up to that width. The padding character is usually a space, and if the field width is specified with a leading zero, then that character will be a zero (the leading zero in this case does not mean the octal width of the field).

    The point that separates the width of the field from the next line of numbers.

    String of digits (precision); specifies the maximum number of characters in a string that should be printed, or the number of digits to be printed to the right of the decimal point for float or double variables.

    Length modifier l, which indicates that the corresponding data element is of type long rather than int.

    The scanf function that performs input is analogous to printf and allows you to do many things in the opposite direction.

    from the same transformations. The scanf(control, arg1, arg2, ...) function reads characters from standard input, interprets them according to the format specified in the control argument, and places the results in the remaining arguments. The control line is described below; the other arguments, each of which must be a pointer, specify where the appropriately converted input should be placed.

    The control string usually contains conversion specifications that are used to directly interpret

    retration of input sequences. The control line may contain:

    Spaces, tabs, or newlines (“blanks”), which are ignored;

    Regular characters (not %), which are assumed to match the next non-whitespace characters in the input stream;

    A conversion specification consisting of a % character, an optional assignment suppression character *, an optional number specifying the maximum field width, and a conversion character.

    48.Line is a sequence of characters that is treated as one element. The string can contain letters, numbers, and various special characters. In SI, string literals, or constant strings, are enclosed in double quotes.

    A string in SI is an array of characters that ends with a null character ('\0'). A string is accessed through a pointer that refers to the first character of the string. The value of a string is the address of its first character. Thus, in SI it is legitimate to say that a string is it is a pointer, actually a pointer to the first character of a string. A string can be assigned in a declaration to either an array of characters or a variable of type char*.

    Structures.

    In modern programming languages, there is a special data type that can be

    may include several elements of simpler (and different!) types.

    A structure is a data type that can include several fields - elements

    different types (including other structures).

    One of the uses of structures is organizing various databases, lists, etc.

    Since a structure is a new data type, it must first be declared at the beginning

    char title; // name, character string

    int year; // year of publication, integer

    int pages; // number of pages, integer

    To refer to the entire structure, use its name, and to refer to an individual

    The name of this field is separated by a dot. Structure elements are introduced sequentially according to

    alone. You can fill them out in any order. You can work with a structure field in the same way as

    and with a variable of the appropriate type: numeric variables can participate in arithmetic

    In logical expressions, you can perform all standard operations on strings.

    strcpy(b.author, "A.S. Pushkin");

    Structures serve to process a large amount of information, so most often

    The program uses arrays of structures. They are announced in the same way as usual, but first

    Essentially (above) you need to declare the structure itself as a new data type.

    To access a structure field, a point is also used, but now you need to indicate it in a square

    in parentheses there is also the number of the desired structure, for example

    for (i = 0; i< 20; i ++) // цикл по всем структурам в массива

    puts(A[i].title); // display the title of the book

    Structures, just like any other types, can be parameters of functions and procedures.

    Working with files.

    Files can be text files (in which you can only write letters, numbers, parentheses and

    etc.) and binary (which can store any characters from the table).

    Text files

    (same as for linear arrays) errors of missing or insufficient data in the file.

    Binary files

    It is convenient to work with a binary file when the data has been written (or will be read)

    another program and they do not need to be viewed manually. The main advantage of this method is the speed of reading and writing, since the entire array is read (or written) at once as a single block.

    Descriptions of functions for working with files are in the stdio.h library

    First you need to create a pointer to a variable of type FILE (FILE* file;).

    The file is opened by calling the fopen function (file = fopen(file_name, "w");)

    The first parameter of this function is the file name, the second specifies in what mode the file should be opened. "w" - open for writing, "r" - open for reading, "a" - file appending (these are the most used modes, although there are others). Writing and reading data from a file is carried out by the following functions: fputc, fputs, fgetc, fgets, fprintf, fscanf (for a description of these functions, see stdio.h).

    Closing a file is done by calling the fclose function (fclose(file);).

    I/O

    The C language itself does not provide any input/output capabilities. All of these high-level mechanisms must be provided by explicitly callable functions.

    Output functions:

    Int putchar(int c).Put one character to stdout

    Int fputs(int c,FILE *f).Outputs one character to file f

    Int puts(char *s).Puts the string s up to the terminating character with code 0 to the standard output. Prints a newline at the end

    Int fputs(char *s,FILE *f).Outputs the string s up to the trailing character with the code into file f.

    Int printf(). Prints text in the specified format to standard output.

    The format description begins with the % symbol

    %d is a signed integer

    %u - unsigned integer

    %f is a real number

    %s-character

    Input functions:

    Int getchar(void).reads one character from stdin

    Int fgetc(FILE *f).Reads one character from file f

    Char *fgets(char *s,int size,FILE *f).Reads from file f a line of at most size characters, including the end-of-line character, and places it at address s

    Int scanf() Reads data from standard input according to the specified format

    24 C language data types.

    C has integer types of various sizes, signed and unsigned, floating point, characters, enums, and structs. In addition, the C language offers a union type, with which you can either store heterogeneous data in one memory location that does not overlap in lifetime (this allows you to save memory), or access the contents of a memory area as data of different types ( which allows you to change the data type interpretation without changing the data itself).

    There are several basic types in C:

    char - single byte, can contain one character from the valid character set;

    int is an integer, usually representing the machine's natural representation of integers;

    float - single precision floating point number;

    double - double precision floating point number.

    There are also several qualifiers that can be used in conjunction with the specified base types. For example, the short and long qualifiers apply to integers:

    long int counter;

    In such declarations, the word int can be omitted, which is usually done.

    The signed or unsigned qualifiers can be applied to the char type and any integral type. Unsigned values ​​are always positive or zero and obey the laws of modulo 2n arithmetic, where n is the number of bits in the type representation. So, if a char value has 8 bits, then unsigned char has values ​​in the range from 0 to 255, and signed char - from -128 to 127 (on a binary machine).

    numeral additional code). Whether values ​​of type simply char are signed or unsigned is implementation dependent, but in either case the printable character codes are positive.

    The long double type is intended for high-precision floating-point arithmetic. As with integers, the sizes of floating-point objects are implementation dependent; float, double and long double can

    may appear to be one size, or may be two or three different sizes.

    Arithmetic constants.

    An integer constant, such as 1234, is of type int. A long constant ends with the letter l or L, for example 123456789L; An integer that is too large to be represented as an int will be represented as a long.

    Unsigned constants end with the letter u or U, and the ending ul or UL indicates that the constant's type is unsigned long. Floating-point constants have a decimal point (123.4), or an exponential part (1e-2), or both. If they do not have a ending, they are considered to be of type double. The ending f or F indicates the float type, and 1 or L indicates the long double type.

    In addition to decimal, an integer value can have an octal or hexadecimal representation. If a constant starts from zero, then it is represented in octal form; if it starts from 0x or 0X, then it is represented in hexadecimal. For example, the decimal integer 31 can be written as 037 or as 0X1F. Octal and hexadecimal constants can be terminated with L (to indicate the type is long) and U (to indicate that the constant is unsigned). For example, the constant 0XFUL has the value 15 and the type is unsigned long.

    Character and string constants.

    A character constant is an integer written as a character surrounded by single quotes, such as "x". The value of a character constant is the numeric code of a character from the character set on a given machine. For example, the ASCII character constant "0" has the value 48, which has nothing to do with the numeric value 0. When we write "0" rather than some value (for example 48), depending on the encoding method, we make the program independent of the private meaning of the code, and it is also easier to read. Character constants can be used in number operations just like any other integer, although they are more often used to compare with other characters.

    Some characters in character and string constants are written using escape sequences, such as \n (newline); Such sequences are represented by two symbols, but represent one.

    In addition, an arbitrary octal code can be specified as "\ooo", where ooo is one, two or three octal digits (0...7) or "\xhh", where hh is one, two or more hexadecimal digits (0 ...9, a...f, A...F). So we could write

    #define VTAB "013" /* vertical tab in ASCII */

    #define BELL "\007" /* call to ASCII */

    or in hexadecimal:

    #define VTAB "\xb" /* vertical tab in ASCII */

    #define BELL "\x7" /* call to ASCII */

    The full set of escape sequences is as follows:

    \a bell signal

    \\ backslash

    \b return to step (downhole)

    \f page feed

    \n newline

    \r carriage return

    \t horizontal tab \v vertical-tab

    \? question mark

    \" single quote

    \" double quote

    \ooo octal code

    \xhh hexadecimal code

    The character constant "\0" is a character with a null value, the so-called null character. Instead of just 0, the notation "\0" is often used to emphasize the symbolic nature of the expression, although in both cases the notation denotes zero.

    A string constant, or string literal, is zero or more characters enclosed in double quotes, such as "I am a string constant" or “” (the empty string).

    Quotes are not included in the string, but serve only as delimiters. Just like character constants, escape sequences can be included in strings; \", for example, represents a double quotation mark. String constants can be concatenated ("glued together") at compile time; for example, writing two strings "Hello," "world!" is equivalent to writing one string like "Hello, world!".

    This property allows you to break long lines into parts and place these parts on separate lines.

    In fact, a string constant is an array of characters. The internal representation of a string requires a trailing null character "\0", so the string requires one byte more memory than the number of characters between the double quotes. This means that there is no limit to the length of the string you specify, but you must scan the entire string to determine its length.

    Constants

    Constants are called immutable quantities. There are integer, real, character and string constants. The compiler, having identified a constant as a token, classifies it as one of the types based on its appearance.

    The constant formats corresponding to each type are given in Table. 7.1.

    Table 7.1.

    Constants in C++

    Constant Format Examples
    Whole Decimal: a sequence of decimal digits not starting with zero unless it is the number zero 8, 0, 199226
    Octal: zero followed by octal digits (0,1,2,3,4,5,6,7) 01, 020, 07155
    Hexadecimal: Ox or OX followed by hexadecimal digits (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) 0xA, 0x1 B8, 0X00FF
    Real Decimal: [digits], [digits] 5.7, .001, 35.
    Exponential: [digits][.][digits](E|e)[+|-[digits] 0.2E6, .11e-3, 5E10
    Symbolic One or two characters enclosed in apostrophes "A", 'yu', "*", 'db' '\0' '\n', '\012' "\x07\x07"
    String A sequence of characters enclosed in quotation marks "Vasia was here", "\tValue r=\0xF5\"

    If it is necessary to form a negative integer or real constant, then the sign of the unary operation of changing the sign (-) is placed in front of the constant, for example: -218, -022, -0x3С, -4.8, -0.1е4.

    A real constant in exponential format is represented as a mantissa and an order. The mantissa is written to the left of the exponent sign (E or e), the order is written to the right of the sign. The value of the constant is defined as the product of the mantissa and the number 10 raised to the power indicated in the order. Please note that spaces within the number are not allowed, and to separate the integer part from the fractional part, a dot, rather than a comma, is used.

    The programmer can set the type of the constant independently. Either the whole part or the fractional part can be omitted, but not both at once. If both parts are specified, the dot character is required.

    Character constants, consisting of one character, occupy one byte in memory and have the standard type char. Two-character constants occupy two bytes and are of type int, with the first character placed in the byte with the lower address.

    The backslash character is used to represent:

    · codes that do not have a graphic image (for example,
    \a – sound signal, \n – move the cursor to the beginning of the next line);

    · apostrophe ("), backslash (\), question mark (?) and quotation marks (");

    · any character using its hexadecimal or octal code, for example, \073, \0xF5. The numeric value must be in the range from 0 to 255.

    Sequences of characters starting with a backslash are called managers, or escape sequences. In table 7.2. their permissible values ​​are given. The escape sequence is interpreted as a single character. If a backslash is immediately followed by a character not included in the table. 7.2, the result of the interpretation is undefined. If an invalid digit occurs in a sequence, it is considered the end of the digital code.


    Table 7.2.

    Escape sequences in C++

    Escape sequences can also be used in string constants, otherwise called string literals. For example, if you need to write a quotation mark inside a string, it is preceded by a slash, which is how the compiler distinguishes it from the quotation mark delimiting the string:

    "Publishing house \"Peter\""

    All string literals are treated by the compiler as distinct objects.

    String constants separated in the program only by whitespace characters are combined into one during compilation. A long string constant can be placed on multiple lines by using a backslash followed by a line feed as the hyphen. These characters are ignored by the compiler, and the next line is treated as a continuation of the previous one. For example, the line

    A character constant is a sequence of one or more characters enclosed in single quotes (for example "x"). If there is a single character inside single quotes, the value of the constant is the numeric value of that character in the encoding used on the machine. The meaning of a multi-character constant is implementation dependent.

    A character constant cannot contain a single quote " or a newline; certain other characters can be used to represent them escape sequences:

    Escape sequence \ooo consists of a backslash followed by one, two, or three octal digits specifying the meaning of the desired character. The most common example of such a construct is \0 (not followed by a digit); it specifies a NULL character. Escape sequence \xhh consists of a backslash followed by a letter x, followed by hexadecimal digits specifying the meaning of the desired character. There is no limit on the number of digits, but the result will be undefined if the value of the resulting character exceeds the value of the "largest" allowed character. If in this implementation the type char is treated as a signed number, then the value in both octal and hexadecimal escape sequences is obtained using "sign propagation", as if a cast operation was performed char. If \ is not followed by any of the characters listed above, the result is undefined.

    Some implementations have an extended character set that cannot be covered by the type char. The constant for such a set is written with the letter L ahead (for example L"x") and is called an extended symbolic constant. This constant has the type wchar_t(integer type defined in the standard header file ). As with regular character constants, octal and hexadecimal escape sequences are also possible here; if the specified value exceeds the type wchar_t, the result will be undefined.

    Some of the escape sequences given are new (hexadecimal ones in particular). The extended type for symbols is also new. Character sets commonly used in America and Western Europe are suitable for the type char, and type wchar_t was added mainly for Asian languages.

    A2.5.3. Floating point constants

    A floating point constant consists of an integer part, a decimal point, a fractional part, e or E and an integer (possibly signed) representing the order, and possibly a type suffix given by one of the letters: f, F, l or L. Both the integer and fractional parts are a sequence of numbers. Either the whole part or the fractional part (but not both) may be missing; there may also be a missing decimal point or E with order (but not both at the same time). The type is determined by the suffix: F or f determine the type float, L or l- type long double; if there is no suffix, the type is assumed double.

    Suffixes for floating point constants are new.

    A2.5.4. Enum constants

    Identifiers declared as enum members (A8.4) are type constants int.

    A2.6. String literals

    A string literal, also called a string constant, is a sequence of characters enclosed in double quotes (For example, "..."). The string is of type "character array" and has a memory class static(A4), which is initialized with the given characters. Whether identical string literals are represented by a single copy or multiple copies is implementation dependent. The behavior of a program attempting to modify a string literal is undefined.

    String literals written in series are combined (concatenated) into one line. After any concatenation, a NULL byte (\0) is added to the string, allowing the program traversing the string to find the end of the string. String literals cannot contain a newline character or a double quote; they must use the same escape sequences as in character constants.

    As with character constants, a string literal with extended set characters must begin with a letter L(for example L"..."). A superset string literal is of type "array of wchar_t". The concatenation of regular and "extended" string literals with each other is undefined.

    The fact that string literals are not necessarily represented by different copies, the prohibition on their modification, as well as the concatenation of adjacent string literals are innovations of the ANSI standard. "Extended" string literals are also declared for the first time.

    A3. Syntax notation

    In the syntax notation used in this manual, syntactic concepts are typed in italics and words and symbols taken literally are typed in regular font. Alternative designs are usually listed in a column (each alternative on a separate line); In rare cases, long lists of small alternatives appear on a single line, labeled "one of." An optional word-term or non-term is provided with the index " necessary". So, record

    { expression required }

    denotes an expression enclosed in curly braces, which in general may not be present. A complete list of syntactic structures is given in A13.

    Unlike the grammar given in the first edition of this book, the grammar given here explicitly describes the precedence and order of operations in expressions.

    A4. What do identifiers mean?

    Identifiers, or names, refer to different objects (in the original - things. - Note. ed.): functions; tags for structures, unions and enumerations; elements of structures or associations; typedef-names; marks and objects. An object (sometimes called a variable) is a piece of memory whose interpretation depends on two main characteristics: memory class n her type. The memory class reports the lifetime of the memory associated with the object being identified, the type specifies what kind of values ​​are in the object. Any name is associated with its own scope (that is, the part of the program where this name is known) and a link attribute that determines whether this name in another file represents the same object or function. Scope and link attribute are discussed in A11.

    A4.1. Memory class

    There are two memory classes: auto And static. Several keywords, together with the object declaration context, specify the storage class for those objects.

    Automatic objects are local to the block (A9.3), and when leaving it they “disappear”. A declaration given inside a block, if it lacks a storage class specification or specifies the auto qualifier, will create an automatic object. An object marked in the ad with the word register, is automatic and is placed, if possible, in the machine register.

    Static objects can be block-local or outside of blocks, but in both cases their values ​​are retained after the block (or function) is exited until it is re-entered. Inside a block (including the block that forms the body of the function), static objects in declarations are marked with the word static. Objects declared outside all blocks at the same level as function definitions are always static. Using a keyword static they can be made local within the translation unit (in which case they receive the attribute intercom), and they become global for the entire program if you omit the explicit memory class or use the keyword extern(in this case they receive the attribute external communications).

    A4.2. Basic types

    There are several basic types. Standard header file , described in Appendix B, defines the largest and smallest values ​​for each type in that particular implementation. Appendix B provides the minimum possible values.

    The size of objects declared as symbols allows them to store any character from the machine's accepted character set. If the object is of type char really stores a character from a given set, then its value is the code of this character, i.e. some non-negative integer. Type variables char can store other values, but then the range of their values, and especially whether these values ​​are signed or unsigned, is implementation dependent.

    Unsigned characters declared using words unsigned char, have the same bit depth as regular characters, but represent non-negative values; using words signed char You can explicitly declare signed characters, which take up the same amount of space as regular characters.

    Type unsigned char was not mentioned in the first edition of the language, but was used by everyone. Type signed char- new.

    Besides char among integer types there can be integers of three sizes: short int, int And long int. Regular objects of type int have a natural size adopted in the architecture of a given machine, other sizes are intended for special needs. Longer integers at least cover all the values ​​of shorter integers, however in some implementations regular integers may be equivalent to short ones ( short) or long ( long) whole. All types int represent signed values ​​unless otherwise noted.

    For unsigned integers, the keyword is used in declarations unsigned. Such integers obey arithmetic modulo 2 to the power n, Where n is the number of bits in the representation of a number, and hence there is never overflow in unsigned integer arithmetic. The set of non-negative values ​​that can be stored in signed objects is a subset of the values ​​that can be stored in corresponding unsigned objects; the signed and unsigned representations of each such value are the same. Any two of the floating point types: single precision ( float), double precision ( double) and with increased accuracy ( long double) may be synonyms, but each subsequent type of this list must at least ensure the accuracy of the previous one.

    long double- new type. In the first edition of the language, a synonym for double was long float, the latter has now been withdrawn from circulation.

    Transfers- one-of-a-kind types that are given a complete list of values; Each enum has a set of named constants associated with it (A8.4). Enumerations behave like integers, but the compiler usually issues a warning message if an object of some enum type is assigned something other than its constant or an expression not from that enumeration.

    Because enumeration objects can be thought of as numbers, an enumeration is an arithmetic type. Types char And int of all sizes, each of which can be signed or unsigned, and the enumerations are called integers (integral) types. Types float, double And long double are called c types floating point.

    Type void specifies an empty set of values. It is used as the "return type of a function" when the function does not produce any resultant value.

    A4.3. Derived types

    In addition to the basic types, there is an almost endless class of derived types that are formed from existing ones and describe the following structures:

    · arrays objects of a given type;

    · functions, returning objects of a given type;

    · pointers to objects of a given type;

    · structures, containing a sequence of objects, possibly of different specified types;

    · associations, each of which can contain any of several objects of different specified types.

    In general, the above methods for constructing objects can be used recursively.

    A4.4. Type qualifiers

    An object type can be supplied with a qualifier. Declaring an object with a qualifier const indicates that its value will not change further; declaring an object as volatile(changeable, fickle ( English)), we point out its special properties for optimization performed by the compiler. None of the qualifiers affect the value ranges and arithmetic properties of objects. Qualifiers are discussed in A8.2.

    A5. Objects and Lvalues

    Object- this is some named memory area; lvalue is an expression denoting an object. An obvious example lvalue is an identifier with the corresponding memory type and class. There are operations that generate lvalue. For example, if E- an expression of pointer type, then *E there is an expression for lvalue, denoting the object pointed to E. The term " lvalue" comes from the assignment notation E1 = E2, in which the left ( left- left ( English), hence the letter l, value- value) operand E1 must be an expression lvalue. By describing each statement, we tell whether it expects lvalue as operands and whether it produces lvalue as a result.

    A6. Transformations

    Some operators, depending on their operands, can cause their values ​​to be converted from one type to another. This paragraph explains what to expect from such conversions. A6.5 states the rules by which conversions are performed for most common operators. When considering each individual operator, these rules may be refined.

    In the program Strings, or string constants, are represented by a sequence of character images enclosed in quotation marks (not apostrophes), such as “any characters.” Among the characters of the string there may be escape sequences corresponding to the codes of non-representable (special) character constants.

    Examples:

    “1234567890”

    “\tcomposition of the presidium”

    “beginning of line \t and end of line.”

    As termination character The symbol with code 0 is selected (do not confuse it with the symbol "0"). So the definition

    char HelloStr = "Hello, world";

    actually interpreted as

    char HelloStr = ("H", "e", "l", "l", "o", " ", ", "w", "o", "r", "l", "d" , "\0");

    This happens when an array is initialized with a string. In all other cases, the encountered string is interpreted as an unnamed array of the appropriate length that has not yet been created. That is, the expression

    printf("Hello, world\n");

    is actually interpreted as

    char str1 = "Hello, world\n";

    When placing a string in memory, the translator automatically adds the character ‘\0’ to its end, i.e. null byte.

    The number of elements in such an array is 1 more than in the image of the corresponding string constant, because a null byte ‘\0’ was added to the end of the line.

    Strings have another feature: the translator assigns each string a separate place in the computer memory, even in cases where several strings completely coincide (the SI language standard suggests that in specific implementations this rule may not be followed).

    Assign a value to a character array(i.e., a string) cannot be done using a regular assignment operator. You can place a string in an array either using initialization (when defining a character array) or using input functions. In function scanf() or printf() for character strings the specification is used transformation % s.

    Example:

    /*print a character string*/

    #include

    ( char B=”Open Sesame!”;

    printf(“%s”,B); ) /*end of program*/

    Result program execution: Sesame, open!

    In the program, the length of the array IN– 17 elements, i.e. the length of the string to be placed in the array (16 characters), plus the null byte at the end of the string. It is 17 bytes that are allocated when initializing the array in the example above. Initializing a character array with a string constant is a shorthand version of array initialization and was introduced into the language for simplicity. You can use normal initialization by placing the initial values ​​of the array elements in curly braces and remembering to place the special line ending character ‘\0’ at the end of the list of initial values. Thus, the following initialization of array B was allowed in the program:

    char B = ('C','e','z','a','m',',',' ','o','t','k','p','o' ,'th','s','i','!','\0');

    How to enter strings or display them on the screen?

    The line can be output either by a function already known to you printf() with input specifier "%s", or the special function int puts (char *string), which prints the string string to the screen and returns some non-zero value if successful.

    Why do we need the specifier " %s"? This is done so that you can print strings with any characters. Compare:

    Example:

    char str = "I wanted to output %d...";

    printf("%s", str); /* Correct option */

    printf("\n"); /* Newline delimiter */

    printf(str); /* Incorrect option */

    In the first In this case, the function will print exactly what is required of it. But in second case printf(), having met in the line str The "%d" specifier (after all, now this line is the first, which means it specifies the output format) will conclude that it must be followed by a number. And since it does not follow, instead of " %d" some garbage will be printed - the number currently on the stack.

    The consequences may be more serious if the line contains the sequence "%s", it will treat it as a string and print it until it encounters a null character. And where she will meet him, how much she will have time to type and whether it will crash because she is not accessing her memory - no one knows.

    The line ending convention must be observed when forming strings from individual characters in programs. As an example, consider the following

    /*reading a line from the terminal*/

    int getline(char s, / *entered string*/

    int lim) /*its maximum length*/

    ( int c, i; /* c – input character */

    for (i=0; i

    s[i] = '\0';

        String input/output. Basic String Functions

    One of the most popular I/O operations is the character string I/O operation. String I/O functions are included in the SI library for exchanging data via standard input/output streams gets() And puts(), which are convenient to use when creating dialog systems.

    For input string there is a function with a prototype

    char *gets (char *string),

    which reads a line from the keyboard and puts it into a buffer string, a pointer to which it returns. If an error occurs, EOF is returned.

    The function has only one argument – pointer s < stdio. h>.

    gets() function finishes its work when entering a character ‘\ n, which is automatically transferred from the keyboard to the computer when a key is pressed . At the same time, the symbol itself ‘\ n is not written to the input string. Instead, a null character is placed in the string ‘\0’ . Thus, functiongets() produces the input of the “correct” string, not just sequences of characters.

    Here you should pay attention to the following feature of entering data from the keyboard. Function gets() starts processing information from the keyboard only after pressing a key < Enter>. Thus, it “wait” until it is dialed necessary information and the key is pressed<Enter>. Only after this does data entry into the program begin.

    Example: #include

    int main (void) (

    printf("Input a string:");

    printf("The string input was: %s\n", string);

    Function puts() (line output to the screen) if successful, returns the last character printed, which is always a character ‘\ n" , if an error occurs, it is returned from the EOF function. The prototype of this function looks like this:

    int puts(char*s); /*string output function*/

    The function has only one argument – pointer s to an array of characters. The function prototype is described in the file < stdio. h>.

    Let's give the simplest example use of these functions.

    #include

    char strl = ”enter the employee’s last name:”;

    Recall that any string of characters in the SI language must end with a null character ‘\0’ . To the last element of the array strl The null character will be written automatically during translation when the array is initialized. For function puts() The presence of a null character at the end of the string is required.

    Otherwise, i.e. when there is no character in the character array‘\0’ , the program may terminate abnormally, because functionputs () in search of a null character, it will go through all available memory byte by byte, starting, in our example, from the address strl . This must be remembered if the program generates a string to display it on the display screen.

    Example : #include

    int main (void) (

    char string = "This is an example output string\n";

        String manipulation functions

    Since there is no predefined type for a string in C, there are no operations that are so familiar to many, such as comparing and concatenating strings, implemented in many languages ​​as addition and comparison operators. Here, adding arrays is not allowed, and when comparing, not the strings themselves will be compared, but only pointers on them, which, of course, is not interesting to us.

    For string manipulation there is a set of functions declared in the file (those writing on Windows may include the file instead<windows.h>).

    The most important functions:

      int strcmp (char *string1, char *string2)

    carries out comparing two strings. Returns a negative number if the first row is less than the second, 0 if the rows are equal and a positive number if the first row is greater than the second. In more detail, the function returns the difference between the codes of the first unequal characters encountered (if the strings are unequal in length, then a non-zero character will be compared with zero).

    Example :

    #include

    #include

    int main (void) (

    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";

    ptr = strcmp(buf2, buf1);

    if (ptr > 0)

    printf("buffer 2 is greater than buffer 1 \n");

    printf("buffer 2 is less than buffer 1 \n");

    ptr = strcmp(buf2, buf3);

    printf("buffer 2 is greater than buffer 3\n");

    printf("buffer 2 is less than buffer 3\n");

    The following will appear on the screen:

    buffer 2 is greater than buffer 1

    buffer 2 is less than buffer 3

      char *strcpy (char *dest, char *source)

    carries out copying lines source in place of the line dest. Again, make sure that the entire line fits in the space provided. The function returns a pointer to the destination string.

    Example: #include

    #include

    int main (void) (

    char *str1 = "a b c d e f g h i";

    strcpy(string, str1);

    printf("%s \n", string);

    The screen will display: a b c d e f g h I

      char *strcat (char *string1, char *string2)

    carries out joining two lines. The second line is added to the end of the first. The function does not check (and technically cannot check) the presence of the required amount of memory at the end of the first line - you must take care of this. The function returns a pointer to the first row.

    Example:

    #include

    #include

    int main(void) (

    char destination;

    char *blank = " ", *c = "C++", *turbo = "Turbo";

    strcpy(destination, turbo); //Copy the string "turbo"

    in place destination

    strcat(destination, blank); // Gluing destination...

    strcat(destination, c); // first with blank, then with c

    printf("%s\n", destination);

    The screen will appear: Turbo C++

      int strlen (char *string)

    returns line lengthstring (not counting the null character).

      char *strdup (char *string)

    creates duplicate linestring and returns a pointer to it. Please note that, unlike other functions, strdup creates a string itself and therefore, after you no longer need it, do not forget to free it.

      char *strncpy (char *dest, char *source, int count)

      char *strncat (char *string1, char *string2, int count)

    similarly strcpy And strcat, but only the first ones are copied count characters. The functions don't add a terminating null to the string—you'll have to do that yourself.

      char *strchr (char *string, int c)

      char *strstr (char *string, char *substring)

    looking for first occurrence to line string respectively symbol c and substrings substring. Both functions return the address of the first occurrence or NULL, if one is not found.

    Example :

    int main (void) (

    char *ptr, c = "r";

    //Create a line

    strcpy(string, "This is a string");

    ptr = strchr(string, c);

    printf("The character %c is at position: %d\n", c, ptr-string);

    printf("The character was not found\n");

    Character constants

    Constants real type

    Integer constants

    General format: ± n(+ is usually not included).

    Decimal constants- a sequence of numbers 0...9, the first of which should not be 0. For example, 22 and 273 are ordinary integer constants; if you need to enter a long integer constant, then the attribute is indicated L(l) - 273L (273l). For such a constant, 4 bytes will be allocated. A regular integer constant that is too long for the type int, is treated as a longer type ( long or long long).

    There is a notation system for octal and hexadecimal constants.

    Octal constants- a sequence of numbers from 0 to 7, the first of which must be 0, for example: 020 = 16-decimal.

    Hexadecimal constants- a sequence of numbers from 0 to 9 and letters from A to F (a...f), starting with the symbols 0Х (0х), for example: 0X1F (0х1f) = 31 decimal.

    Octal and hexadecimal constants can also end with the letter L(l) - long, for example, 020L or 0X20L.

    Examples of integer constants:

    1992 13 1000L - decimal;

    0777 00033 01l - octal;

    0x123 0X00ff 0xb8000l - hexadecimal.

    These constants are stored in memory in double format, and in external representation they can have two forms:

    1) with fixed decimal point, recording format: ± n.m, Where n, m- integer and fractional parts of a number;

    2) floating decimal point (exponential form): ± n.mp, Where n, m- integer and fractional parts of a number, r- order, for example, 1.25×10 -8 is written as 1.25E-8.

    Examples of fixed-point and floating-point constants:

    1.0 -3.125100е-10 0.12537е+13

    A character constant is a character enclosed in single quotes: "A", "x" (occupies 1 byte).

    The C language uses and. special (managing ) characters not displayed on the screen; their purpose is to influence the order in which other characters are displayed. Since they are not displayed on the screen, a pair of characters is used to indicate them in the program text, the first of which is always a backslash ( backslash ) ("\"). The main ones are:

    The backslash in character and string (see below) constants also represents some ordinary characters whose writing there would lead to ambiguity:

    When assigning to a character variable, these sequences must be enclosed in apostrophes. Character constant "\0" (not to be confused with the symbol - number"0" !) is often written instead of the integer constant 0 to emphasize the symbolic nature of some expression (see the topic "Strings").



    Examples of character constants: "A", "9", "$", "\n", "\"".

    A string constant is a sequence of ASCII characters enclosed in quotation marks ("). In the internal representation, the null character "\0" is added to string constants, also called a null terminator, marking the end of the string. Quotes are not part of the string, but only serve to delimit it. A string is an array consisting of characters. Internal representation of the constant "01234\0ABCDEF":

    "0","1","2","3","4","\0","A","B","C","D","E","F"," \0"

    Examples of string constants:

    "System", "\n\tArgument\n", "State \"WAIT\""

    The compiler automatically places a null character (null terminator) at the end of a string constant. Null character - this is not the number 0, it is not printed and has code 0 in the ASCII code table.

    For example, the string "" is an empty string containing only a null terminator.

    2.8. Named Constants