Overall, it is essential to understand:
1) There are three types of program structures: sequential structure, selection structure (branching structure), and loop structure.
2) Programs should always be read starting from the main() entry point, reading sequentially from the top down (performing loops when encountering loops, making selections when encountering selections). There is only one main function.
3) Data in computers is stored in binary format. The location where data is stored is called its address.
4) A bit represents a single binary digit, either 0 or 1. A byte refers to 8 bits.
Commonly tested concepts:
1) The compilation pre-processing is not part of the C language, does not take up runtime, and should not have a semicolon. Programs written in C are called source programs, stored in text files in ASCII values.
2) The correct way to define PI is define PI 3.1415926; this format is incorrect; a semicolon must not appear.
3) Each C language program has exactly one main function.
4) Functions cannot be defined within other functions.
5) An algorithm can have no input but must have an output.
6) The break statement can be used in loop structures and switch statements.
7) The comma operator has the lowest precedence, while the assignment operator has the second-lowest precedence.
Chapter 1: Basic Knowledge of C Language
Section 1: Understanding the Basics of C Language
1) Programs written in C are called source programs, also known as compilation units.
2) The writing format of C language is flexible; multiple statements can be written on one line or across multiple lines.
3) A C language program has exactly one main function, which is the starting point of the program.
Section 2: Familiarity with VC++
1) VC is software used to run C language programs.
2) After writing a C language program, it must first be compiled, then linked, and finally run. (.c —> .obj —> .exe) Note that .c and .obj files cannot be executed; only .exe files can be run. (Frequently tested!)
Section 3: Identifiers
1) Identifiers (must-know content):
Valid identifiers must consist of letters, digits, and underscores. Any other elements are incorrect.
Also, the first character must be a letter or an underscore. If the first character is a digit, it is incorrect.
2) Identifiers are classified into keywords, predefined identifiers, and user-defined identifiers.
Keywords cannot be used as user-defined identifiers. main, define, scanf, printf are not keywords. A confusing aspect is that If can be used as a user-defined identifier because the first letter is capitalized, so it is not a keyword.
Predefined identifiers: memorize define, scanf, printf, include. Remember that predefined identifiers can be used as user-defined identifiers.
User-defined identifiers: Generally tested every year; see textbook exercises for details.
Section 4: Base Conversion
Convert decimal to binary, octal, and hexadecimal.
Convert binary, octal, and hexadecimal to decimal.
Section 5: Integers and Real Numbers
1) C language only has octal, decimal, and hexadecimal, not binary. However, all bases must be converted to binary for processing during execution. (Tested twice)
a) In C language, octal numbers must start with 0. The value 018 is invalid; there is no 8 in octal, and it carries over at 8.
b) In C language, hexadecimal numbers must start with 0x.
2) Valid forms of decimals: In C language, if there is a zero on either side of the decimal point, it can be omitted.
1.0 in C language can be written as 1.
0.1 in C language can be written as .1.
3) Valid forms of floating-point data:
a) 2.333e-1 is valid, and the data represents 2.333×10-1.
b) Exam mnemonic: There must be a number before and after ‘e’, and the number after ‘e’ must be an integer. Refer to examples in the textbook.
4) Generally, an integer is 4 bytes, a character is 1 byte, and a double is generally 8 bytes:
long int x; indicates x is a long integer.
unsigned int x; indicates x is an unsigned integer.
Sections 6 and 7: Arithmetic Expressions and Assignment Expressions
Core: An expression must have a value!
1) Arithmetic expressions: +, -, *, /, %
It is essential to note that if both sides of ‘/’ are integers, the result is an integer. The result of 3/2 is 1.
If one side is a decimal, the result will be a decimal. The result of 3/2.0 is 0.5.
It is crucial to remember that ‘%’ is the remainder; it is the easiest to confuse with the division operator. The ‘%’ operator requires both sides to be integers; otherwise, it is incorrect.
2) Assignment expressions: The value of the expression is the leftmost value. a=b=5; this expression equals 5, and constants cannot be assigned.
1) int x=y=10; wrong, cannot assign continuously during definition.
2) int x,y;
x=y=10; correct, can assign continuously after definition.
3) The left side of an assignment can only be one variable.
4) int x=7.7; correct, x equals 7.
5) float y=7; correct, y equals 7.0.
3) Compound assignment expressions:
int a=2;
a*=2+3; after execution, the value of a is 12.
It is important to remember to place parentheses around (2+3) before performing the operation.
4) Increment expressions:
Increment and decrement expressions: Suppose a=5, ++a (is 6), a++ (is 5);
The mechanism of execution: ++a first adds 1 to the variable’s value, then stores the result in variable a, so the value of ++a is 6, while a++ uses the value of a as 5, then adds 1 to a, making it 6.
After executing both ++a and a++, if a is used in subsequent programs, it will have the value 6.
Exam mnemonic: ++ before means add and then use, ++ after means use and then add.
5) Comma expressions:
They have the lowest precedence. The value of the expression is the value of the rightmost expression of the comma.
The value of (2,3,4) is 4.
z=(2,3,4) (the whole is an assignment expression), at this point z’s value is 4. (This can be a bit tricky!)
z=2,3,4 (the whole is a comma expression), at this point z’s value is 2.
Supplement:
1) Empty statements cannot be executed randomly, as they can lead to logical errors.
2) Comments have been a focus of exams in recent years; comments are not part of C language, do not take up runtime, and do not require a semicolon. They cannot be nested!
3) Type casting:
It must be (int)a, not int(a), note that there must be parentheses for the type.
Note the difference between (int)(a+b) and (int)a+b. The former casts a+b, while the latter casts a before adding b.
4) Three cases of truncating decimals:
1) int a=1.6;
2) (int)a;
3) 1/2; 3/2;
Section 8: Characters
1) Valid forms of character data:
‘1’ is a character occupying one byte, while “1” is a string occupying two bytes (including an end character).
‘0’ has an ASCII value of 48, ‘a’ has an ASCII value of 97, and ‘A’ has an ASCII value of 65.
Common exam errors for single characters: ’65’, “1”.
Characters can be used in arithmetic operations; remember: ‘0’-0=48.
To convert between uppercase and lowercase letters: ‘A’+32=’a’, the difference is generally 32.
2) Escape characters:
Escape characters are divided into general escape characters, octal escape characters, and hexadecimal escape characters.
General escape characters: memorize \0, \n, \\, \”, \\.
Octal escape characters: ‘\141’ is valid; leading zeros cannot be written.
Hexadecimal escape characters: ‘\x6d’ is valid; leading zeros cannot be written, and ‘x’ must be lowercase.
3) Character types and integers are closely related: both have significant similarities.
char a = 65;
printf(“%c”, a); will output: a
printf(“%d”, a); will output: 65
Chapter 9: Bitwise Operations
1) Bitwise operation questions: there will be one or two exam questions.
The general method of processing: almost all bitwise operation questions must follow this process (first convert decimal to binary, then back to decimal).
Example 1: char a = 6, b;
b = a<<2; to solve this type of question, first convert a’s decimal 6 to binary and then perform the bitwise operation.
Example 2: Always remember the XOR operator “^”. 0 XOR 1 equals 1.
0 XOR 0 equals 0. Two females cannot produce offspring.
Exam mnemonic: one male (1) and one female (0) can produce a child (1).
Example 3: When no data is discarded, << left shift by one means multiply by 2; >> right shift by one means divide by 2.
Chapter 2
Section 1: Data Output (I) (II)
1) When using printf and scanf functions, always include #include “stdio.h” at the beginning.
2) printf can have one or two parameters. (This has been tested in multiple-choice questions)
3) printf(” First part “, second part); presents the second part’s variable, expression, constant in the format of the first part!
4) printf(“a=%d, b=%d”, 12, 34) Exam focus!
It is crucial to remember that 12 and 34 are displayed in the terminal (the black screen) in the format of the first part. The exam core is: exactly the same. On the black screen, it displays as a=12, b=34.
printf(“a=%d,\n b=%d”, 12, 34) will output: a=12,
b=34.
5) int x=017; be sure to understand why this result occurs! The process is important.
printf(“%d”, x); 15
printf(“%o”, x); 17
printf(“%#o”, x); 017
printf(“%x”, x); 11
printf(“%#x”, x); 0x11
6) int x=12, y=34; pay attention to this type of question.
char z=’a’;
printf(“%d “, x, y); one format specifier, two output variables; the second variable y does not output.
printf(“%c”, z); the result is: 12a
7) Must memorize:
Format Specifier |
Content Represented |
Format Specifier |
Content Represented |
%d |
Integerint |
%c |
Characterchar |
%ld |
Long Integerlong int |
%s |
String |
%f |
Floating Pointfloat |
%o |
Octal |
%lf |
Double |
%#o |
Octal with Leading Zero |
%% |
Output a Percent Sign |
%x |
Hexadecimal |
] |
%#x |
Hexadecimal with Leading Zero |
Example explanation:
printf(“-“, 123); the second part has three digits, exceeding the specified two digits, outputs 123 as is.
printf(“]”, 123); the second part has three digits, less than the specified five digits, left-pads with two spaces: 123.
printf(“f”, 1.25); requires six digits for the decimal; if there are fewer than six, pads with zeros. The result is 1.250000.
printf(“%5.3f”, 125); three decimal places, five total digits; the result is 1.250 (the decimal point counts as one digit).
printf(“%3.1f”, 1.25); one decimal place, three total digits; the result is 1.3 (rounding applies).
Section 3: Data Input
1) scanf(“a=%d, b=%d”, &a, &b) Exam super focus!
It is crucial to remember that data must be input in the format of the first part in the terminal. The exam core is: exactly the same.
In the black screen, inputting a=12, b=34 will correctly assign 12 and 34 to a and b. Any slight difference will not work.
2) scanf(“%d, %d”, x, y); this format is absolutely wrong; the second part of scanf must be an address!
scanf(“%d, %d”, &x, &y); be sure to write it this way!
3) Pay special attention to pointers in scanf examination.
For example: int x=2; int *p=&x;
scanf(“%d”, x); wrong; scanf(“%d”, p); correct.
scanf(“%d”, &p); wrong; scanf(“%d”, *p); wrong.
4) Specify input length (exam focus)
Terminal input: 1234567
scanf(“-M%d”, &x, &y, &z); x will be 12, y will be 3456, z will be 7.
Terminal input: 1 234567; due to the space between 1 and 2, only 1 digit goes to x.
scanf(“-M%d”, &x, &y, &z); x will be 1, y will be 2345, z will be 67.
5) Characters and integers are closely related:
int x=97;
printf(“%d”, x); the result is 97.
printf(“%c”, x); the result is a.
6) Input differences between characters and integers (exam super focus)
scanf(“%d”, &x); when inputting 1, this represents the integer 1.
scanf(“%c”, &x); when inputting 1, this represents the character ‘1’, which has an ASCII value of 48.
Supplementary notes:
1) Format examination of scanf function:
Note that the second part of the function must be &a, not a;
scanf(“%d%d%*d%d”, &a, &b, &c); skips the third input data.
2) Examination of putchar and getchar functions:
char a = getchar() has no parameters; it gets a character input from the keyboard into variable a.
putchar(‘y’) outputs the character y to the screen.
3) How to swap the values of two variables x and y (must memorize)
Cannot do x=y, y=x; must use a temporary variable t=x; x=y; y=t.
4) How to retain three decimal places, rounding the fourth digit (must memorize)
y=(int)(x*100+0.5)/100.0; this retains two digits, rounding the third digit.
y=(int)(x*1000+0.5)/1000.0; this retains three digits, rounding the fourth digit.
y=(int)(x*10000+0.5)/10000.0; this retains four digits, rounding the fifth digit.
This has broader significance; note that x=(int)x removes the decimal part.
Chapter 3
Particularly noteworthy: In C language, non-zero represents logical true, while 0 represents logical false.
C language has constructed types but no logical types.
Relational operators: note the writing of <=, the difference between == and =! (exam focus)
If only handles the next statement; to handle multiple, use braces!
1) Relational expressions:
a) The value of an expression can only be 1 (true) or 0 (false).
For example, 9>8 is true, so the value of the expression 9>8 is 1.
For example, 7<6 is false, so the value of the expression 7<6 is 0.
b) The most common mistake in exams: int x=1,y=0,z=2;
x
c) The difference between assignment and equality! Always remember “=” is assignment, while “==” is equality. Although many can recite this, I still urge everyone to remember it well; otherwise, if you make a mistake, I will strongly despise you!
2) Logical expressions:
Core: The value of an expression can only be 1 (true) or 0 (false).
a) There are three logical operators: &&, ||, !.
b) Note the precedence: ! > && > ||.
c) Be aware of short-circuit behavior; this is often tested. Refer to examples in the textbook, and make sure you can solve example 1 and example 2.
d) The method to express that x is less than 0 and greater than 10.
0
3) If statements:
a) else matches the closest if statement without else.
b) The swapping program is written as: t=x; x=y; y=t;
c) if(a
if(a
d) Standalone if statement: if(a
Standard if statement: if(a
Nested if statement: if(ac)printf(“ok!”);
Multi-choice if statement: if(a==t)printf(“a”); else if(b==t)printf(“b”); else if(c==t)printf(“c”); else printf(“d”);
Through exercises, become familiar with the above four types of if statements!
Classic exam question: Combine the above four types of if statement questions; if answered incorrectly, please take responsibility yourself! Ready, set!
int a=1, b=0;
if(!a)b++;
else if(a==0) if(a)b+=2; else b+=3; what is the value of b?
If you did not understand the question, do not jump to conclusions; those who cannot understand but can do it still have a reason to live.
The correct answer is b equals 3.
int a=1, b=0;
if(!a) b++; is false and does not execute.
else if(a==0) is also false and does not execute.
if(a) b+=2; belongs to the nested if statement of else if, which does not execute.
else b+=3; if none of the if-else-if statements are correct, the else statement will execute!
4) Conditional expressions:
expression1 ? expression2 : expression3
a) Exam mnemonic: true first, false second.
b) Note that when the value of expression1 is non-zero, expression2’s value is used as the entire result; when expression1’s value is zero, expression3’s value is used as the entire result.
c) int a=1, b=2, c=3, d=4, e=5;
k=a>b?c:d>e?d:e; what is the value of k? The answer is d.
5) Switch statements:
a) Be sure to understand the execution flow! The detailed process was discussed in class; please ensure you understand it!
b) Note the difference between having and not having break; in the examples in the book, if there is no break, as long as one case matches, all subsequent cases will execute. If there is a break, it will jump out of the switch statement directly. Break in C language means to break, to cut off.
c) Switch can only be used with break, not with continue.
d) switch(x) x: is an integer constant, character constant, or enumeration type data.
{case 1: … cannot be a variable.
case 2: …
}
e) Switch is a must-know question type, so everyone must complete the switch exercises in the textbook.
Editor: A Q
Reviewer: Cong Ge
Image and text from the internet
THE END
For more content, please scan the QR code below to follow us