The basic knowledge of C language includes variables, data types, operators, control flow statements, functions, etc. Each part is explained in detail below, and corresponding cases are given.
-
Variables and data types:
- Variables: Variables in C language are used to store data and need to be declared before being used. When declaring a variable, you need to specify its data type. For example:
Copy
int age; // 声明一个整型变量 float price; // 声明一个浮点型变量 char grade; // 声明一个字符类型变量
- Data type: C language supports a variety of data types, including integer types (int, short, long), floating point types (float, double), character types (char), Boolean types (bool), enumeration types (enum), etc. . For example:
Copy
int x = 10; float y = 3.14; char c = 'A'; bool isTrue = true; enum Color {RED, GREEN, BLUE};
-
operator:
- Arithmetic operators: including addition (+), subtraction (-), multiplication (*), division (/) and remainder (%), etc. For example:
Copy
int a = 10; int b = 5; int sum = a + b; // 加法运算 int difference = a - b; // 减法运算 int product = a * b; // 乘法运算 int quotient = a / b; // 除法运算 int remainder = a % b; // 取余运算
- Relational operator: used to compare the size relationship between two values, returning a boolean (true or false). For example:
Copy
int x = 10; int y = 5; bool isEqual = (x == y); // 相等运算 bool isGreater = (x > y); // 大于运算 bool isLess = (x < y); // 小于运算
- Logical operators: Used to combine multiple conditions and return Boolean results. Including and (&&), or (||), and not (!), etc. For example:
Copy
bool isTrue = true; bool isFalse = false; bool result1 = isTrue && isFalse; // 与运算 bool result2 = isTrue || isFalse; // 或运算 bool result3 = !isTrue; // 非运算
-
Control flow statement:
- Conditional Statements (if-else): Execute different blocks of code depending on whether the condition is true or false. For example:
Copy
int x = 10; if (x > 0) { printf("x is positive\n"); } else if (x < 0) { printf("x is negative\n"); } else { printf("x is zero\n"); }
- Loop statements (for, while, do-while): Repeatedly execute a block of code. For example:
Copy
for (int i = 0; i < 5; i++) { printf("i is %d\n", i); } int j = 0; while (j < 5) { printf("j is %d\n", j); j++; } int n = 0; do { printf("n is %d\n", n); n++; } while (n < 5);
-
function:
- A function is a block of code that performs a specific function and can be called and reused multiple times by other code.
- Functions need to be declared before they are used, and include the function name, parameter list and function body.
- A function may or may not return a value.
- For example:
Copy
int add(int a, int b) { return a + b; } int result = add(3, 4); printf("Result is %d\n", result);
These are the basic knowledge and detailed cases of C language. By learning and practicing these basic knowledge and cases, you will be able to master the basic programming ability of C language. Hope it helps you!