You get a bonus - 1 coin for daily activity. Now you have 1 coin

1. BASIC TERMS AND DATA OF C

Lecture



The C language, created by Denis Ritchie in the early 1970s at the Bell Laboratory of the American AT & T Corporation, is one of the universal programming languages. The C language is considered the language of system programming, although it is also convenient for writing application programs. Among the advantages of the C language, it is worth mentioning the portability of programs on computers of various architectures and from one operating system to another, the conciseness of writing algorithms, the logical symmetry of programs, and the ability to obtain program code comparable in speed to execution with programs written in assembly language. The latter is due to the fact that although C is a high-level language with a full set of structured programming constructs, it also has a set of low-level tools that provide access to computer hardware. Since 1989, the C language is governed by the standard of the American Institute of National Standards ANSI C. Currently, in addition to the ANSI C standard, the international standard ISO C (International Standard Organization C) has been developed.

The manual in sections 1-6 discusses the basic constructs of the C language (common to C and C ++). Examples of programs are given in section 7.

SECTION 1. BASIC TERMS AND DATA

Basic language concepts

A program written in C consists of operators. Each statement causes the execution of certain actions in the corresponding step of the program.

When writing operators, Latin uppercase and lowercase letters, numbers and special characters are used. Examples of such characters are: a period (.), A comma (,), a colon (:), a semicolon (;), etc. The set of characters used in the language is called the alphabet of the language.

In a personal computer, symbols are stored as codes. The correspondence between each character and its code is specified by a special code table. It developed the ASCII standard, so character codes are called ASCII codes.

Distinguish between visible and control characters . The first can be displayed on the display screen or printed on the printer. The second ones cause certain actions in the car, for example: beep - code 7 10 , return the cursor one step - code 8 10 , horizontal tabulation - code 9 10 , move the cursor to a new line - code 10 10 , move the cursor to the beginning of the line - code 13 10 etc. Such control characters have decimal numbers 0-31, 127.

For the representation of each character in a personal computer, one byte is used, so the total number of characters is 2 8 = 256. The code table that establishes the correspondence between the character and its code has 256 lines of the form:

 
      symbol_code_in_asic_time_number_number is a symbol.

The first half of the code table is standard, and the second is used to represent characters of national alphabets, pseudographic elements, etc.

An important concept of a language is an identifier, which is used as the name of an object (function, variable, constant, etc.). Identifiers should be selected according to the following rules:

  1. They must begin with a letter of the Latin alphabet (a, ..., z, A, ..., Z) or with an underscore (_).
  2. They can use Latin letters, underscores and numbers (0, ..., 9). The use of other characters in identifiers is prohibited.
  3. In C, lower case letters (a, ..., z) used in identifiers are different from upper case letters (A, ..., Z). This means that the following identifiers are considered different: name, NaMe, NAME, etc.
  4. Identifiers can be of any length, but only part of the characters are perceived and used to distinguish objects (functions, variables, constants, etc.). Their number varies for different programming systems, but in accordance with the ANSI C standard does not exceed 32 (in C ++, this restriction is removed). If the identifier length is set to 5, then the names of count and counter will be identical, since they have the first five characters that match.
  5. The identifiers for new objects should not coincide with the keywords of the language and the names of standard functions from the library.

In C programs, an important role is played by comments. They increase the visibility and ease of reading programs. Comments are framed with the characters / * and * /. They can be recorded anywhere in the program.

In C ++, another form of recording comments is introduced. Anything after the // to the end of the current line will also be treated as a comment. Note that the C compiler built into the Borland C ++ programming system allows using this comment in C programs as well.

Spaces, tabs and newlines are ignored in C programs. This allows you to record various expressions in a well-readable form. In addition, program lines can be started from any position, which makes it possible to select groups of operators in the text.

Data types

Programs operate on a variety of data that can be simple and structured. Simple data is integers and real numbers, symbols and pointers (addresses of objects in memory). Integers do not have, and real numbers have a fractional part. Structured data is arrays and structures; they will be discussed below.

In the language, the concepts of "data type" and "type modifier" are distinguished. The data type is, for example, integer, and the modifier is signed or unsigned. A signed integer will have both positive and negative values, and an unsigned integer will have only positive values. There are five basic types in C language, which are defined by the following keywords:

  • char - character;
  • int is a whole;
  • float - real;
  • double - real double precision;
  • void - meaningless

