Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Operators Arithmetic, Bitwise, Ternary: Tutorial, Examples, FAQs & Interview Tips

Arithmetic Operators

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer division)
%Modulus (remainder)10 % 31
++Incrementa++ or ++aa + 1
--Decrementa-- or --aa - 1

Relational Operators

OperatorNameExampleResult
==Equal to5 == 51 (true)
!=Not equal to5 != 31 (true)
>Greater than5 > 31 (true)
<Less than5 < 30 (false)
>=Greater than or equal5 >= 51 (true)
<=Less than or equal3 <= 51 (true)

Logical Operators

OperatorNameExampleResult
&&Logical AND(5>3) && (2<4)1 (both true)
||Logical OR(5>3) || (2>4)1 (one true)
!Logical NOT!(5>3)0 (negates true)

Bitwise Operators

OperatorNameExample (a=5, b=3)Result
&Bitwise AND5 & 3 (0101 & 0011)1 (0001)
|Bitwise OR5 | 3 (0101 | 0011)7 (0111)
^Bitwise XOR5 ^ 3 (0101 ^ 0011)6 (0110)
~Bitwise NOT~5-6
<<Left shift5 << 110
>>Right shift5 >> 12

Assignment Operators

OperatorEquivalentExample
=Assigna = 5
+=a = a + ba += 3
-=a = a - ba -= 3
*=a = a * ba *= 3
/=a = a / ba /= 3
%=a = a % ba %= 3
Arithmetic, Relational and Logical Operators
#include <stdio.h>

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

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

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

    // Relational
    printf("\n5 == 5: %d\n", 5 == 5);  // 1
    printf("5 != 3: %d\n",  5 != 3);  // 1
    printf("5 > 8:  %d\n",  5 > 8);   // 0

    // Logical
    int age = 20;
    printf("\nage >= 18 && age <= 60: %d\n", age >= 18 && age <= 60);  // 1
    printf("age < 18 || age > 60:   %d\n",  age < 18 || age > 60);   // 0
    printf("!(age == 20):           %d\n",  !(age == 20));            // 0

    return 0;
}
Bitwise, Ternary and sizeof Operators
#include <stdio.h>

int main() {
    int a = 5, b = 3;  // a = 0101, b = 0011 in binary

    // Bitwise operators
    printf("a & b  = %d\n", a & b);   // 1  (0001)
    printf("a | b  = %d\n", a | b);   // 7  (0111)
    printf("a ^ b  = %d\n", a ^ b);   // 6  (0110)
    printf("~a     = %d\n", ~a);      // -6
    printf("a << 1 = %d\n", a << 1); // 10 (multiply by 2)
    printf("a >> 1 = %d\n", a >> 1); // 2  (divide by 2)

    // Ternary operator: condition ? value_if_true : value_if_false
    int num = 7;
    char *result = (num % 2 == 0) ? "even" : "odd";
    printf("\n%d is %s\n", num, result);  // 7 is odd

    int max = (a > b) ? a : b;
    printf("Max of %d and %d is %d\n", a, b, max);  // 5

    // sizeof operator
    printf("\nsizeof(int):    %zu\n", sizeof(int));
    printf("sizeof(double): %zu\n", sizeof(double));
    printf("sizeof(a):      %zu\n", sizeof(a));  // same as sizeof(int)

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.