Comprehensive Knowledge Points of C Language

Click on Dagu News GroupComprehensive Knowledge Points of C LanguageFollow us~

Thank you for your attention to Dagu News Group! If you haven’t followed us yet, please click the blue text “Dagu News Group” below the title to follow us~ “Able to endure hardship, able to fight, able to be happy”, we are the Dagu reporter team, we speak for ourselves~

C language is a general-purpose programming language that is widely used. We will use C language in our current studies and future work. Let’s browse some important knowledge points about C language with the editor!

Overall Understanding

1) There are three types of program structures: sequential structure, loop structure (three loop structures), and selection structure (if and switch).

2) To read a program, start from the main() entry and read sequentially from the top down (perform loops when encountering loops, perform selections when encountering selections).

3) Data in a computer is stored in binary form. The location where data is stored is its address. Recommended to add WeChat: idaxue8, to share more exam materials!

4) A bit is a position that can be 0 or 1. A byte is a unit of storage, one byte = eight bits.

5) Be sure to remember how to convert binary to decimal.

Commonly Tested Concepts

1. Compilation preprocessing is not part of C language and does not run at runtime. Programs compiled in C language are called source programs, which are stored in text files in ASCII values.

2. There is exactly one main function in each C language program.

3. Functions cannot be defined within functions.

4. An algorithm must have output; it can have no input.

5. Break can be used in loop structures and switch statements.

6. The comma operator has the lowest precedence.

Chapter One – Character Data

(1) Examination of valid user identifiers:

Valid identifiers consist of letters, numbers, and underscores. Any other elements are incorrect.

Moreover, the first character must be a letter or an underscore. If the first character is a number, it is incorrect.

Keywords cannot be used as user identifiers. main, define, scanf, printf are not keywords. The confusing part is that If can be used as a user identifier because the first letter of If is capitalized, so it is not a keyword.

(2) Legal forms of real data:

2.333e-1 is valid, and the data is 2.333×10-1.

Exam mnemonic: There must be a number before and after e; the number after e must be an integer.

(3) Legal forms of character data:

‘1’ is a character occupying one byte, while “1” is a string occupying two bytes (including a terminating character).

‘0’ has an ASCII value of 48, ‘a’ has an ASCII value of 97, and ‘A’ has an ASCII value of 65.

(4) Integer types are generally two bytes, character types are one byte, double precision is generally four bytes:

Generally stated during exams, in a 16-bit compilation system or a 32-bit system. In such cases, don’t worry about it; just solve the problems. It’s sufficient to know that integers are generally two bytes, characters are one byte, and doubles are generally four bytes.

(5) Examination of escape characters:

In the program int a = 0x6d, a hexadecimal number is assigned to variable a; note that 0x must exist.

In the program int a = 06d, it is an octal form.

In escape characters, ‘\x6d’ is valid; 0 cannot be written, and x must be lowercase.

‘\141’ is valid; 0 cannot be written.

‘\108’ is invalid because 8 cannot appear.

(6) The precedence of arithmetic operators:

Operators of the same precedence can be evaluated from left to right or from right to left.

(7) Forced type conversion:

It must be (int)a, not int(a); note that the type must have parentheses.

Note the difference between (int)(a+b) and (int)a+b; the former converts a+b, while the latter converts a first then adds b.

(8) Examination of bitwise operations:

There will be one or two exam questions.

The general approach: almost all bitwise operation questions must be processed by converting decimal to binary and then back to decimal.

Example 1: char a = 6, b;

b = a<<2; for such questions, first convert the decimal 6 of a into binary, then perform the bitwise operation.

Example 2: Be sure to remember;

Example 3: When there is no data being discarded, << left shift by one indicates multiplication by 2; >> right shift by one indicates division by 2.

(9) 018 is an illegal value; octal does not have 8, it carries over at 8.

(10) The % symbol requires integers on both sides. If not integers, it is incorrect.

Chapter Two – Functions

A function is a block of code with a specific function;

(1) Function parameters and return values (illustration):

main()

