Fundamentals of C Language

Understanding the Structure of C Programs

This article introduces the basic components, format, and good writing style of C language through a simple C program example, providing a preliminary understanding of C language for beginners.

Example 1: A C program to calculate the sum of two integers:

#include
int main() {
int a, b, sum; /* Define variables a, b, and sum as integer variables */
a = 20; /* Assign the integer 20 to the integer variable a */
b = 15; /* Assign the integer 15 to the integer variable b */
sum = a + b; /* Assign the sum of the two numbers to the integer variable sum */
printf(“a=%d,b=%d,sum=%d\n”, a, b, sum); /* Output the calculation result to the display */
}

Key Points:

1. Every C program must include the following format:

int main() { }

This is the basic structure of a C program, and every program must contain this structure. The parentheses can be left empty, in which case the program will not execute any results.

2. main() – referred to as the “main function” in C language, a C program has exactly one main function, and any C program always starts execution from the main function. The pair of parentheses following the main function cannot be omitted.

3. The content enclosed in curly braces { } is called the body of the main function, and this part is what the computer will execute.

4. Each statement inside the { } must end with a semicolon (;). In C language, a statement that ends with a semicolon is called a C statement, and the semicolon is the marker for the end of a statement.

5. The line printf(“a=%d,b=%d,sum=%d\n”, a, b, sum); – by executing this C language system-provided screen output function, the user can see the running result. After running this program, the following result will be displayed on the monitor:

a=20,b=15,sum=35

6. #include

Note: (1) Lines starting with # (2) Not ending with a semicolon. This line does not have a semicolon, so it is not a statement; in C language, it is called a command line or “preprocessor directive”.

7. The part of the program that starts with /* and ends with */ represents comments in the program. Comments can be added anywhere in the program to improve readability, but the computer completely ignores the comment part when executing the main function, in other words, the computer treats the comment part as if it does not exist in the main function.

Fundamentals of C Language

The Process of Generating C Programs

C programs are first compiled from source files to generate object files, and then linked to generate executable files.

The source program has the extension .c, the object program has the extension .obj, and the executable program has the extension .exe.

Identifiers

When writing programs, functions, variables, etc., must be named, and this name is called an identifier. The naming rules for identifiers in C language are as follows:

  • Identifiers can only consist of letters, digits, and underscores;

  • The first letter of an identifier must be a letter or an underscore;

  • Identifiers are case-sensitive, for example, If and if are two completely different identifiers.

Valid identifiers include: A6, b_3, _mn. Invalid identifiers include: ab#12, 8m, tr3:4, yes no.

Identifiers cannot be the same as keywords that have special meanings in the program, nor can they be the same as user-defined function names or C library functions. In the program, various identifiers should not be repeated to facilitate distinction. When choosing variable names and other identifiers, it is advisable to ensure that they are meaningful.

Fundamentals of C Language

Identifiers can be classified into the following three categories:

1. Keywords

Keywords are a class of identifiers with specific meanings, specifically used to indicate specific components of C language, and cannot be used as user-defined identifiers.

auto, break, case, char, union, do, double, else, enum, extern, goto, if, int, long, short, signed, static, sizeof, struct, switch, unsigned, void, for, while, typedef, continue, float, return, typedef, default

2. Predefined Identifiers

Predefined identifiers also have specific meanings in C language but can be used as user-defined identifiers. Predefined identifiers are divided into two categories:

(1) Library function names, such as (printf, scanf, sin, isdigit, etc.) (2) Preprocessor directive names, such as (define, include)

3. User-defined Identifiers

User-defined identifiers are those defined by the user as needed. Regardless of how identifiers are defined, they must comply with the three naming rules for identifiers.

Fundamentals of C Language

Constants

Constants are quantities whose values cannot be changed during program execution. There are five types of constants: integer constants, real constants, character constants, string constants, and symbolic constants.

(1) Number Conversion

There are four representations of numbers:

