C language program composition


Most applications on the Linux operating system are developed based on the C language (in fact, the Linux operating system is also developed in the C language). This blog will briefly introduce the C language development programs commonly used on the Linux platform.

The structure of a C program

1. Function

  • There must be one and only one main function main(), and the name of the main function is main. Where is main() in the program, C programs always start executing from the main() function.
  • It can be a standard function predefined by the system, such as scanf function, printf function, etc.
  • Most functions are defined by programmers according to the needs of practical problems, and there is a parallel relationship between functions. Based on this, C language is also known as functional language.
  • A function consists of a function header and a function body.
2. Statement
  • A statement is the basic unit that composes a program, and the realization of a function is completed by several statements.
  • Statements are identified by several keywords, such as if-else statements, do-while statements, etc.
  • The input/output of the C language is performed by the scanf function/printf function
3. Other
  • Preprocessing commands. C programs often contain commands starting with "#", which are preprocessing commands, such as #include "stdafx.h" in Figure 1-2.
  • comments. The content starting with /* and ending with */ is the commented part of the program, and the commented statement will not be compiled and executed by the program.

Figure 1-2

Second, C language identifiers, keywords, operators and separators

  1. identifier. There are two types of predefined identifiers and user-defined identifiers.
(1) System predefined identifiers. Their functions and meanings are pre-defined by the system, for example, printf represents the output function name, and main represents the main function name. Different from keywords, system predefined identifiers allow users to assign new meanings, but this often leads to some misunderstandings, so users are advised not to use these system predefined identifiers for other purposes.
(2) User-defined identifiers. A valid user-defined identifier should satisfy:
  • It can only consist of upper and lower case English letters, Arabic numerals and underscores.
  • Identifiers must begin with a letter or underscore, with distinction between uppercase and lowercase letters. Such as R, r is two variable names.
  • The length of the identifier varies according to different compilers. It should not be too long. Usually, it can adapt to various systems between 6 characters.

Like myster, R_1, _start are legal identifiers.

Like 12d, D$, int are invalid user-defined identifiers.

(3) Keywords. Also known as reserved words, they are provided by the system to represent specific grammatical components. For example, int represents integer data, and float represents single-precision real type. All keywords have a special fixed meaning and cannot be used by others

(4) Operators. Used to perform the specified operation on the operand and get a result value. For example: "+" means addition operation, "=" means assignment operation, "==" means "equal" judgment, and some operators have two characters separated, such as "?:" means conditional operation.

According to the number of operands, it can be divided into unary operators, binary operators and ternary operators, also known as unary operators, binary operators, and ternary operators.

(5) Separator. Used to separate individual lexical tokens or program text, used to indicate the end of one entity and the beginning of another in a program. Common delimiters are:

() {} , : ; blank

These delimiters do not represent any operations, they are only used to construct programs.

Third, the algorithm of the C program

The algorithm should have the following characteristics:

  1. infinity.
  2. certainty.
  3. No input or multiple inputs.
  4. have one or more outputs.
  5. effectiveness.

4. Editing, compiling, linking, assembling and executing C programs

In order for the computer to correctly understand and execute the "source" program written in the high-level programming language, it is necessary to translate the source program written in the high-level language into a "target" program in binary form. Software that can do the above tasks is called a compiler or compiler. A source program written in C language is compiled into a binary target program by a compiler, and then the target program is connected with the system function library and other target programs to form an executable program or command program on a certain operating system platform. As shown in Table 2-1.

Table 2-1 C program writing, compiling and linking files
  source program target program Executable program
content programming language machine language machine language
executable Can not Can not Can
file name suffix .c .obj .exe

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511965&siteId=291194637