{

int a = 5, b=6, c;

c = add(a, b);

printf(“%d”, c);

}

Function call

a, b are actual parameters

The entire function returns a value which is

The return value of the add function.

int add(int x, int y)

{

int z;

z = x + y;

return z;

}

Called function

x, y are formal parameters

The function returns an integer value

z is the result obtained after calculation from the add function, which is the return value given to the main program.

The program executes sequentially from top to bottom, when it encounters the add function, it passes the values of a and b to the called function, temporarily interrupting the program to wait for the return value. After obtaining the return value, it continues executing sequentially.

(2) Be sure to pay attention to the transfer of parameters

The difference between passing values and passing addresses between actual and formal parameters (exam focus).

When passing values, changes to the formal parameters do not affect the actual parameters.

When passing addresses, changes to the formal parameters may affect the actual parameters.

(3) Examination of printf function formats:

%d corresponds to integer; %c corresponds to character; %f corresponds to single precision, etc. Width and left alignment modifiers.

%ld corresponds to long int; %lf corresponds to double.

(4) Examination of scanf function formats:

Note that the second part of this function is &a, which is an address, not a;

scanf(“%d%d%*d%d”, &a, &b, &c); skip the input of the third data.

(5) Examination of putchar and getchar functions:

char a = getchar() has no parameters, it gets one character you input from the keyboard and assigns it to variable a.

putchar(‘y’) outputs the character y to the screen.

(6) How to swap the values of two variables x and y (required to memorize)

Cannot use x=y, y=x; must use a temporary variable t=x; x=y; y=t.

(7) How to retain three decimal places, rounding the fourth digit (required to memorize)

This has promotional significance; note that x = (int)x thus removes the decimal part.

(8) Examination of function declarations

Must include: function name, function return type, parameter types.

Not necessarily include: names of formal parameters.

Chapter Three – Expressions

Particularly note: In C language, non-zero represents logical true, while zero represents logical false.

(1) Relational expressions:

The value of an expression can only be 1 (true) or 0 (false).

When a relational expression is true, it yields 1. For example, 9>8 is true, so the value of the expression is 1;

(2) Logical expressions:

Can only be 1 (true) or 0 (false)

a) There are three logical operators: &&, ||, !.

b) !> &&> || has the highest precedence.

c) Note the short-circuit phenomenon. This is often tested in exams.

d) To express that x is greater than 0 and less than 10, 0<x<10 is not permissible (be sure to remember). It must be expressed as (0<x) && (x<10) to indicate greater than 0 and less than 10.

(3) if statement

else is combined with the closest if that does not have else.

(4) Conditional expressions:

expression1 ? expression2 : expression3

Note that when non-zero, it yields the value of expression2; when zero, it yields the value of expression3.

Exam mnemonic: true before, false after.

(5) switch statement:

a) Be sure to note the difference between having break and not having break; in the book (page 34), in the example without break, as long as one case matches, the rest will execute; with break, it jumps directly out of the switch statement.

b) switch can only be used with break, not with continue.

(6) Examination of expressions:

There must be a value for an expression.

Assignment expression: the value of the expression is the leftmost value; a=b=5; this expression is 5; constants cannot be assigned.

Self-increment and self-decrement expressions: assuming a=5, ++a (is 6), a++ (is 5);

Operational mechanism: ++a first adds 1 to the variable’s value, then puts the obtained value back into variable a, making the value of this ++a expression 6; while a++ first uses the value of this expression as 5, then adds 1 to a making it 6, and then puts it back into variable a. After performing ++a and a++, if a is used later, it will be 6.

Exam mnemonic: ++ in front adds first and uses later, ++ at the back uses first and adds later.

The comma expression has the lowest precedence; the value of the expression is the rightmost expression.

The value of the expression (2, 3, 4) is 4.

Chapter Four – Loop Structures

(1) Three types of loop structures:

a) for(); while(); do-while() three types.

b) The for loop must have two semicolons, do not forget.

c) When writing a program, be sure to note that loops must have an end condition; otherwise, it becomes an infinite loop.