①: Binary: All numbers consist of 0s and 1s, with every two making one, and binary numbers will not contain 2. Example: 110101 ②: Octal: Starts with the digit 0 (note it is not the letter O), all numbers consist of 0 to 7, with every eight making one, and octal numbers will not contain 8. Example: 0112, 0123, 077, etc. ③: Decimal: All numbers consist of 0 to 9, with every ten making one, and decimal numbers will not contain 10. Example: 0, 12, -15, etc. ④: Hexadecimal: Starts with 0x or 0X (the digit 0 plus the letter x), all numbers consist of 0 to 9, A to F (or a to f), with every sixteen making one (where A, B, C, D, E, F represent 10, 11, 12, 13, 14, 15 respectively). Example: 0x4A, 0X14c7, etc.

Internally, numbers are represented and stored in binary form. Ordinary decimal numbers input by users must be converted to binary for internal storage in the computer. Similarly, the results of calculations by the computer are also in binary, and they are generally converted back to decimal for user readability. This conversion is usually performed automatically by the computer.

(1) Converting Decimal to Binary, Octal, and Hexadecimal

Division: Divide the decimal number by 2, record the remainder, continue dividing the quotient by 2 until the quotient is 0, then arrange the remainders obtained in reverse order to get the binary number corresponding to that decimal number. The methods for converting to octal and hexadecimal are the same.

Example: The decimal number 13 converts to binary as 1101, to octal as 015, and to hexadecimal as D.

(2) Converting Binary, Octal, and Hexadecimal to Decimal

Multiplication and summation: Multiply each bit of the binary number from low to high (right is low, left is high) by 2^0, 2^1, 2^2, …, and then sum these products.

For example: (1101)2 = (13)10, (317)8 = (207)10, (23E)16 = (574)10.

(3) Converting between Binary and Octal, Hexadecimal

①: Binary to Octal: Group every three bits from right to left into decimal numbers, and the resulting data combined is the corresponding octal number (note: if the high bit is less than three bits, pad with zeros). Example: (010 110 111)2 = (267)8 ②: Binary to Hexadecimal: Group every four bits from right to left into decimal numbers, and the resulting data combined is the corresponding hexadecimal number (note: if the high bit is less than four bits, pad with zeros). Example: (0101 1011)2 = (5B)16 ③: Octal to Binary: Convert each digit to three-bit binary numbers. Example: (13)8 = (001 011)2 = (1011)2 (note: remove the leading zeros, as 0 has no significance in high bits) ④: Hexadecimal to Binary: Convert each digit to four-bit binary numbers. Example: (E3)16 = (1110 0011)2

(2) Integer Constants

Integer constants have three forms: decimal integer constants, octal integer constants, and hexadecimal integer constants.

(Note: C language does not directly represent binary integer constants, and binary will not appear in C language source programs.)

The writing format is as follows:

Decimal integer constants: 123, 0, -24, 85L (long integer constant), etc. Octal integer constants: 051, -026, 0773, etc. Hexadecimal integer constants: 0x55, 0x1101, 0x, 0x5AC0, -0xFF. Here, L indicates a long integer.

(3) Real Constants

Real constants have two representations: decimal form and exponential form.

Decimal form: 5.4, 0.074, -23.0 Exponential form: 5.4e0, 4.3e-3, -3.3e4

(1) Real constants with a decimal part of 0 can be written as 453.0 or 453. (2) When using decimal representation, there must be numbers on both sides of the decimal point; it cannot be written as “.453” and “453.”, but should be written as “0.453” and “453.0”. (3) When using exponential notation, there must be a number before e, and the exponent after e must be an integer (note: the integer exponent can be positive, negative, or can be octal or hexadecimal, but must be an integer).

(4) Character Constants

Character constants are marked by a pair of single quotes ‘ ’. In C language, there are two types of character constants:

(1) A single character enclosed in a pair of single quotes, such as ‘a’, ‘r’, ‘#’. Note: ‘a’ and ‘A’ are two different character constants.