We give them a brief description:

  1. A variable of type char has a size of 1 byte, its values ​​are various characters from the code table, for example: 'f', ':', 'j' (when writing in the program, they are enclosed in single quotes).
  2. The size of an int variable is not defined in the C standard. In most programming systems, the size of an int variable corresponds to the size of the whole machine word. For example, in compilers for 16-bit processors, an int variable is 2 bytes in size. In this case, the sign values ​​of this variable can lie in the range from -32,768 to 32767.
  3. The float keyword allows you to define variables of real type. Their values ​​have a fractional part, separated by a point, for example: -5.6, 31.28, etc. Real numbers can also be written in floating point form, for example: -1.09e + 4. The number before the symbol "e" is called the mantissa, and after the "e" - the order. A float variable takes 32 bits in memory. It can take values ​​in the range from 3.4e-38 to 3.4e + 38.
  4. The double keyword allows you to define a real double precision variable. It takes up twice as much memory in memory as a float variable (i.e., its 64-bit size). A double variable can take values ​​in the range from 1.7e-308 to 1.7e + 308.
  5. The keyword void (meaningless) is used to neutralize the value of an object, for example, to declare a function that returns no value.

An object of some basic type can be modified. For this purpose, special keywords called modifiers are used. The ANSI C standard contains the following type modifiers:

  • unsigned
  • signed
  • short
  • long

Modifiers are written before type specifiers, for example: unsigned char. If the specifier is omitted after the modifier, the compiler assumes that the specifier is an int. Thus, the following lines:

  long a;  
       long int a;  
 

are identical and define an object as a long integer. Tab. 1 illustrates possible combinations of modifiers (unsigned, signed, short, long) with specifiers (char, int, float and double), and also shows the size and range of the object (for 16-bit compilers).

Table 1

Type of Size in bytes (bits) Change interval
char 18) from -128 to 127
unsigned char 18) from 0 to 255
signed char 18) from -128 to 127
int 2 (16) from -32768 to 32767
unsigned int 2 (16) from 0 to 65535
signed int 2 (16) from -32768 to 32767
short int 2 (16) from -32768 to 32767
unsigned short int 2 (16) from 0 to 65535
signed short int 2 (16) from -32768 to 32767
long int 4 (32) from -2147483648 to 2147483647
unsigned long int 4 (32) 0 to 4294967295
signed long int 4 (32) from -2147483648 to 2147483647
float 4 (32) from 3.4E-38 to 3.4E + 38
double 8 (64) from 1.7E-308 to 1.7E + 308
long double 10 (80) from 3.4E-4932 to 3.4E + 4932

Variables and constants

All variables must be defined (declared) before they are used. This sets the type, and then comes a list of one or more variables of this type, separated by commas. For example:

  int a, b, c; 
       char x, y; 
 

The language distinguishes the concept of a variable declaration and its definition. The declaration sets the properties of an object: its type (for example, integer), size (for example, 4 bytes), etc. The definition along with this causes a memory allocation (in the given example the definition of variables is given).

Variables can be divided in rows in an arbitrary way, for example:

  float a; 
       float b; 
 

Variables in the C language can be initialized when they are defined:

  int a = 25, h = 6; 
       char g = 'Q', k = 'm'; 
       float r = 1.89; 
       long double n = r * 123; 
 

We now find out where the data is defined in the program text. In the language, global and local objects are possible. The former are defined outside the functions and are therefore available for any of them. Local objects are internal to the functions. They begin to exist, upon entering a function, and are destroyed after exiting it. The following shows the structure of the C program and possible places in the program where global and local objects are defined.

  int a;  / * Definition of a global variable * / 

  int function (int b, char c);  / * Function declaration (i.e. description  
                                     its title) * / 

  void main (void) 
  {// Body of the program 
       int d, e;  // Define local variables 
       float f;  // Define a local variable 
         ...            
  } 
  int function (int b, char c) / * Definition of function and formal  
                                  parameters (essentially local  
                                  variables) b and c * / 
  {// Function body 
       char g;  // Define a local variable 
         ... 
  } 
 

Note that program execution always starts with a call to the main () function, which contains the program body. The body of the program, like the body of any other function, is placed between the opening and closing curly braces.

In C, all definitions must follow the statements that make up the function body. In C ++, this restriction is removed and definitions can be located anywhere in the program. If they are made in functions, then the corresponding objects will be local, and if outside functions, then global.

