🟢 Beginner  ·  Lesson 08

Operators in C

C में Operators

Types of Operators in C

An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. C has a rich set of operators that can be categorized as:

  • Arithmetic Operators: + - * / %
  • Relational (Comparison) Operators: == != > < >= <=
  • Logical Operators: && || !
  • Assignment Operators: = += -= *= /= %=
  • Increment/Decrement: ++ --
  • Bitwise Operators: & | ^ ~ << >>
  • Ternary Operator: ? :
  • sizeof Operator: sizeof()

1. Arithmetic Operators

Used to perform basic mathematical operations.

OperatorNameExample (a=10, b=3)Result
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3 (integer division!)
%Modulus (Remainder)a % b1
C Language
#include <stdio.h>
int main() {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);  // Integer division
    printf("a %% b = %d\n", a % b); // %% prints single %
    return 0;
}
a + b = 13 a - b = 7 a * b = 30 a / b = 3 a % b = 1
⚠️ Integer Division

10 / 3 gives 3, not 3.33! When both operands are integers, C performs integer division (truncates the decimal). Use 10.0 / 3 or cast: (float)10 / 3 to get 3.333333.

2. Relational Operators

Used to compare two values. Always return 1 (true) or 0 (false).

OperatorMeaninga=5, b=3Result
==Equal toa == b0 (false)
!=Not equal toa != b1 (true)
>Greater thana > b1 (true)
<Less thana < b0 (false)
>=Greater than or equala >= 51 (true)
<=Less than or equala <= 30 (false)
💡 Common Mistake: = vs ==

= is assignment (sets a value). == is comparison (checks equality). Writing if (a = 5) instead of if (a == 5) is a classic bug!

3. Logical Operators

Used to combine multiple conditions. Return 1 (true) or 0 (false).

OperatorNameExampleResult
&&AND1 && 00 — both must be true
||OR1 || 01 — at least one true
!NOT!10 — reverses the value
C Language – Logical Operators Example
#include <stdio.h>
int main() {
    int marks = 85, attendance = 78;

    // Eligible if marks >= 80 AND attendance >= 75
    if (marks >= 80 && attendance >= 75) {
        printf("Eligible for scholarship\n");
    }
    // Fail if marks < 40 OR attendance < 75
    if (marks < 40 || attendance < 75) {
        printf("Not eligible\n");
    }
    return 0;
}
Eligible for scholarship

4. Assignment Operators

OperatorExampleEquivalent to
=a = 5a = 5
+=a += 3a = a + 3
-=a -= 2a = a - 2
*=a *= 4a = a * 4
/=a /= 2a = a / 2
%=a %= 3a = a % 3

5. Increment & Decrement Operators

++ increases a value by 1, -- decreases by 1. These can be pre (before variable) or post (after variable).

C Language
#include <stdio.h>
int main() {
    int a = 5;
    printf("a++  = %d\n", a++);  // Prints 5, THEN increments to 6
    printf("a    = %d\n", a);    // Now a is 6
    printf("++a  = %d\n", ++a);  // Increments to 7, THEN prints 7
    printf("a--  = %d\n", a--);  // Prints 7, THEN decrements to 6
    printf("--a  = %d\n", --a);  // Decrements to 5, THEN prints 5
    return 0;
}
a++ = 5 a = 6 ++a = 7 a-- = 7 --a = 5

6. Ternary Operator ( ? : )

A short form of if-else. Syntax: condition ? value_if_true : value_if_false

C Language
#include <stdio.h>
int main() {
    int marks = 75;
    char *result = (marks >= 40) ? "Pass" : "Fail";
    printf("Result: %s\n", result);

    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Max: %d\n", max);
    return 0;
}
Result: Pass Max: 20

Operator Precedence (Priority)

When an expression has multiple operators, precedence determines which gets evaluated first. Higher precedence = evaluated first.

PriorityOperatorsAssociativity
1 (Highest)() [] . ->Left to Right
2++ -- ! ~ (cast) sizeofRight to Left
3* / %Left to Right
4+ -Left to Right
5<< >>Left to Right
6< <= > >=Left to Right
7== !=Left to Right
8–12Bitwise &, ^, |, &&, ||Left to Right
13?:Right to Left
14 (Lowest)= += -= *= /=Right to Left