(2) A character constant enclosed in a pair of single quotes, starting with a backslash ollowed by several digits or letters, such as ‘\n’, where “\” means escape, and the character following it represents different meanings. This type of character constant is called an escape character. Specific examples are shown in the table below.

Escape Character Meaning of Escape Character ASCII Code

\n New line 10\t Horizontal tab to next tab position 9\b Backspace 8\r Carriage return 13\f Form feed 12\\ Backslash symbol “\” 92\’ Single quote symbol 39\” Double quote symbol 34\a Bell sound 7\ddd 1 to 3 digit octal number representing a character\xhh 1 to 2 digit hexadecimal number representing a character

Fundamentals of C Language

(5) String Constants

In C language, a sequence of characters enclosed in double quotes is a string constant.

Example: “ni hao”, “happy”, etc.

(6) Symbolic Constants

Symbolic constants are constants defined by macro definitions using “#define” in C programs, which can be represented by identifiers.

Example: A C program to calculate the area of a circle.

#include
#define PI 3.14159
int main() {
float r, s;
r = 12.5;
s = PI * r * r;
printf(“s= %f “, s);
}

Note: #define is a macro definition; in this program, every occurrence of PI represents 3.14159. PI is referred to as a symbolic constant. It is customary to use uppercase letters to represent symbolic constants and lowercase letters to represent variables, making it easier to distinguish between them.

Variables

A variable is a quantity whose value can change. A variable must have a name and occupies a certain storage unit in memory, where the value of that variable is stored. Different types of variables occupy different sizes of storage units, and variables must be defined before use.

(1) Integer Variables

Integer variables are divided into four types: basic type (int), short type (short int or short), long type (long int or long), and unsigned type (unsigned int, unsigned short, unsigned long).

Different compilation systems have different specifications for the number of bits and value ranges occupied by these four types of integer data.

Type Specifiers

Fundamentals of C Language

Note:

The word signed indicates “signed” (i.e., distinguishing between positive and negative numbers); not writing signed also implies signed. Unsigned indicates “unsigned” (only representing positive numbers).

(2) Real Variables

In C language, real variables are divided into single precision (float) and double precision (double). For example:

float a, b;
double m;

In VC, float type data occupies 4 bytes (32 bits) in memory, while double type data occupies 8 bytes. Single precision real numbers provide 7 significant digits, while double precision real numbers provide 15 to 16 significant digits. Real constants are not distinguished between float and double; a real constant can be assigned to either a float or double variable, but the variable will truncate the corresponding significant digits of the real constant based on its type.

Note: Real variables can only store real values; integer variables cannot store real values, and real variables cannot store integer values.

(3) Character Variables

Character variables are used to store character constants, defined as:

char variable_name;

Where the keyword char defines the character data type, occupying one byte of storage unit.

Example: char cr1, cr2; cr1 = ‘A’, cr2 = ‘B’;

When assigning a character to a character variable, it does not store the character itself in memory, but rather stores the corresponding ASCII code of that character in the memory unit. For example, the ASCII code for character ‘A’ is 65, and its storage form in memory is as follows: 01000001.

Since characters are stored in memory as ASCII codes, their storage form is similar to that of integers, so character data and integer data can be interchangeable in C language. A character can be output in both character and integer forms, and character data can also be involved in arithmetic operations, which is equivalent to performing operations on their ASCII codes.

Fundamentals of C Language

Automatic Type Conversion and Type Casting

When the types of data in the same expression are different, the compiler will automatically convert them to the same type before performing calculations. The conversion priority is:

char < int < float < double

That is, the lower-level type on the left converts to the higher-level type on the right. Specifically, if the highest priority data in the expression is of type double, then all other data in that expression will be converted to type double, and the calculation result will also be of type double; if the highest priority data is of type float, then all other data in that expression will be converted to type float, and the calculation result will also be of type float.

