C Programming

Chapter 3: Operators and Expressions in C Language

Chapter 3: Operators and Expressions in C Language

In the previous chapters, you learned about the introduction to C, variables, constants, data types, input, and output. In this chapter, you will learn about operators and expressions, which are used to perform calculations, comparisons, and logical operations in C programs.

3.1 What is an Operator?

An operator is a symbol that performs a specific operation on one or more values or variables.

For example:

int sum = 10 + 20;

Here:

10 and 20 are operands

+ is an operator

sum stores the result

Output:

30

3.2 Types of Operators in C

C provides several types of operators:

Arithmetic Operators

Relational Operators

Logical Operators

Assignment Operators

Increment and Decrement Operators

Conditional Operator

Bitwise Operators

Special Operators

3.3 Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorOperationExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remaindera % b

Example

#include <stdio.h> int main() {    int a = 20;    int b = 6;    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("Remainder = %d\n", a % b);    return 0; }

Output

Addition = 26 Subtraction = 14 Multiplication = 120 Division = 3 Remainder = 2

Note: When two integers are divided, the fractional part is generally discarded.

3.4 Addition Operator +

The addition operator is used to add two values.

#include <stdio.h> int main() {    int a = 15;    int b = 25;    int sum = a + b;    printf("Sum = %d", sum);    return 0; }

Output

Sum = 40

3.5 Subtraction Operator -

The subtraction operator finds the difference between two values.

#include <stdio.h> int main() {    int a = 50;    int b = 20;    printf("Difference = %d", a - b);    return 0; }

3.6 Multiplication Operator *

The multiplication operator multiplies two values.

#include <stdio.h> int main() {    int a = 10;    int b = 5;    printf("Product = %d", a * b);    return 0; }

Output

Product = 50

3.7 Division Operator /

The division operator divides one value by another.

#include <stdio.h> int main() {    int a = 20;    int b = 4;    printf("Result = %d", a / b);    return 0; }

Output

Result = 5

Decimal Division

To get a decimal result, use floating-point values.

#include <stdio.h> int main() {    float a = 10;    float b = 4;    printf("Result = %.2f", a / b);    return 0; }

Output

Result = 2.50

3.8 Modulus Operator %

The modulus operator returns the remainder after division.

#include <stdio.h> int main() {    int a = 17;    int b = 5;    printf("Remainder = %d", a % b);    return 0; }

Output

Remainder = 2

The modulus operator is commonly used with integer values.

3.9 Relational Operators

Relational operators are used to compare two values.

The result of a comparison is usually:

1 → True

0 → False

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

#include <stdio.h> int main() {    int a = 10;    int b = 20;    printf("%d\n", a == b);    printf("%d\n", a != b);    printf("%d\n", a < b);    printf("%d\n", a > b);    return 0; }

Output

0 1 1 0

3.10 Difference Between = and ==

This is an important concept in C programming.

Assignment Operator =

Used to assign a value.

int age = 18;

Equality Operator ==

Used to compare two values.

age == 18

Example:

int a = 10; int b = 10; printf("%d", a == b);

Output:

1

3.11 Logical Operators

Logical operators are used to combine or reverse conditions.