Example: 2 + 3 * 43 * 4 = 12 first, then 2 + 12 = 14 (not 20!).

Use parentheses () to override precedence: (2 + 3) * 4 = 20.

Summary

  • Arithmetic: + - * / % — basic math operations
  • Relational: == != > < — compare values, return 0 or 1
  • Logical: && || ! — combine conditions
  • Assignment: = += -= — assign values
  • Increment/Decrement: ++ -- — increase/decrease by 1
  • Ternary: ? : — one-line if-else
  • Don't confuse = (assignment) with == (comparison)
🏋️ Practice Exercise

Write a program that takes two numbers and prints: their sum, difference, product, quotient, remainder, which is greater (using ternary), and whether their sum is even or odd (using %).

Operators के प्रकार

एक operator एक symbol है जो compiler को एक या अधिक operands पर specific operation perform करने के लिए कहता है। C में operators के ये प्रकार हैं:

  • Arithmetic Operators: + - * / %
  • Relational Operators: == != > < >= <=
  • Logical Operators: && || !
  • Assignment Operators: = += -= etc.
  • Increment/Decrement: ++ --
  • Ternary Operator: ? :

1. Arithmetic Operators

Basic mathematical operations के लिए use होते हैं।

OperatorनामExample (a=10, b=3)Result
+जोड़a + b13
-घटावa - b7
*गुणाa * b30
/भागa / b3 (integer division!)
%शेषफल (Modulus)a % b1
⚠️ Integer Division

10 / 3 का result 3 होगा, 3.33 नहीं! जब दोनों operands integers हों, C integer division करता है। Float result के लिए: (float)10 / 3 use करें।

2. Relational Operators

दो values compare करने के लिए use होते हैं। हमेशा 1 (true) या 0 (false) return करते हैं।

Operatorअर्थa=5, b=3Result
==बराबर हैa == b0 (false)
!=बराबर नहीं हैa != b1 (true)
>बड़ा हैa > b1 (true)
<छोटा हैa < b0 (false)
💡 Common गलती: = vs ==

= assignment है (value set करता है)। == comparison है (equality check करता है)। if (a = 5) की जगह if (a == 5) लिखना एक classic bug है!

3. Logical Operators

Multiple conditions combine करने के लिए use होते हैं।

OperatorनामExampleResult
&&AND (और)1 && 00 — दोनों true होने चाहिए
||OR (या)1 || 01 — कोई एक true हो
!NOT (नहीं)!10 — value को reverse करता है

4. Assignment Operators

OperatorExampleइसके बराबर
+=a += 3a = a + 3
-=a -= 2a = a - 2
*=a *= 4a = a * 4
/=a /= 2a = a / 2

5. Increment और Decrement Operators

C Language
int a = 5;
a++;   // Post-increment: पहले use करो, फिर बढ़ाओ (5 print होगा, फिर 6)
++a;   // Pre-increment: पहले बढ़ाओ, फिर use करो (7 print होगा)
a--;   // Post-decrement
--a;   // Pre-decrement

6. Ternary Operator ( ? : )

if-else का short form। Syntax: condition ? true_value : false_value

C Language
int marks = 75;
char *result = (marks >= 40) ? "Pass" : "Fail";
printf("Result: %s\n", result); // Output: Pass

int a = 10, b = 20;
int max = (a > b) ? a : b;
printf("Max: %d\n", max); // Output: 20

Operator Precedence (Priority)

जब expression में multiple operators हों, precedence तय करती है कि पहले कौन evaluate होगा। याद रखें: * / की priority + - से ज़्यादा है।

Example: 2 + 3 * 43 * 4 = 12 पहले, फिर 2 + 12 = 14

Parentheses use करें: (2 + 3) * 4 = 20

सारांश

  • Arithmetic: + - * / % — basic गणित
  • Relational: == != > < — values compare करते हैं
  • Logical: && || ! — conditions combine करते हैं
  • Assignment: += -= *= — shorthand assignment
  • Ternary: ? : — एक-line if-else
  • = और == को confuse न करें!
🏋️ Practice Exercise

दो numbers input करके program लिखें जो: sum, difference, product, quotient, remainder, कौन बड़ा है (ternary से), और sum even है या odd (% से) print करे।

← Back to C Tutorial