d) The last semicolon of the do-while() loop must not be omitted (be careful during practical exams).

(2) The difference between break and continue

Memory method:

break: means to break (breaks the entire loop), so seeing break exits the entire loop.

continue: means to continue (continues the loop), but ends the current loop, skipping the remaining statements in the loop body, jumping back to the beginning of the loop, checking the loop condition, and starting a new round of looping.

(3) Nested loops

It means having loops within loops; this can be complex, requiring careful layer-by-layer calculations; generally, remember that two layers are used for processing two-dimensional arrays.

(4) The difference between while((c=getchar())!=’\n’) and while(c=getchar()!=’\n’):

First, look at a = 3 != 2 and (a=3) != 2:

(!= has higher precedence than =) so the first computes 3!=2 first; the first a value is 1; the second a value is 3.

Exam note: The importance of parentheses here.

Chapter Five – Pointer Variables

The essence of pointer variables is to hold addresses, while general variables hold values.

int *p, the difference between *p and p:

*p can be used as a variable; the * serves to retrieve the value at the address stored in p.

p is used as an address.

*p++ and (*p)++ are important differences in error correction questions:

*p++ changes the address.

(*p)++ changes the value.

Three naming conventions (exam focus):

Array names: represent the address of the first element. Array names cannot be incremented; they are address constant names (often tested).

Function names: represent the entry address of the function.

String constant names: represent the address of the first character.

Chapter Six – Arrays

Important concepts of one-dimensional arrays:

Discussion of the array a[10].

1. a represents the array name, which is the address of the first element, i.e., the address of the element a[10].

2. a is an address constant, so any occurrence of a++, or a=a+2 assignments are errors.

3. a is a one-dimensional array name, so it is a column pointer; that is, a+1 jumps one column.

Discussion of the array a[3][3].

1. a represents the array name, which is the address of the first element, i.e., the address of the element a[10].

2. a is an address constant, so any occurrence of a++, or a=a+2 assignments are errors.

3. a is a two-dimensional array name, so it is a row pointer; that is, a+1 jumps one row.

4. a[0], a[1], a[2] are also address constants; they cannot be assigned values, and they are all column pointers; a[0]+1, a[1]+1, a[2]+1 all jump one column.

5. Note that a and a[0], a[1], a[2] are different; their base types are different. The former is a row of elements, while the latter are columns of elements.

Techniques for solving problems with two-dimensional arrays:

If there is a[3][3]={1,2,3,4,5,6,7,8,9} type of question.

Step one: Write them as: First column Second column Third column

a[0]→ 1 2 3 → First row

a[1]→ 4 5 6 → Second row

a[2]→ 7 8 9 → Third row

Step two: This way of solving problems is very simple:

*(a[0]+1) we know is the first row’s first element moving one column forward, so here it is the a[0][1] element, which is 1.

*(a[1]+2) we know is the second row’s first element moving two columns forward, so here it is the a[1][2] element, which is 6.

Be sure to remember: whenever dealing with two-dimensional array problems, write them in the above format before solving; this will make it easier.

Array initialization, both one-dimensional and two-dimensional, one-dimensional can be omitted, but the second dimension of two-dimensional must be written

int a[]={1,2} is valid. int a[][4]={2,3,4} is valid. But int a[4][]={2,3,4} is invalid.

Row pointers in two-dimensional arrays:

int a[1][2];

Here a is now a row pointer, a+1 jumps one row of array elements. Combined with (*p[2]) pointer

a[0], a[1] are now column pointers. a[0]+1 jumps one array element. Combined with *p[2] pointer array usage

Also remember the “strip method”:

a[2] becomes *(a+2), a[2][3] becomes *(a+2)[3], and can then be transformed into *(*(a+2)+3)

This concept is very important!

</x)&&(x<10) indicates greater than 0 and less than 10. <></x<10 is not permissible (be sure to remember).

(Intern Editor Reporter Group Liu Kun)

(Content adapted from University Student Growth Network)

Comprehensive Knowledge Points of C Language

Leave a Comment