DAY3 C Language Operators and Explicit Type Conversion

程序员成长:技术、职场与思维模式实战指南 10w+人浏览 1.7k人参与

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

  1. Precision loss may occur after conversion;

  2. The original variable remains unchanged — only the result of the expression is converted;

  3. Example:

    int a = 3.14;     // a = 3, fractional part is discarded
    float b = 5;      // b = 5.0, fractional part filled with 0
    

Ⅱ. Arithmetic Operators

OperatorMeaningExample
+Addition1 + 2, a + b, a + 1
-Subtraction1 - 2, a - b, a - 1
*Multiplication1 * 2, a * b
/Division1 / 2, a / b, a / 2
%Modulus (remainder)10 % 3, a % 3
++Increment by 1a++ or ++a
--Decrement by 1a-- 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

OperatorMeaningExample
=Assignmenta = b + 1;
+=Add and assigna += 5;
-=Subtract and assigna -= 5;
*=Multiply and assigna *= 2;
/=Divide and assigna /= 2;
%=Modulus and assigna %= 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

  1. Float → Integer: fractional part is truncated.

  2. Integer → Float: fractional part filled with .0.

  3. Same-size type copy:

    • Assigning an unsigned short to a short may cause sign changes.
  4. Smaller type → Larger type:

    • Positive numbers: extra bits are filled with 0;
    • Negative numbers: extra bits are filled with 1 (sign extension).
  5. Larger type → Smaller type:

    • Higher bits are truncated (e.g., short → char).

Ⅴ. 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

Memory Mapping Example
Type Conversion Example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值