Data Types, Variables, and Operators

Understanding basic data types (int, float, char, etc.), declaring variables, and using arithmetic, relational, and logical operators.


C Operators Explained

Operators in C

Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. C language supports a rich set of built-in operators.

Types of Operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Increment and Decrement Operators
  • Conditional Operator
  • Special Operators

Using Arithmetic, Relational, and Logical Operators in C

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. Common arithmetic operators in C include:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo - remainder of division)

Example: Arithmetic Operators

 #include <stdio.h>

int main() {
    int a = 10;
    int b = 5;

    printf("a + b = %d\n", a + b);  // Output: 15
    printf("a - b = %d\n", a - b);  // Output: 5
    printf("a * b = %d\n", a * b);  // Output: 50
    printf("a / b = %d\n", a / b);  // Output: 2
    printf("a %% b = %d\n", a % b);  // Output: 0

    // Integer division truncates.  10 / 3 = 3
    printf("10 / 3 = %d\n", 10 / 3); // Output: 3

    // To get a floating-point result, cast one of the operands to float or double.
    printf("10.0 / 3 = %f\n", 10.0 / 3); // Output: 3.333333
    printf("10 / 3.0 = %f\n", 10 / 3.0); // Output: 3.333333
    printf("(float)10 / 3 = %f\n", (float)10 / 3); // Output: 3.333333

    return 0;
} 

Relational Operators

Relational operators are used to compare two values. The result of a relational operation is either true (1) or false (0).

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Example: Relational Operators

 #include <stdio.h>

int main() {
    int x = 5;
    int y = 10;

    printf("x == y: %d\n", x == y);  // Output: 0 (false)
    printf("x != y: %d\n", x != y);  // Output: 1 (true)
    printf("x > y: %d\n", x > y);    // Output: 0 (false)
    printf("x < y: %d\n", x < y);    // Output: 1 (true)
    printf("x >= y: %d\n", x >= y);   // Output: 0 (false)
    printf("x <= y: %d\n", x <= y);   // Output: 1 (true)

    return 0;
} 

Logical Operators

Logical operators are used to combine or negate logical expressions. They also return either true (1) or false (0).

  • && (Logical AND) - True if both operands are true.
  • || (Logical OR) - True if at least one operand is true.
  • ! (Logical NOT) - True if the operand is false, and vice versa.

Example: Logical Operators

 #include <stdio.h>

int main() {
    int a = 1;  // Representing true
    int b = 0;  // Representing false

    printf("a && b: %d\n", a && b);  // Output: 0 (false)
    printf("a || b: %d\n", a || b);  // Output: 1 (true)
    printf("!a: %d\n", !a);        // Output: 0 (false)
    printf("!b: %d\n", !b);        // Output: 1 (true)

    // Combination with Relational Operators
    int x = 5;
    int y = 10;

    printf("(x < 10) && (y > 5): %d\n", (x < 10) && (y > 5)); // Output: 1 (true)
    printf("(x > 10) || (y < 5): %d\n", (x > 10) || (y < 5)); // Output: 0 (false)

    return 0;
}