Along with variables in the language, there are the following types of constants:

  • real, for example 123.456, 5.61e-4. They can be supplied with the suffix F (or f), for example 123.456F, 5.61e-4f;
  • integers, for example 125;
  • short integers, at the end of which the letter (suffix) H (or h) is added, for example 275h, 344H;
  • long integers, at the end of which the letter (suffix) L (or l) is added, for example, 361327L;
  • Unsigned, at the end of which the letter U (or u) is added, for example 62125U;
  • octal, in which a zero (0) is written before the first significant digit, for example, 071;
  • hexadecimal, in which a pair of zero-x (0x) characters is written before the first significant digit, for example, 0x5F;
  • character is the only character enclosed in single quotes, for example, 'O', '2', '.' etc. Characters that do not have a graphic representation can be written using special combinations, for example, \ n (code 10), \ 0 (code 0). These combinations look like two characters, although in fact they are one character. Any binary image of one byte can also be represented: '\ NNN', where NNN is from one to three octal digits. Hexadecimal setting of character codes is also allowed, which is represented as: '\ x2B', '\ xZ6', etc .;
  • string - a sequence of zero characters and more, enclosed in double quotes, for example: "This is a string constant." Quotes are not included in the string, but only limit it. The string is an array of the listed elements, at the end of which is placed a byte with the symbol '\ 0'. Thus, the number of bytes required to store a string is one greater than the number of characters between double quotes;
  • a constant expression consisting of some constants, which is calculated during the broadcast (for example: a = 60 + 301);
  • type long double, at the end of which the letter L (or l) is added, for example: 1234567.89L.

How to enter and display information

Input / output operations in the C language are organized by means of library functions (and there are quite a lot of them).

The simplest input mechanism is reading one character at a time from the standard input stream (from the keyboard) using the getchar () function. It has the following prototype (i.e. title description):

  int getchar (void); 
 

Here the type of the single argument (void) and the type of the function return value (int) are defined.

Operator type:

  x = getchar ();  
 

assigns the variable x to the next character entered. The variable x must be of character or integer type.

Another function, putchar (x), outputs the value of the variable x to the standard output stream (on the display screen). The putchar () function has a prototype:

  int putchar (int);  
 

The declarations getchar () and putchar () are made in the stdio.h header file, which contains descriptions of the header functions of standard input / output library functions. To make library functions available to the program, this file must be connected to it. Connection is done using preprocessor directive

  #include  
 

placed at the beginning of the program (for details, see Section 5).

Note that for the function getchar (), after selecting a character, you must press the key. Sometimes it creates certain inconveniences. The getch () and getche () functions eliminate them. They have the following prototypes:

  int getch (void); 
       int getche (void);  
 

Both of these functions enter the character immediately after pressing the corresponding key (here it is not necessary to additionally press the key). The difference between them is that getche () displays the character being entered on the display screen, but getch () does not. Prototypes of these functions are contained in the conio.h file (console input / output). To use them, the conio.h file must also be connected to the program using the #include directive.

Formatted data output

The printf () function (the prototype is contained in the stdio.h file) provides formatted output. It can be written in the following formal form:

  printf ("control string", argument _1, argument _2, ...); 
 

The control line contains components of three types: ordinary characters that are simply copied to the standard output stream (displayed on the display screen); conversion specifications, each of which causes the next argument to be displayed on the screen from the following list; control character constants.

Each conversion specification begins with a% sign and ends with some symbol that defines the conversion. There may be other characters between the% sign and the conversion symbol according to the following format:

  % [signs] [field width] [accuracy] [F | N | h | l | L] c_n 
 

All parameters in square brackets are optional.

In place of the parameter c_n (conversion character) can be written:

with
    • - the value of the argument is a character;

d
    • or
i
    • - the argument value is a decimal integer;

e
    • - the argument value is a real decimal number in exponential form of the form 1.23e + 2;

E
    • - the argument value is a real decimal number in exponential form of the form 1.23E + 2;

f
    • - the argument value is a floating point decimal number;

g
    • (or
G
    • ) - used as e or f, and excludes the output of non-significant zeros;

about
    • - the argument value is an octal integer;

s
    • - the value of the argument is a string of characters (characters of the string are output until the end of line character is encountered, or the number of characters specified by the precision is displayed);

u
    • - the value of the argument is an unsigned integer;

x
    • - the argument value is a hexadecimal integer with the digits 0, ..., 9, a, b, c, d, e, f;

X
    • - the argument value is a hexadecimal integer with the digits 0, ..., 9, A, B, C, O, E, F;

R
    • - the argument value is a pointer;

n
    • - used in formatting operations. The argument corresponding to this specification symbol must be a pointer to an integer. It returns the position number of the line (displayed on the screen) in which the specification% n is written.

