โ๏ธ Detailed Explanation of C Language Operators and Expressions โ Comprehensive Understanding of Arithmetic, Logical, and Assignment Operators
๐งฉ I. Introduction
In the previous section, we learned about variable definitions and input/output functions (printf, scanf). However, the true “soul” of a program lies in its ability to perform calculations and logical judgments on data. In this lesson, we will comprehensively master the operators and expressions in C language โ enabling your program to not only “display” but also “think”!
๐งฎ II. What are Operators and Expressions
โ Operator
An operator is a symbol that performs operations on variables or constants. For example: +, -, *, /, %, ==, &&, etc.
โ Expression
An expression is a combination of operators and operands that results in a value after evaluation. For example:
a + b x * y – 3 (a + b) * c
โ III. Arithmetic Operators
Arithmetic operators are used for mathematical calculations and are the most common type of operations.
Operator Meaning Example Result
- Add 3 + 2 5
- Subtract 5 – 2 3
- Multiply 4 * 3 12 / Divide 7 / 2 3 (integer division truncates)
- Modulo 7 % 2 1
๐ง Note:
-
If both numbers are integers, the result of / will be an integer.
-
To obtain a decimal result, use floating-point types:
float result = 7.0 / 2; printf(“%.2f”, result); // Output 3.50
โ๏ธ IV. Relational Operators
Used to compare the size relationship between two values, the result can only be two types: ๐ True (1) or False (0).
Operator Meaning Example Result
== Equal a == b True if equal != Not equal a != b True if not equal
Greater than a > b True if a is greater < Less than a < b True if a is less = Greater than or equal a >= b True if condition is met <= Less than or equal a <= b True if condition is met
โ Example:
int a = 5, b = 8; printf(“%d\n”, a > b); // Output 0 (false) printf(“%d\n”, a != b); // Output 1 (true)
๐งฉ V. Logical Operators
Logical operators are used to combine multiple conditions, commonly used in if statements.
Operator Meaning Example Result
&& And (&&) a > 0 && b > 0 True if both conditions are true Or (||) ! Not !(a > b) Negation, true becomes false, false becomes true
โ Example:
int a = 10, b = 20; if (a > 0 && b > 0) { printf(“a and b are both positive\n”); }
๐ VI. Assignment Operators
Operator Meaning Example Equivalent to
= Assignment a = 5 โ += Add and assign a += 3 a = a + 3 -= Subtract and assign a -= 2 a = a – 2 *= Multiply and assign a *= 2 a = a * 2 /= Divide and assign a /= 2 a = a / 2 %= Modulo and assign a %= 3 a = a % 3
โ Example:
int a = 10; a += 5; // Equivalent to a = a + 5 printf(“%d\n”, a); // Output 15
๐งฎ VII. Increment and Decrement Operators
Operator Meaning Example Description
++ Increment by 1 a++ or ++a Variable increases by 1 — Decrement by 1 a– or –a Variable decreases by 1
โ ๏ธ Difference:
int a = 5; printf(“%d\n”, a++); // Use first then add โ Output 5 printf(“%d\n”, ++a); // Add first then use โ Output 7
โ๏ธ VIII. Comprehensive Case: Finding the Maximum of Three Numbers
#include <stdio.h> int main() { int a, b, c, max; printf("Please enter three integers:"); scanf("%d %d %d", &a, &b, &c); max = a; // Assume a is the largest if (b > max) max = b; // Compare b if (c > max) max = c; // Compare c printf("The maximum value is: %d\n", max); return 0; }
Example of running result:
Please enter three integers: 8 3 9 The maximum value is: 9
๐ง IX. Class Summary
โ In this section, we have mastered:
The classification and usage of various operators in C language
The basic logic of expression evaluation
The role of relational and logical operators in conditional statements
The simplified syntax for assignment, increment, and decrement
๐ใIoT Smart Classroomใ
Higher Vocational Internet of Things and Programming Teaching ยท Learn Programming from Scratch. Follow me, and in the next lesson, we will master the “thinking ability” of programs together!