C Operators -

C Operators

C programming relies heavily on operators to perform various tasks, such as calculations, comparisons, and logic control. Operators simplify code, making it more efficient and easier to understand. In this post, we’ll explore operators with explanations and examples.


Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example:

#include <stdio.h>

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

Relational Operators

Relational operators compare two values and return true (1) or false (0).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

#include <stdio.h>

int main() {
    int x = 5, y = 10;
    printf("x == y: %d\n", x == y);
    printf("x != y: %d\n", x != y);
    printf("x > y: %d\n", x > y);
    printf("x < y: %d\n", x < y);
    return 0;
}

Logical Operators

Logical operators are used to combine multiple conditions.

OperatorDescriptionExample
&&Logical AND(a > b) && (c > d)
||Logical OR(a > b) || (c > d)
!Logical NOT!(a == b)

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;
    printf("(a < b) && (c > b): %d\n", (a < b) && (c > b));
    printf("(a > b) || (c > b): %d\n", (a > b) || (c > b));
    printf("!(a == b): %d\n", !(a == b));
    return 0;
}

Assignment Operators

These operators assign values to variables and can also perform operations while assigning.

OperatorDescriptionExample
=Assigna = b
+=Add and assigna += b
-=Subtract and assigna -= b
*=Multiply and assigna *= b
/=Divide and assigna /= b
%=Modulus and assigna %= b

Example:

#include <stdio.h>

int main() {
    int num = 10;
    num += 5;
    printf("After += operation: %d\n", num);
    return 0;
}

Bitwise Operators

Bitwise operators work at the bit level, allowing low-level manipulation of data.

OperatorDescriptionExample
&ANDa & b
|ORa | b
^XORa ^ b
~NOT~a
<<Left shifta << 2
>>Right shifta >> 2

Increment and Decrement Operators

These operators increase or decrease the value of a variable by 1.

OperatorDescriptionExample
++Increment by 1a++ or ++a
--Decrement by 1a-- or --a

Example:

#include <stdio.h>

int main() {
    int num = 5;
    printf("Original: %d\n", num);
    printf("After Increment: %d\n", ++num);
    printf("After Decrement: %d\n", --num);
    return 0;
}

Conditional (Ternary) Operator

This operator is a shorthand for the if-else statement.

Syntax:

condition ? expression1 : expression2;

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("The maximum value is: %d\n", max);
    return 0;
}

Special Operators

1. Comma Operator

The comma operator allows you to evaluate multiple expressions in a single statement.

Example:

#include <stdio.h>

int main() {
    int a, b, c;
    a = (b = 10, c = 20);
    printf("a: %d, b: %d, c: %d\n", a, b, c);
    return 0;
}

2. sizeof Operator

The sizeof operator returns the size of a data type or variable in bytes.

Example:

#include <stdio.h>

int main() {
    printf("Size of int: %lu bytes\n", sizeof(int));
    printf("Size of char: %lu bytes\n", sizeof(char));
    return 0;
}

Real-Life Example: Simple Calculator

#include <stdio.h>

int main() {
    char operator;
    double num1, num2;

    printf("Enter operator (+, -, *, /): ");
    scanf("%c", &operator);

    printf("Enter two operands: ");
    scanf("%lf %lf", &num1, &num2);

    switch(operator) {
        case '+':
            printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
            break;
        case '-':
            printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
            break;
        case '*':
            printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
            break;
        case '/':
            if (num2 != 0)
                printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
            else
                printf("Division by zero error!\n");
            break;
        default:
            printf("Invalid operator\n");
    }
    return 0;
}

Conclusion

Understanding operators in C is fundamental for developing powerful applications. From simple arithmetic to complex logical operations, mastering operators will significantly enhance your programming skills. For more programming tutorials, visit Master Coding Science at mastercodingscience.com.

Leave a Comment