|4. Operators in C
Chapter 4C Language Tutorial~1 min read

Operators in C

Operators — Calculations आणि Comparisons

Operator म्हणजे values वर operations करायचे symbols. C मध्ये arithmetic, relational, logical, assignment आणि bitwise operators आहेत.

Arithmetic Operators — गणित

Arithmetic operations

c
#include <stdio.h>

int main() {
    int a = 10, b = 3;

    printf("%d + %d = %d\n", a, b, a + b);  // 13
    printf("%d - %d = %d\n", a, b, a - b);  // 7
    printf("%d * %d = %d\n", a, b, a * b);  // 30
    printf("%d / %d = %d\n", a, b, a / b);  // 3 (integer division!)
    printf("%d %% %d = %d\n", a, b, a % b); // 1 (remainder)

    // float division साठी cast करा
    printf("%.2f / %d = %.2f\n", (float)a, b, (float)a / b); // 3.33

    // Increment / Decrement
    int x = 5;
    x++;   // x = 6 (post-increment)
    x--;   // x = 5 (post-decrement)
    ++x;   // x = 6 (pre-increment)
    printf("x = %d\n", x);  // 6

    return 0;
}

Relational आणि Logical Operators

Comparison and logical operations

c
#include <stdio.h>

int main() {
    int a = 10, b = 20;

    // Relational operators — result 1 (true) किंवा 0 (false)
    printf("a == b: %d\n", a == b);  // 0
    printf("a != b: %d\n", a != b);  // 1
    printf("a < b:  %d\n", a < b);   // 1
    printf("a > b:  %d\n", a > b);   // 0
    printf("a <= 10: %d\n", a <= 10); // 1

    // Logical operators
    int age = 20;
    int has_id = 1;  // 1 = true, 0 = false

    printf("Entry allowed: %d\n", age >= 18 && has_id); // 1 (AND)
    printf("Any pass: %d\n", age < 18 || has_id);       // 1 (OR)
    printf("Not student: %d\n", !has_id);               // 0 (NOT)

    return 0;
}

Assignment Operators

Assignment shorthand

c
int count = 10;

count += 5;   // count = count + 5  → 15
count -= 3;   // count = count - 3  → 12
count *= 2;   // count = count * 2  → 24
count /= 4;   // count = count / 4  → 6
count %= 4;   // count = count % 4  → 2

printf("count = %d\n", count);  // 2

Key Points — लक्षात ठेवा

  • Integer division: 10/3 = 3, not 3.33 — float साठी cast करा
  • % = modulus (remainder): 10%3 = 1
  • ++ (increment), -- (decrement)
  • Relational operators: result 1 (true) किंवा 0 (false)
  • && = AND, || = OR, ! = NOT
0/12 chapters पूर्ण