DAY3. C Language Operators and Explicit Type Conversion
C Language Operators and Explicit Type Conversion
Ⅰ. Explicit Type Conversion (Type Casting)
When a programmer determines that it’s necessary to convert one data type to another, explicit type conversion (type casting) is used.
Syntax
(type) constant
(type) variable
(type) expression
Notes
-
Precision loss may occur after conversion;
-
The original variable remains unchanged — only the result of the expression is converted;
-
Example:
int a = 3.14; // a = 3, fractional part is discarded float b = 5; // b = 5.0, fractional part filled with 0
Ⅱ. Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 1 + 2, a + b, a + 1 |
- | Subtraction | 1 - 2, a - b, a - 1 |
* | Multiplication | 1 * 2, a * b |
/ | Division | 1 / 2, a / b, a / 2 |
% | Modulus (remainder) | 10 % 3, a % 3 |
++ | Increment by 1 | a++ or ++a |
-- | Decrement by 1 | a-- or --a |
Important Notes
-
The divisor cannot be 0, otherwise you’ll get:
Floating point exception (core dumped) -
%(modulus) can only be used with integers (int,short,char,long) — not with floating-point numbers. -
For increment and decrement:
int a = 10; a++; // Post-increment: use first, then add 1 ++a; // Pre-increment: add first, then use
Ⅲ. Assignment Operators
| Operator | Meaning | Example |
|---|---|---|
= | Assignment | a = b + 1; |
+= | Add and assign | a += 5; |
-= | Subtract and assign | a -= 5; |
*= | Multiply and assign | a *= 2; |
/= | Divide and assign | a /= 2; |
%= | Modulus and assign | a %= 3; |
Concept
The
=operator copies the value from the right-hand side (Rvalue) into the left-hand side (Lvalue) memory location.
- Lvalue: A variable that can be assigned a value.
- Rvalue: A value or expression that can appear on the right side of an assignment.
Valid Example
a = b + 1;
Invalid Example
a + 1 = 20; // Left-hand side is not a variable
Ⅳ. Type Matching and Compatibility Rules
-
Float → Integer: fractional part is truncated.
-
Integer → Float: fractional part filled with
.0. -
Same-size type copy:
- Assigning an
unsigned shortto ashortmay cause sign changes.
- Assigning an
-
Smaller type → Larger type:
- Positive numbers: extra bits are filled with 0;
- Negative numbers: extra bits are filled with 1 (sign extension).
-
Larger type → Smaller type:
- Higher bits are truncated (e.g.,
short → char).
- Higher bits are truncated (e.g.,
Ⅴ. Comma Operator
Expressions connected by commas (
,) form a comma expression.
- Evaluation order: from left to right.
- The final value of the expression is the value of the last expression.
Example:
int a, b, c, num;
num = (a + b, b + c, a - c, b - c);
printf("%d\n", num); // Output: result of (b - c)
Ⅵ. sizeof() Operator
Used to obtain the number of bytes a variable, data type, or expression occupies in memory.
Usage
sizeof(variable);
sizeof(type);
sizeof(expression);
Example:
int a;
printf("%lu\n", sizeof(a)); // Output: 4
printf("%lu\n", sizeof(int)); // Output: 4
printf("%lu\n", sizeof(3.14)); // Output: 8
Ⅶ. Operator Associativity
Most C operators have left-to-right associativity,
but assignment operators (=,+=,-=etc.) are right-to-left associative.
Ⅷ. Example: Assignment and Type Conversion
#include <stdio.h>
int main(void)
{
short a = 0;
a = 10;
unsigned short b = 49152;
a = b; // Different types, but compatible
printf("a = %d\n", a);
return 0;
}
Memory Visualization


802

被折叠的 条评论
为什么被折叠?