In assignment operations, if the types on both sides of the assignment operator are different, the type on the right side of the assignment operator will be converted to the type on the left side; when the type on the right is higher than that on the left, the data on the right will be truncated during conversion.

In addition to automatic conversion, there is also type casting, represented as:

(type)(expression); Example: (int)(a + b)

Discussion:When the value of a is assigned as 3.4 and the value of b is assigned as 2.7, what are the values of (int)(a + b) and (int)a + b?

Fundamentals of C Language

Understanding C Operators

C language has a wide range of operators, which can be divided into the following categories:

1. Arithmetic Operators: Used for various numerical operations. Includes addition (+), subtraction (-), multiplication (*), division (/), modulus (%), increment (++), and decrement (–), totaling seven types.

2. Assignment Operators: Used for assignment operations, divided into simple assignment (=), compound arithmetic assignment (+=, -=, *=, /=, %=), and compound bitwise assignment (&=, |=, ^=, >>=, <<=), totaling eleven types.

3. Comma Operator: Used to combine several expressions into one expression (,).

4. Relational Operators: Used for comparison operations. Includes greater than (>), less than (<), equal to (==), greater than or equal to (>=), less than or equal to (<=), and not equal to (!=), totaling six types.

5. Logical Operators: Used for logical operations. Includes AND (&&), OR (||), and NOT (!), totaling three types.

6. Conditional Operator: This is a ternary operator used for conditional evaluation (?:).

7. Bitwise Operators: Operate on binary bits. Includes bitwise AND (&), bitwise OR (|), bitwise NOT (~), bitwise XOR (^), left shift (<<), and right shift (>>), totaling six types.

8. Pointer Operators: Used for dereferencing (*) and address-of (&) operations.

9. Sizeof Operator: Used to calculate the number of bytes occupied by a data type (sizeof).

10. Special Operators: Include parentheses (), subscript [], member (->, .), etc.

Additionally, based on the number of operands involved in the operation, C language operators can be classified as: unary operators (e.g., !), binary operators (e.g., +, -), and ternary operators (e.g., ? :).

Arithmetic Operators and Arithmetic Expressions

1. Basic Arithmetic Operators

(1) + (Addition operator or positive value operator, e.g., 2 + 5).

(2) – (Subtraction operator or negative value operator, e.g., 4 – 2).

(3) * (Multiplication operator, e.g., 3 * 8).

(4) / (Division operator, e.g., 11 / 5).

The division operation has two cases:

a. When both sides of the division are integers, the result will definitely be an integer (note: only the integer part is taken, not rounded).

For example: 5 / 2 equals 2, not 2.5; 1 / 2 equals 0.

b. When at least one side of the division is a real number (i.e., decimal), the result will be a real number.

For example: 5 / 2.0 equals 2.5; 7.0 / 2.0 equals 3.5.

(5) % (Modulus operator or remainder operator, both sides must be integer data, e.g., 9 % 7 equals 2).

It should be noted that when the operands are negative, the result may vary depending on the compiler. In VC, the sign of the result is the same as the dividend; for example, 13 % -2 equals 1, while -15 % 2 equals -1.

Fundamentals of C Language

2. Arithmetic Expressions and Operator Precedence and Associativity

An arithmetic expression is an expression that connects operands (also called operands) using arithmetic operators and parentheses, conforming to C language syntax rules. The operands include functions, constants, and variables.

In computer languages, the evaluation rules for arithmetic expressions are similar to the rules of arithmetic operations in mathematics, and the operation rules and requirements are as follows:

(1) In arithmetic expressions, multiple layers of parentheses can be used, but the parentheses must be paired. The calculation starts from the innermost parentheses, calculating the values of each expression from the inside out.

(2) In arithmetic expressions, for operators of different precedence, operations can be performed according to the operator precedence from high to low. If operators of the same precedence appear in the expression, operations are performed according to the associativity of the operators.

(3) If the types of operands on either side of an operator are different, automatic conversion or type casting is used to make both operands the same type before performing the operation.

