What are different operators and expressions used in C language?



Operators are special symbols in C that performs an operation on values and variables. These special symbols allow us to manipulate data and variables in different ways. Those Operators are classified into the following −

  • Arithmetic operators.
  • Relational operators.
  • Logical operators.
  • Assignment operators.
  • Increment and decrement operators.
  • Bitwise operators.
  • Conditional operators.
  • Special operators.

Expressions in C is a combination of variables, operators, and values that generates a single result. There are two types of expression those are below:

  • Simple Expression
  • Complex Expression

Let's know about Expression First, then we will cover Operators in brief.

Simple Expression in C

A simple expression is a mix of one or more values (operands) and symbols (operators) that gives us one result. It does not have complicated parts like functions or nested expressions. Simple expressions usually have just one math or logic operation.

Below is an example of a Simple Expression in C:

#include <stdio.h>

int main(){
   int a = 3;      // Operand
   int b = 6;      // Operand
   int sum = a + b; // Simple Expression adds up a and b
   printf("The sum is: %d
", sum); // Print the result. return 0; }

Complex Expression in C

A complex expression is an expression that includes multiple operators and operands. so it can have arithmetic, logical, relational, bit-wise operations to get result. It also contain parentheses to maintain correct order.

Below is an example of complex expression in C.

#include <stdio.h>

int main() {
   int a = 5;      // Operand
   int b = 10;     // Operand
   int c = 2;      // Operand
   int result;     // Variable to store the result
        
   // Complex expression: (a + b) * c - (b / a)
   result = (a + b) * c - (b / a);
        
   printf("The result of the complex expression is: %d
", result); // Print the result return 0; }

Below is the detailed overview of Types of Operator in C.

Arithmetic operator

Arithmetic operators are used for numerical calculations (or) to perform arithmetic operations like addition, subtraction etc. below is table that will you more understand

Operator Description Example a=20,b=10 Output
+ Addition a+b 20+10 30
- subtraction a-b 20-10 10
* multiplication a*b 20*10 200
/ Division a/b 20/10 2(quotient)
% Modular Division a%b 20%10 0 (remainder)

Following is the C program for arithmetic operator ?

#include<stdio.h>
void main (){
   int a= 20, b = 10;
   printf (" %d", a+b);
   printf (" %d", a-b);
   printf (" %d", a*b);
   printf (" %d", a/b);
   printf (" %d", a%b);
}

Output

When the above program is executed, it produces the following result ?

30
10
200
20

Relational operators

Relational operators are used for comparing two expressions such as greater, lesser, equal, not equal. The table below provides more clarity.

Operator Description Example a=20,b=10 Output
< less than a<b 10<20 1
<= less than (or) equal to a<=b 10<=20 1
> greater than a>b 10>20 0
>= greater than (or) equal to a>=b 10>=20 0
== equal to a==b 10==20 0
!= not equal to a!=b 10!=20 1

The output of a relational expression is either true (1) (or) false (0).

Following is the C program for the relational operator ?

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

Output

When the above program is executed, it produces the following result ?

1 1 0 0 0 1 1 1

Logical Operators

Logical operators are used to combine 2 (or) more expressions logically. hese are like special tools that help in making decisions in our programs based on true or false answers. Imagine you're playing a game where you need to check if certain conditions are met to win. For example, you might need to know if you have enough points and if you have collected a special item. Logical operators help us test these situations together, so we can decide what to do next in our program.

There are three types of logical operators. they are as follows:

  • Logical AND (&&)
  • Logical OR ( || )
  • Logical NOT (!)

Logical AND(&&)

The Logical AND operator (&&) is a fundamental tool in programming that allows us to check whether two conditions are true at the same time. Imagine you have two questions, and you want to know if both answers are "yes." If they are, then the result is true. If any of both answer is "no," then the result is false.

The table below shows how AND operator works.

exp1 exp2 exp1&&exp2
T T T
T F F
F T F
F F F

Logical OR(||)

The Logical OR operator (||) is helpful in programming. which allows us to check if at least one of two conditions is true. Think of it like asking two questions and wanting to know if either one of them gets a "yes" answer. If either condition is true, the overall result is true. If both are false, then the result is false.

The table below provide more details about OR(||).

exp1 exp2 exp1||exp2
T T T
T F T
F T T
F F F

Logical NOT(!)

The Logical NOT operator (!) is a unique tool in programming that helps us to flip the truth value of a particular condition. You can think of it as a switch that turns "yes" into "no" or "true" into "false." If something is true, using the Logical NOT operator will make it false, and if something is false, it will become true.

The table below provides more details about NOT operator

exp !exp
T F
F T

Logical operator summary with examples

Operator Description Example a=20,b=10 Output
&& logical AND (a>b)&&(a<c) (10>20)&&(10<30) 0
|| logical OR (a>b)||(a<=c) (10>20)||(10<30) 1
! logical NOT !(a>b) !(10>20) 1

Following is the C program for the logical operator ?

#include<stdio.h>
void main (){
   int a= 10, b = 20, c= 30;
   printf (" %d", (a>b) && (a<c));
   printf (" %d", (a>b) || (a<c));
   printf (" %d", ! (a>b));
}

Output

When the above program is executed, it produces the following result ?

0 1 1

Assignment operators

Assignment operators are symbols that help us give values to variables. They are used when we want to store a value in a variable. The types of assignment operators are ?

  • Simple assignment.
  • Compound assignment.

Simple Assignment

The simple assignment operator (=) is used to give a value to a variable. It takes the value on the right side and puts it in the variable on the left side. It's like putting a number or word inside a particular box.

Compound Assignment

The compound assignment operators do two things at once. Firstly, they do a math operation like adding, subtracting, multiplying, or dividing, and then they store the result back in the very same variable.

The table below has the assignment operator and their Example as well ?

Operator Description Example
= simple assignment a=10
+=,-=,*=,/=,%= compound assignment a+=10"a=a+10
a=10"a=a-10

Following is the C program for assignment operator ?

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

Output

When the above program is executed, it produces the following result ?

10
20

Increment and decrement operator

Let us understand what is an increment operator.

Increment operator (++)

This operator increments the value of a variable by 1

The two types include ?

  • pre increment
  • post increment

If we place the increment operator before the operand, then it is pre-increment. Later on, the value is first incremented and the next operation is performed on it.

For example,

z = ++a; // a= a+1
z=a

If we place the increment operator after the operand, then it is post-increment and the value is incremented after the operation is performed.

For example,

z = a++; // z=a
a= a+1

Example

Following is the C program for increment operator ?

#include <stdio.h>
void main() {
   int a= 10, z;
   z= ++a;
   printf("z=%d", z);
   printf("
a=%d", a); z= a++; printf("
z=%d", z); printf("
a=%d", a); }

Output

When the above program is executed, it produces the following result ?

z=11
a=11
z=11
a=12

Decrement operator ? (- -)

It is used to decrement the values of a variable by 1.

The two types are ?

  • pre decrement
  • post decrement

If the decrement operator is placed before the operand, then it is called pre-decrement. Here, the value is first decremented and then, operation is performed on it.

For example,

z = - - a; // a= a-1
z=a

If the decrement operator is placed after the operand, then it is called post-decrement. Here, the value is decremented after the operation is performed.

For example,

z = a--; // z=a
a= a-1

Following is the C program for decrement operator ?

#include <stdio.h>

void main() {
   int a=10, z;
   z= --a;
   printf("z=%d", z);
   printf("
a=%d", a); z= a--; printf("
z=%d", z); printf("
a=%d", a); }

Output

When the above program is executed, it produces the following result ?

z=9
a=9
z=9
a=8

Bitwise Operator

Bitwise operators work directly with the binary (bit-level) form of numbers. In computers, numbers are stored as a series of 0s and 1s, called bits. Bitwise operators help us manipulate these bits directly, which can be very useful for certain types of programming tasks.

Types of Bitwise Operators are listed below in the table

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left Shift
>> Right shift
~ One's Complement

Bitwise AND (&) in C

Bitwise AND (&) operator compares two numbers bit by bit from the right side. If both bits are 1 result will be 1, If any of both bits is 0 result will be 0.

Example code:

5 & 3;  // In binary: 101 & 011 = 001 (Result is 1)

Output:

1
Bitwise AND
a b a&b
0 0 0
0 1 0
1 0 0
1 1 1


Bitwise OR(|)

This operator compares number bit by bit. if any of bits is 1 result will be 1. If both bit are 0 result will be 0.

Example code:

5 | 3;  // In binary: 101 | 011 = 111 (Result is 7)

Output:

7
Bitwise OR
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1


Bitwise XOR(^)

This operator compares two numbers bit by bit. If both bits are 1 or both are 0, the result will be 0; if they are different, the result is 1.

Example code:

5 ^ 3;  // In binary: 101 ^ 011 = 110 (Result is 6)

Output:

6
Bitwise XOR
a b a^b
0 0 0
0 1 1
1 0 1
1 1 0

Left Shift

If the value is left shifted one time, then its value gets doubled. it's like we are multiplying the number by 2 each time we do left shift.

For example, a = 10, then a<<1 = 20

Right shift

If the value of a variable is right-shifted one time, then its value becomes half the original value. it's opposite to left shift here it's just like we are dividing a number by 2 each time we do the right shift.

For example, a = 10, then a>>1 = 5

Ones complement

It converts all ones to zeros and zeros to ones.

For example, a = 5, then ~a=2 [only if 4 bits are considered].

Following is another C program for bitwise operator ?

#include<stdio.h>
void main (){
   int a= 20, b = 10,c=10;
   printf (" %d", a<<1);
   printf (" %d", b>>1);
   printf (" %d", ~c);
}

Output

When the above program is executed, it produces the following result ?

40
5
11

Signed

1's complement = - [ give no +1]

For example, ~10 = - [10+1] = -11

~-10 = - [-10+1] = 9

Unsigned

1's complement = [65535 - given no]

Conditional operator (? :)

The conditional operator, also known as the ternary operator, is a simple and easy way to make decisions in programming. It allows us to choose one value from two options based on a particular condition.

The syntax for ternary is as follows ?

exp1? exp2: exp3

If exp1 is true, exp2 is evaluated. Otherwise, exp3 is evaluated. Or in the form of if-else.

if (exp1)
   exp2;
else
   exp3;

Following is the C program for the conditional operator ?

#include<stdio.h>
void main (){
   int z;
   z = (5>3) ? 1:0;
   printf ("%d",z);
}

Output

When the above program is executed, it produces the following result ?

Special operations

Some of the special operations are comma, ampersand (&), size of operators.

  • Comma ( , ) ? It is used as separator for variables. For example; a=10, b=20
  • Address (&) ? It get the address of a variables.
  • Size of ( ) ? It is used to get the size of a data type of a variable in bytes.

Following is the C program for special operations ?

#include <stdio.h>

int main() {
    int a = 10;
    float b = 20;

    printf("a = %d, b = %.1f
", a, b); printf("a address = %p
", (void*)&a); printf("b address = %p
", (void*)&b); printf("a size = %ld
", sizeof(a)); printf("b size = %ld
", sizeof(b)); return 0; }

Output

When the above program is executed, it produces the following result ?

a=10 b=20.00
Address of a =1 2 3 4
Address of b = 5 6 7 8 Only for this example
Size of a = 4 bytes
Size of b = 4 bytes
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2024-11-12T10:37:28+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements