Chapter 2 overview of C language
github: https://github.com/xiaochao200/C-Primer-Plus/tree/main/Day1
2.1 simple C program example
- Procedure listing 2.1 first c
#include <stdio.h> int main(void) { int num; num = 1; printf("I'm a simple "); printf("computer\n"); printf("My favorite number is %d because it is first.\n", num); getchar(); // Prevent program output from flashing return 0; }
I'm a simple computer My favorite number is 1 because it is first.
2.2 example interpretation
2.2.1 composition of C program
A typical C program consists of several important parts
-
#include pretreatment
-
The int main(void) main() function is always the first function to be called, even if other functions are written earlier
-
function name() other functions
-
The function body of a statement function consists of statements and declarations. There are six kinds of statements in C language, and most of them need to end with a semicolon
① Label statement
② Compound statement
③ Expression statements: assignment expressions
④ Select statement
⑤ Iterative statement
⑥ Jump statement: return statement
2.2.2 procedure interpretation
1. #include pretreatment
#include <stdio.h>
Equivalent to stdio All contents in the H file are entered at the location of the line, similar to Python's import third-party library, and stdio H file includes standard input and output. For example, the printf() function is defined in it. Because some programs don't need input and output, stdio H built in
2. main() function
int main(void)
C programs must start from the main() function (even if other functions are written in front). int is the return type of the function, that is, integer; Main is the function name. The names of other functions can be changed, but the main function name cannot be changed; () is used to identify that it is a function. There can be parameters in parentheses. If there is no parameter, it is void (or empty).
3. Notes
- Single line note:
// This is a single line comment
- Multiline comment:
/* This is Multiline notes */
4. {} - function body and block
{} marks the beginning and end of the function body. It can also be used to combine multiple statements in a function into a unit or block
5. Declaration
int num;
Declare an integer variable named num in the function. Previously, the variable was required to be declared at the top of the block. Now it is not required
-
name
① When naming variables, meaningful variable names or identifiers shall be used. If the meaning cannot be clearly expressed, comments can be added.
② You can name with letters, numbers and underscores, but you can't start with numbers. It's best not to start with an underscore (avoid duplicate names with identifiers in the Library).
③ Avoid using keywords and reserved identifiers
6. Assignment
num = 1;
The assignment expression statement in the six basic statements, namely "assign value 1 to variable num"
7. printf() function
printf("I'm a simple "); printf("computer\n"); printf("My favorite number is %d because it is first.\n", num);
I'm a simple computer My favorite number is 1 because it is first.
-
printf() is a standard function that prints the contents of double quotes within parentheses
-
The second printf is not printed, escape character:
\ n: line break, i.e. Enter \ t: Tab, i.e. Tab \ b: Backspace, i.e. Backspace
-
% d in third printf is not printed, placeholder:
- %Remind the program to print a variable there. d means to print the variable as a decimal integer
- %d is replaced by the following variable num
- f in printf() represents a formatted print function
8. return statement
return 0;
Jump statements among the six basic statements. Because int in the function declaration int main(void) indicates that the return value of the main() function should be an integer, the missing return statement will return 0 when the program runs to the outermost closing curly bracket}. It's best not to miss
2.3 structure of simple program
The program consists of one or more functions and must have the main() function.
A function consists of a function header and a function body.
- Function header: including function name, return type and parameter information
- Function body: enclosed by {} and composed of a series of statements and declarations
#include <stdio.h> int main(void) { //Statement + [declaration] return 0; } //(most statements end with a semicolon.)
2.4 improve program readability
① Meaningful function names ② comments ③ separate into conceptual parts with blank lines ④ each statement occupies one line
2.5 further use of C
- Procedure list 2.2 fathm_ft.c
// fathm_ft.c -- convert 2 tones to inches #include <stdio.h> int main(void) { int feet, fathoms; fathoms = 2; feet = 6 * fathoms; printf("There are %d feet in %d fathoms!\n", feet, fathoms); printf("Yes,I said %d feet!\n", 6 * fathoms); getchar(); return 0; }
There are 12 feet in 2 fathoms! Yes,I said 12 feet!
2.5.1 multiple statements
Two variables are declared in a declaration, separated by commas (feet and fatoms)
int feet, fathoms; // Equivalent to int feet; int fathoms; // You can't say that int feet, int fathoms;
2.5.2 multiple printing
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
There are 12 feet in 2 fathoms!
Two% d, followed by two variables separated by commas
2.6 multiple functions
Write your own new functions
- Procedure listing 2.3 two_func.c
// two_func.c -- a file contains two functions #include <stdio.h> // Function declaration void butler(void); int main(void) { printf("I will summon the butler function.\n"); butler(); printf("Yes. Bring me some tea and writeable DVDs.\n"); getchar(); return 0; } // Function definition void butler(void) { printf("You rang, sir?\n"); }
I will summon the butler function. You rang, sir? Yes. Bring me some tea and writeable DVDs.
1. The bulter() function appears three times
- The first time: the function declaration (function prototype) tells the compiler to use the function in the program, otherwise it cannot run
- The second time: it appears in main() as a function call
- 3rd time: function definition
2. Function execution order
- No matter whether the filter () function is defined before or after the main() function, the C program starts from the main() function
- The Convention is to define the main() function in the front and other functions in the back (it needs to be declared in front of the main() function)
- When the bulter() function is executed depends on where it is called in the main() function, regardless of where it is defined
2.7 commissioning procedure
Find the error in listing 2.4
Procedure listing 2.4 nogood c
// nogood.c -- program with error #include <stdio.h> int main(void) ( int n, int n2, int n3; /* The program has several errors n = 5; n2 = n * n; n3 = n2 * n2; printf("n = %d, n squared = %d, n cubed = %d\n",n, n2, n3) return 0; )
2.7.1 syntax error
Grammatical errors refer to the mistakes made by not following the rules of C language
- The body of the main() function replaces curly braces with parentheses
- Variable declaration error
int n, int n2, int n3; // Change to int n, n2, n3; // perhaps int n; int n2; int n3;
- * / is omitted from the end of the comment in main(), or / / is used instead/*
- Semicolon missing at the end of printf() statement
2.7.2 semantic error
Semantic error refers to correct grammar and wrong meaning, that is, logical error
Suppose the above code wants to realize that n2 represents the square of N and n3 represents the cube of n
n3 = n2 * n2;
The actual code n3 represents the fourth power of n
2.7.3 program status
1) Syntax errors can be modified according to the error prompt of the compiler
2) How to track programs and find semantic errors?
- For those with too many cycles, you can track part of the cycle
- Insert extra printf() at keys
- The debugger can run the program step by step and check the value of the program variable
2.8 keywords and reserved identifiers
Keywords are as shown in the following table. Reserved identifiers include identifiers starting with underscore characters and standard library function names, such as printf()
auto | extern | short | while |
---|---|---|---|
break | float | signed | _Alignas |
case | for | sizeof | _Alignof |
char | goto | static | _Atomic |
const | if | struct | _Bool |
continue | inline | switch | _Complex |
default | int | typedef | _Generic |
do | long | union | _Imaginary |
double | register | unsigned | _Noreturn |
else | restrict | void | _Static_assert |
enum | return | volatile | _Thread_local |