3. Increment and Decrement Operators

Function: Increases or decreases the value of a variable by 1.

For example: ++i, –i (increment or decrement the value of i before using it). i++, i– (increment or decrement the value of i after using it).

(1) Only variables can use increment (++) and decrement (–) operators; constants or expressions cannot be used, such as 10++ or (x + y)++ are illegal.

(2) The associativity of ++ and — is “right to left”, for example, -i++ means that the left side is the negation operator, and the right side is the increment operator. Both negation and increment operations are “right to left” associative, equivalent to -(i++).

Increment (decrement) operators are commonly used in loop statements and pointers. Students should understand the difference between “i++” and “++i” as well as “i–” and “–i”, especially the difference between the value of the expression and the value of the variable.

Fundamentals of C Language

Assignment Operators and Assignment Expressions

1. Assignment Operators and Assignment Expressions

The assignment symbol “=” is the assignment operator, which serves to assign a value to a variable or assign the value of one variable to another variable. An expression composed of assignment operators is called an assignment expression. The general form is:

variable_name = expression

In a program, a variable can be assigned multiple times; each time a value is assigned, the data in the corresponding storage unit is updated, and the current data in memory is the last value assigned.

Example: a = 12; This expression reads as “assign the value of 10 to variable a”.

Note: a. If the types of the operands on both sides of the assignment operator are inconsistent, the system will automatically perform type conversion. The conversion rule is: the type of the value on the right side of the assignment operator is converted to the type of the variable on the left side.

Example: int y = 3.5; The final value stored in variable y is the integer 3.

b. The value of the copied expression can be reassigned to a variable, forming a continuous assignment.

For example: x = y = 25 is a continuous assignment expression, x = y = 25 is equivalent to x = (y = 25), so the final value of the expression x = y = 25 is 25.

Fundamentals of C Language

2. Compound Assignment Operators

By adding other operators before the assignment operator, compound assignment operators can be formed. The compound operators related to arithmetic operations are: +=, -=, *=, /=, %=.

There should be no spaces between the two symbols, and the precedence of compound assignment operators is the same as that of assignment operators. The expression n += 1 is equivalent to n = n + 1, which means taking the value in variable n, increasing it by 1, and then assigning it back to variable n; the operation rules for other compound assignment operators follow suit.

For example, to find the value of the expression a += a -= a * a, where the initial value of a is 12.

Steps:

(1) First perform the operation “a -= a * a”, which is equivalent to a = a – a * a = 12 – 144 = -132. (2) Then perform the operation “a += -132”, which is equivalent to a = a + (-132) = -132 – 132 = -264.

Comma Operator and Comma Expressions

In C language, the comma is used not only as a separator but also as an operator – the comma operator, which connects several expressions together, such as a = b + c, a = b * c, etc., is called a comma expression.

The general form is:

expression1, expression2, expression3, …, expressionn

Example: x = 2, y = 3, z = 4

Comma expressions have left-to-right associativity, meaning that expression1 is evaluated first, followed by expression2, until expressionn is evaluated. The value of expressionn is the value of the entire comma expression. The value of the above comma expression is the value of expression z = 4, which is 4. It should be noted that the comma operator has the lowest precedence among all operators.

Example: Consider the following program segment:

int main() {
int a = 2, b = 4, c = 6, x, y;
y = (x = a + b), (b + c);
printf(“y=%d,x=%d”, y, x);
}

The program displays the result: y = 6, x = 6.

Discussion: What would be the result of changing y = (x = a + b), (b + c); to y = ((x = a + b), b + c)?

Relational Operators and Relational Expressions

1. Logical Values in C Language

In C language, there are only two logical values: true and false. Non-zero represents true, while zero represents false. Therefore, for any expression, if its value is zero, it represents a false value; if its value is non-zero, it represents a true value. For example, -5 has a logical value of true.

2. Logical Expressions