Optional parameters in the conversion specification:

  • The minus sign (-) indicates that the converted parameter should be aligned to the left in its field;
  • The plus sign (+) requires the output of the result with a sign;
  • a string of numbers specifying the minimum field size (field width). It can also use the * symbol, which also allows you to set the minimum width of the field and the accuracy of the representation of the output number;
  • a dot (.) separating the size of the field from the next row of numbers;
  • a string of numbers that specifies the maximum number of characters to be output, or the number of digits that are displayed to the right of the decimal point in values ​​of the float or double types (precision);
  • character F, defining pointer type far;
  • the symbol N defining a pointer of the type near;
  • the h character, defining the argument of the type short int (used together with the conversion characters d, i, o, u, x, x);
  • the l character, indicating that the corresponding argument is of type long (in the case of d, i, o, x, X transformation characters) or double (in the case of e, E, f, g, G transformation characters);
  • the symbol L, indicating that the corresponding argument is of the type long double (used in conjunction with the transformation symbols e, E, f, g, G);
  • the # symbol, which may appear before the transformation characters g, f, e, and before the x character. In the first case, the decimal point will always be displayed, and in the second - the prefix 0x before the corresponding hexadecimal number.

If there is no conversion character after the% sign, then it is displayed on the screen. Thus, the string %% leads to the display of the% sign.

The printf () function uses a control string to determine how many arguments there are and what their types are. Arguments can be variables, constants, expressions, function calls; The main thing is that their values ​​correspond to a given specification.

If there are errors, for example, in the number of arguments or the type of conversion, the results will be incorrect.

Among the control character constants, the following are most often used:

    • \ a - for short-term sounding;
    • \ b - to move the cursor left one position;
    • \ f - to feed the format;
    • \ n - to go to a new line;
    • \ r - for carriage return;
    • \ t - horizontal tab;
    • \ v - vertical tab;
    • \\ - output character \;
    • \ '- output the character';
    • \ "- output character";
  • \? - output symbol?.

For example, as a result of a function call:

  printf ("\ tComputer \ n% d \ n", i);
 

first, a horizontal tab (\ t) is performed, i.e. the cursor moves from the edge of the screen, then the word Computer is displayed, then the cursor moves to the beginning of the next line (\ n), then the integer i is in the format% d (decimal integer), and finally the cursor moves to the beginning newline (\ n).

You can print a string of characters like this:

  printf ("This is a character string");
 

Formatted data entry

The scanf () function (the prototype is contained in the stdio.h file) provides formatted input. It can be written in the following formal form:

  scanf ("control string", argument_1, argument_2, ...);
 

The scanf () arguments must be pointers to the corresponding values. To do this, a & is written in front of the variable name. The assignment of pointers will be discussed further.

The control string contains conversion specifications and is used to set the number and types of arguments. It may include:

  • spaces, tabs and newlines (they are all ignored);
  • conversion specifications consisting of the% sign, possibly, the * (prohibition of assignment), possibly a number that specifies the maximum size of the field, and the conversion character itself;
  • ordinary characters, except% (it is believed that they must coincide with successive unknown characters in the input stream).

Consider the conversion characters of the scanf () function (indicated after the% symbol):

with
    • - a single character is expected at the entrance;

d
    • or
i
    • - a decimal integer is expected at the input and the argument is a pointer to a variable of type int;

D
    • or
l
    • - a decimal integer is expected at the input and the argument is a pointer to a variable of type long;

e
    • or
E
    • - a floating-point real number is expected at the input;

f
    • - a floating-point real number is expected at the input;

g
    • or
G
    • - a floating-point real number is expected at the input;

about
    • - an octal integer is expected at the input and the argument is a pointer to a variable of type int;

ABOUT
    • - an octal integer is expected at the input and the argument is a pointer to a variable of the long type;

s
    • - At the entrance is expected to appear a string of characters

x
    • - a hexadecimal integer is expected at the input and the argument is a pointer to a variable of type int;

X
    • - a hexadecimal integer is expected at the input and the argument is a pointer to a variable of the long type;

R
    • - at the entrance, the pointer is expected to appear in the form of a hexadecimal number;

n
    • - used in formatting operations. The argument corresponding to this specification symbol must be a pointer to an integer. It returns the position number (after input), in which the specification% n;

u
    • - an unsigned integer is expected at the input and the argument is a pointer to an unsigned int variable;

U
    • - an unsigned integer is expected at the input and the argument is a pointer to an unsigned long variable;

[]
    • - scans the input string to get characters.

The following modifiers may be written before some characters of the transformation:

F
    • - changes the default pointer to the far type pointer;

N
    • - changes the default pointer to the pointer of the near type;

h
    • - converts the argument to the type short int (can be written before the characters d, i, o, u, x);

l
    • - converts the argument to the type long int (can be written before the characters d, i, o, u, x);

L
  • - converts the argument to the type of long double (can be written before the characters e, f, g).

You can enter an integer (int a;), a character (char b;) and a real number (float t;) as follows:

  scanf ("% d", & a);
     scanf ("% c", & b);
     scanf ("% d% c% f", & a, & b, & t); 

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithmization and programming. Structural programming. C language

Terms: Algorithmization and programming. Structural programming. C language