Operators in C Programming

Welcome to C Programming Tutorial

Your guide to getting started with C programming.

Operators

Arithmetic Operators: Used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    printf("a + b = %d\\n", a + b); // Addition
    printf("a - b = %d\\n", a - b); // Subtraction
    printf("a * b = %d\\n", a * b); // Multiplication
    printf("a / b = %d\\n", a / b); // Division
    printf("a %% b = %d\\n", a % b); // Modulus
    
    return 0;
}

Relational Operators: Used to compare two values.

#include <stdio.h>

int main() {
    int x = 10, y = 20;
    printf("x == y: %d\\n", x == y); // Equal to
    printf("x != y: %d\\n", x != y); // Not equal to
    printf("x > y: %d\\n", x > y);   // Greater than
    printf("x < y: %d\\n", x < y);   // Less than
    printf("x >= y: %d\\n", x >= y); // Greater than or equal to
    printf("x <= y: %d\\n", x <= y); // Less than or equal to
    
    return 0;
}

Logical Operators: Used to combine conditional statements.

#include <stdio.h>

int main() {
    int x = 10, y = 20;
    printf("x < 15 && y > 15: %d\\n", x < 15 && y > 15); // Logical AND
    printf("x < 15 || y < 15: %d\\n", x < 15 || y < 15); // Logical OR
    printf("!(x < 15): %d\\n", !(x < 15));              // Logical NOT
    
    return 0;
}

Assignment Operators: Used to assign values to variables.

#include <stdio.h>

int main() {
    int a = 10;
    a += 5;  // a = a + 5
    printf("a += 5: %d\\n", a);
    
    a -= 3;  // a = a - 3
    printf("a -= 3: %d\\n", a);
    
    a *= 2;  // a = a * 2
    printf("a *= 2: %d\\n", a);
    
    a /= 4;  // a = a / 4
    printf("a /= 4: %d\\n", a);
    
    return 0;
}
Previous Next
Modern Footer