Operators in C

Test on C operators

Operators combine, compare or modify values. The main beginner groups are arithmetic operators, relational operators, logical operators, assignment operators, and increment/decrement operators.

Arithmetic Operators Top ↑

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Integer remainder
#include <stdio.h>

int main(void) {
    int x = 30;
    int y = 20;

    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);
    printf("x %% y = %d\n", x % y);
    return 0;
}

Output

x + y = 50
x - y = 10
x * y = 600
x / y = 1
x % y = 10

Because both operands in x / y are integers, this example uses integer division.

Increment, Decrement and Compound Assignment Top ↑

int i = 45;
i++;       // 46
i += 5;    // 51
i--;       // 50

Compound assignment such as i += 5 means i = i + 5.

Post-Increment vs Pre-Increment Top ↑

int i = 45;
int a = i++;  // a gets 45, then i becomes 46

int j = 45;
int b = ++j;  // j becomes 46, then b gets 46

Key change: the position of ++ changes whether increment happens before or after the value of the expression is produced.

Relational Operators Top ↑

OperatorMeaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
int x = 30;
int y = 20;

printf("%d\n", x == y);  // 0
printf("%d\n", x > y);   // 1
printf("%d\n", x != y);  // 1

Comparison and logical expressions produce 0 when false and a nonzero value when true; the operators shown here produce either 0 or 1.

Logical Operators Top ↑

OperatorMeaning
&&Logical AND
||Logical OR
!Logical NOT
int x = 40;
int y = 50;

printf("%d\n", (x == 40) && (y == 30));  // 0
printf("%d\n", (x == 40) || (y == 30));  // 1
printf("%d\n", !(x == 40));              // 0

Practice Questions Top ↑

  1. Predict the result of 50 / 4 when both values are integers.
  2. Compare the results of i++ and ++i when their values are assigned to another variable.
  3. Write a condition that is true only when age >= 18 and has_id == 1.
  4. Change the relational examples above and verify each output.




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer