Chapter 1: C language Introduction
The development process of C language C language is in the early 1970s. In 1978, the US Telephone Telegraph (AT & T) Bel laboratory officially published C language. At the same time, the famous "THE C Programming Language" book is combined by B.W.kernighan and D.M.ritchit. Usually referred to as "K & R", it is also known as "K & R" standard. However, there is no complete standard C language in "K & R", and later, a C language standard has been developed by the National Standard Society, published in 1983. Usually called ANSI C.
Contemporary best programming language
Early C language is mainly used for UNIX systems. Due to the powerful function of C language and the advantages of all aspects, it has gradually been aware of people. When it is in the 1980s, C begins to enter other operating systems and has been widely used in all kinds of large, medium, small and microcomputers. Be one of the best programming languages for contemporary.
C language characteristics
C language is a structured language. It is clear, easy to organize programs in modularity, easy to debug and maintain. C language performance and handling ability. It not only has rich operators and data types, which makes it possible to achieve various complex data structures. It can also access the physical address of the memory and perform the operation of the bit (BIT) level. Since the C language implements the programming operation of the hardware, the C language integrates in the high-level language and low language functions. It can be used for the development of system software, and is also suitable for application software development. In addition, the C language also has the characteristics of high efficiency and strong portability. Therefore, it is widely transplanted to various types of computers, thereby forming a variety of C language.
C language version
The most popular C language has the following: · Microsoft C or MS C · Borland Turbo C or Turbo C • AT & T C these C language version not only implements ANSI C standard, but also on this basis, some expansion Make more convenient, perfect.
Object-oriented programming language
On the basis of C, C is launched by Bjarne Strou-Strup in Bel Laboratory 1983. C further expands and improves C language to become an object-oriented programming language. C currently popular latest version is Borland C 4.5, Symantec C 6.1, and Microsoft Visual C 2.0. C proposes some more in-depth concepts that it supports these object-oriented concepts easily mapped the problem space to the program space, providing programmers with a different way of thinking and programming methods with traditional structural programming. Therefore, the complexity of the entire language is also increased, and it has a certain difficulty.
C and C
However, C is the foundation of C , C language and C language are compatible in many ways. Therefore, master the C language, then learn C to learn the object-oriented language with a familiar syntax, which achieves a half-powered purpose.
C Source Procedure Structure Characteristics
To illustrate the characteristics of the C language source program structure, look at the following procedures. These proceedings are simply difficult to express the characteristics of C language source programs in constituent structures. Although the relevant content has not been described, it can be learned from these examples to form a basic part of a C source program and a writing format. Main () {Printf ("Billows C language tutorial, Hello! / N");} main is a function name of the main function, indicating that this is a master. Every C source program must have, and can only have a master function (main function). Function call statement, the function of the Printf function is to send the content to which you want to output to the display. The Printf function is a standard function defined by the system that can be called directly in the program. # include # include
Main () {Double X, S; Printf ("INPUT NUMBER: / N"); scanf ("% lf", & x); s = sin (x); printf ("SINE OF% LF IS% LF / N" , x, s);} Each row comment Include is called a file containing the command extension called .H file is also known as the header file or the header definition two real variables, to use the display prompt information from the keyboard from the keyboard to get a real number from the keyboard using the display prompt information from the back program. X find the sinusoid of X and assign it to the variable S display program operation result main function end program's function is from the keyboard to enter a number X, ask X's sine, then output the result. Two lines before main () are called preprocessing commands (see later). There are several other kinds of pretreatment commands. The include is called a file containing commands, which contains a file specified in the spare bracket "or quotation" to this program to become part of this program. The included file is usually provided by the system, its extension is .h. Therefore, it is also called a header file or header. The core of the C language includes a function prototype of each standard library function. Therefore, when you call a library function in the program, you must include the header file of the function prototype. In this example, three library functions are used: Enter a function scanf, sinusoidal function SIN, and the output function printf. The SIN function is a mathematical function, its header file is a Math.h file, so it contains Math.h with the include command before the program's main function. Scanf and printf are standard input and output functions, and its header file is stdio.h, and the stdio.h file is included with the include command before the main function.
It should be noted that the C language specifies that the two functions of Scanf and PrintF can save the included commands for their header files. So in this case, you can also delete the second row of command #include
. Similarly, the Printf function is used in Example 1.1, which also omits the command.
In the main function body in the example, it is divided into two parts, part of the description portion, and the other part of the execution section. The description refers to the type of variable. No variables are used in the example, so there is no explanation. C language regulations, all used variables in the source program must first explain, after use, otherwise it will be wrong. This is a feature of the compilation advanced programming language, which is different from the interpreted Basic language. The description part is an important part of the C source program structure. In this example, two variables x, S are used to indicate the input argument and the SIN function value. Since the SIN function requires that these two amount must be a double precision floating point, these two variables are used with type specifier. The four behaviors after the description are executed or called the execution statement section to complete the functionality of the program. The first line of the execution section is the output statement, call the printf function to output a prompt string on the display, and the operator inputs the value of the variable x. The second behavior inputs a statement, calls the SCANF function, accepts the number on the keyboard and stores the variable x. The third line is to call the SIN function and send the function value to the variable S. The fourth line is the value of the value of the variable S using the PrintF function, that is, the sine sine value of X. The program ends. Printf ("Input Number: / N"); scanf ("% LF", 'C10F10 & X); s = sin (x); Printf ("SINE OF% LF IS% LF / N",' C10F10X, S); operation When this program is first given on the display screen, it is done by the first line of the execution portion. The user typed a certain number on the keyboard under the prompt, such as 5, press the Enter key, and then give the calculation result on the screen.
The input and output functions are used in the first two examples to input and output functions Scanf and Printf, which we must introduce in detail in Chapter 3. Here we briefly introduce their format so as to use. The two functions of Scanf and Printf are called format input functions and format output functions, respectively. Its meaning is to enter the output value in the specified format. Therefore, the parameter tables in parentheses are composed of two parts: "format control string", the parameter table format control string is a string, which must be enclosed in double quotes, which represents the input and output data. Types of. Various types of format representations can be referred to in Chapter 3. A non-format control character can also appear in the format control string in the printf function, and the original text is placed on the display screen. The input or output is given in the parameter table. When there are multiple quantities, use comma intervals. For example: Printf ("SINE OF% LF IS% LF / N", X, S); where% LF is a format character, indicating the double precision floating point number. It is in the format string, and the two variables of X and S are correspondingly in the format string. The rest of the character is an unfolded character, which outputs Int Max (Int A, INT B) on the screen, main () {INT X, Y, Z; Printf ("INPUT TWO NUMBERS: / N"); scanf ("% D% D ", & x, & y); z = max (x, y); Printf (" maxmum =% d ", z);} int MAX (int A, int b) {if (A> b) Return A ELSE RETURN B;} The function of this function is to enter two integers and output the large number. / * Function Description * // * Primitive * // * Variable Description * // * Input X, Y value * // * Call MAX function * / / * output * // * Define MAX function * // * Put the result Returns the function of the program in the Motor * / The above example is used by the user to enter two integers. After the program is executed, the large number is output. This program consists of two functions, the main function and the MAX function. The function is a parallel relationship. Other functions can be called from the main function. The function of the MAX function is compared to two, then returns the larger number to the main function. The MAX function is a user-defined function. Therefore, in the main function (the third line of the program) is given. It can be seen that in the description section of the program, it can not only have variable description, but also have a function description. The details of the function will be introduced in Chapter 5. The contents of the comment are not performed with / * and * / from each line of the program. The execution process of the program in the above example is first displayed on the screen. Please enter two numbers. Enter the number of SCANF function statements to send the two numbers x, y, and then call the MAX function, and Pass the parameters A, B of the MAX function to the value of X, Y. In the MAX function, the size of A, B is compared, and the larger returns the larger the variable z of the main function, and the value of z is output on the screen.
C Source Procedure Structure Characteristics
1. A C language source program can consist of one or more source files.
2. Each source file can be composed of one or more functions.
3. One source program is composed of how many files, there is only one main function, ie the main function.
4. The source program can have a pre-processing command (the include command is only one), and the preprocessing commands should usually be placed at the forefront of the source file or source program.
5. Each statement must end with a semicolon. However, the pretreatment command, the function head and the larch "}" cannot be added.
6. The identifier, must add at least one space between the keywords to indicate interval. If there is an obvious interval, it can no longer add a spark.
Rules that should be followed when writing programs
From writing, it is easy to read, understand, and maintenance. The following rules should be followed when writing procedures: 1. One instruction or one statement occupies a line.
2. The part of the {is usually expressed in a hierarchy of the program. {} Is generally aligned with the first letter of the structural statement and occupies a row separately. 3. Low level of statement or instructions can be written after a high level of statement or indication. In order to look more clear, increase the readability of the program. These rules should be stripped when programming to develop a good programming style.
C language character set
Characters are the most basic elements that make up the language. C language character set consists of letters, numbers, spaces, punctures, and special characters. In the character constant, the string constant, and the comment can also use Chinese characters or other representative graphics symbols. 1. Letters lowercase letters A ~ Z total 26, uppercase letters A ~ Z total 26
2. Number 0 ~ 9 total 10
3. The blank character, the tab, the wrap, and the like are collectively referred to as blank characters. The blank character works only in character constants and string constants. When there is any other place, only the intervals, the compiler is ignored. Therefore, the use of blank characters in the program does not affect the compilation of the program, but the use of blank characters in the program will increase the clarity and readability of the program.
4. Points and special characters
C language vocabulary
Words used in C language are six categories: identifiers, keywords, operators, separators, constants, annotation, and more.
1. The identifier is collectively referred to as an identifier used in the program name, function name, reference numeral, and the like. The function name of the library function is defined by the user from the system definition. C. According to the identifier, the identifier can only be a string composed of letters (A ~ Z, A ~ Z), numbers (0 ~ 9), underscore (), and its first character must be a letter or underscore.
The following identifier is legal:
A, X, 3X, BOOK 1, SUM5
The following identifier is illegal: 3s appears in the digital start S * T * -3x Execute illegal characters in the beginning of BOWY-1 in the use of the identifier, you must pay attention to the following: (1) Standard C does not limit the length of the identifier, but it is subject to a variety of C language compilation system restrictions, and is also limited by specific machines. For example, the first eight bits of the identifier in a version C are valid, and when the two identifiers are the same, the same identifier is considered the same identifier. (2) In the identifier, the case is different. For example, Book and Book are two different identifiers. (3) Although the identifier can be arbitrarily defined by the programmer, the identifier is a symbol used to identify a certain amount. Therefore, naming should try to have the corresponding meaning in order to read understanding, do "as the names". 2. Keyword keyword is a string that is specified by the C language and is often referred to as a reserved word. User-defined identifiers should not be the same as the keywords. The keyword of the C language is divided into the following categories: (1) Type specifese is used to define, describe variables, functions, or other data structures. As used in the previous example INT, Double, etc. (2) statement definitions are used to represent the functionality of a statement. The IF ELSE used in Example 1.3 is the statement definition of the conditional statement. (3) The preprocessing command word is used to represent a preprocessing command. Include used in the previous examples. 3. Operator C language contains a fairly rich operator. The operator consists of variables, functions together, indicating various arithmetic functions. The operator consists of one or more characters. 4. Separator has two types of comma and spaces employed in C language. Comma is mainly used in type descriptions and function parameters tables, separating each variable. Spaces are used between the words of statements, interval. There must be more than one space between the keyword, the identifier, otherwise a syntax error will occur, such as putting int A; written into an inta; C compiler, the INTA is treated as an identifier, and the result is inevitable error . 5. The constants used in constant C language can be divided into digital constants, character constants, string constants, symbol constants, escape characters, and more. In the second chapter, it will be specifically given. 6. The annotation of the comment C language is the string that begins with "/ *" and ends with "* /". An comment between "/ *" and "* /". When compiling, the comment is not processed. Note Any location in the program can occur. Note The meaning used to prompt or interpret the program to the user. In the debugger, the statement that is not used is also available in the comment, so that the translation skip does not work, and then remove the note after the debug.