“&&” and “||” have two operands, so they are both binary operators, while “!” has only one operand, making it a unary operator. Examples of logical operations are as follows:

(1) a && b: The expression a && b is true only when both a and b are true.

It is worth noting that in mathematics, the relational expression 0

(2) a || b: The expression a || b is true when at least one of a or b is true.

(3) !a: Represents negation; if a is true, then !A is false, and vice versa. For example, !-5 equals 0.

In C language, logical expressions composed of && or || may produce a “short-circuit” phenomenon in certain specific situations.

(1) x && y && z: Only when x is true (non-zero) does it need to evaluate the value of y; only when both x and y are true does it need to evaluate the value of z; if x is false, there is no need to evaluate y and z, and the entire expression evaluates to 0. The mnemonic is: “one false means all false”.

Example: (!5 == 1) && (++i == 0) – the value of the expression (!5 == 1) is 0, so the computer skips the evaluation of (++i == 0); the value of the expression (!5 == 1) && (++i == 0) is 0.

(2) x || y || z: As long as the value of x is true (non-zero), there is no need to evaluate the values of y and z; the entire expression evaluates to 1. Only when the value of x is false does it need to evaluate the value of y; only when both x and y are false does it need to evaluate the value of z. The mnemonic is: “one true means all true”.

Fundamentals of C Language

Bitwise Operations

1. Bitwise Operators

In computers, data is stored in binary form, and bitwise operations refer to operations on the binary bits in storage units. C language provides six types of bitwise operators.

2. Bitwise Operations

The bitwise operators & | ~ << >> ^ are arranged in order of precedence from high to low as follows:

The bitwise NOT operator “~” has the highest precedence, while left shift and right shift have the same precedence, followed by bitwise AND “&”, bitwise XOR “^”, and bitwise OR “|”. The order is ~ << >> & ^ |.

Example 1: The left shift operator “<<” is a binary operator. Its function is to shift all bits of the operand on the left of “<<” to the left by a specified number of bits indicated by the number on the right of “<<”, discarding high bits and padding low bits with 0.

For example: a << 4 means shifting all bits of a to the left by 4 bits. If a = 00000011 (decimal 3), after left shifting 4 bits, it becomes 00110000 (decimal 48).

Example 2: The right shift operator “>>” is a binary operator. Its function is to shift all bits of the operand on the left of “>>” to the right by a specified number of bits indicated by the number on the right of “>>”.

For example: if a = 15, a >> 2 means shifting 00000111 to 00000011 (decimal 3).

It should be noted that for signed numbers, the sign bit will move along with it. When it is a positive number, the highest bit is padded with 0, while for negative numbers, the sign bit is 1, and whether the highest bit is padded with 0 or 1 depends on the compiler’s specifications.

Example 3: If the binary number a is 00101101, what binary number b should be to flip the high 4 bits of a while keeping the low 4 bits unchanged using XOR operation?

Analysis: XOR operation is commonly used to flip specific bits; simply XOR the bits to be flipped with 1, because bits with a value of 1 in the original number will yield 0 when XORed with 1, and bits with a value of 0 will yield 1 when XORed with 1. Bits XORed with 0 will retain their original values. XOR operation can also be used to swap two values without using a temporary variable.

For example, if int a = 3, b = 4; to swap the values of a and b, the following statements can be used: a = a ^ b; b = b ^ a; a = a ^ b;

Thus, the answer to this question is: 11110000.

Fundamentals of C Language

C language is a very suitable language for programming beginners, and the importance of building a solid foundation cannot be overstated. Therefore, I present this article of valuable content, hoping that everyone can benefit from it.

Reply with keywords in the background:

PS Tutorial | Resume | Office | University Resources | Big Package

Web Production | Internship | Employment | Interview | Software

Postgraduate Entrance Examination | CET-4 | CET-6 | MATLAB …

Get more high-quality content from University FM!

(Content sourced from the internet, organized by University FM, please retain the card below when reprinting)

Fundamentals of C Language

Leave a Comment