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.
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:
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.
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:
We give them a brief description:
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:
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 |
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:
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.
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:
withOptional parameters in the conversion specification:
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:
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");
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:
Consider the conversion characters of the scanf () function (indicated after the% symbol):
withThe following modifiers may be written before some characters of the transformation:
FYou 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
Algorithmization and programming. Structural programming. C language
Terms: Algorithmization and programming. Structural programming. C language