OperatorMeaning
&&Logical AND
` 
!Logical NOT

Logical AND &&

The result is true only when both conditions are true.

#include <stdio.h> int main() {    int age = 20;    printf("%d", age >= 18 && age <= 60);    return 0; }

Output:

1

Logical OR ||

The result is true when at least one condition is true.

#include <stdio.h> int main() {    int marks = 40;    printf("%d", marks >= 40 || marks == 35);    return 0; }

Output:

1

Logical NOT !

The NOT operator reverses the result of a condition.

#include <stdio.h> int main() {    int age = 15;    printf("%d", !(age >= 18));    return 0; }

Output:

1

3.12 Assignment Operators

Assignment operators are used to assign or update values in variables.

OperatorExampleEquivalent To
=a = 10Assign 10
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5

Example

#include <stdio.h> int main() {    int number = 10;    number += 5;    printf("%d", number);    return 0; }

Output

15

3.13 Increment Operator ++

The increment operator increases the value of a variable by 1.

#include <stdio.h> int main() {    int number = 10;    number++;    printf("%d", number);    return 0; }

Output:

11

3.14 Decrement Operator --

The decrement operator decreases the value of a variable by 1.

#include <stdio.h> int main() {    int number = 10;    number--;    printf("%d", number);    return 0; }

Output:

9

3.15 Pre-Increment and Post-Increment

There are two common forms of increment.

Pre-Increment

++a;

The value is incremented before it is used in the expression.

Post-Increment

a++;

The current value is used first, and then the variable is incremented.

Example:

#include <stdio.h> int main() {    int a = 5;    printf("%d\n", a++);    printf("%d", a);    return 0; }

Output:

5 6

3.16 Expressions in C

An expression is a combination of:

Variables

Constants

Operators

Function calls

that produces a value.

Example:

sum = a + b;

Here:

a + b

is an expression.

3.17 Operator Precedence

When an expression contains multiple operators, C follows rules to determine which operation is performed first.

Example:

int result = 10 + 5 * 2;

Multiplication is performed before addition.

10 + 10 = 20

So:

Result = 20

Using Parentheses

Parentheses can change the order of evaluation.

int result = (10 + 5) * 2;

Now addition is performed first.

15 × 2 = 30

3.18 Conditional Operator ?:

The conditional operator is a short form of a simple decision.

Syntax

condition ? expression1 : expression2;

Example

#include <stdio.h> int main() {    int age = 20;    age >= 18        ? printf("Eligible")        : printf("Not Eligible");    return 0; }

Output:

Eligible

3.19 Simple Calculator Program

#include <stdio.h> int main() {    int a, b;    printf("Enter first number: ");    scanf("%d", &a);    printf("Enter second number: ");    scanf("%d", &b);    printf("\nAddition = %d", a + b);    printf("\nSubtraction = %d", a - b);    printf("\nMultiplication = %d", a * b);    if (b != 0)    {        printf("\nDivision = %d", a / b);        printf("\nRemainder = %d", a % b);    }    return 0; }

3.20 Important Points to Remember

Operators are symbols used to perform operations.

Arithmetic operators perform mathematical calculations.

Relational operators compare values.

Logical operators combine conditions.

= is used for assignment.

== is used for comparison.

++ increases a value by 1.

-- decreases a value by 1.

% returns the remainder after division.

Parentheses can change the order of evaluation.

An expression produces a value.

Chapter Summary

In this chapter, you learned about operators and expressions in C. Operators are essential for performing calculations, comparing values, updating variables, and creating logical conditions.

You studied:

Arithmetic operators

Relational operators

Logical operators

Assignment operators

Increment and decrement operators

Conditional operator

Expressions

Operator precedence

These concepts will be used extensively in the next topics, especially decision-making statements and loops.

Practice Questions

Very Short Answer Questions

What is an operator?

What is an operand?

Which operator is used to find the remainder?

What is the difference between = and ==?

What does the && operator represent?

What does the || operator represent?

What is the purpose of the ++ operator?

What is an expression?

Short Answer Questions

Explain arithmetic operators with examples.

Explain relational operators.

What are logical operators? Explain &&, ||, and !.

Explain assignment operators with examples.

Differentiate between pre-increment and post-increment.

Explain operator precedence.

Programming Questions

1. Write a program to add two numbers.

#include <stdio.h> int main() {    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);    printf("Sum = %d", a + b);    return 0; }

2. Write a program to calculate the remainder.

#include <stdio.h> int main() {    int a, b;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);    printf("Remainder = %d", a % b);    return 0; }

3. Write a program to increase a number by 1.

#include <stdio.h> int main() {    int number;    printf("Enter a number: ");    scanf("%d", &number);    number++;    printf("After increment = %d", number);    return 0; }

4. Write a program to find the greater of two numbers using the conditional operator.

#include <stdio.h> int main() {    int a, b, greater;    printf("Enter two numbers: ");    scanf("%d %d", &a, &b);    greater = (a > b) ? a : b;    printf("Greater number = %d", greater);    return 0; }