C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program. Usually, operators take part in a program for manipulating data and variables and form a part of the mathematical, conditional, or logical expressions.

In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical functions. It is a symbol that operates on a value or a variable. For example, + and - are the operators to perform addition and subtraction in any C program. C has many operators that almost perform all types of operations. These operators are really useful and can be used to perform every operation.

Additionally, you can also learn more about the uses of C language.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Arithmetic Operator With Example

Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and variables).

The following table provided below shows all the arithmetic operators supported by the C language for performing arithmetic operators.

Operator

Description

  • (Addition)

It adds two operands

− (Subtraction)

It subtracts second operand from the first

* (Multiplication)

It multiplies both operands

/ (Division)

It is responsible for dividing numerator by the denomerator

% (Modulus)

This operator gives the remainder of an integer after division

Let’s look at an example of arithmetic operations in C below assuming variable a holds 7 and variable b holds 5.

// Examples of arithmetic operators in C

#include <stdio.h>

int main()

{

    int a = 7,b = 5, c;    

    c = a+b;

    printf("a+b = %d \n",c);

    c = a-b;

    printf("a-b = %d \n",c);

    c = a*b;

    printf("a*b = %d \n",c);

    c = a/b;

    printf("a/b = %d \n",c);

    c = a%b;

    printf("Remainder when a is divided by b = %d \n",c);   

    return 0;

}

Output:

a+b = 12

a-b = 2

a*b = 35

a/b = 1

Remainder when a divided by b = 2

The operators shown in the program are +, -, and * that computes addition, subtraction, and multiplication respectively. In normal calculation, 7/5 = 1.4. However, the output is 1 in the above program. The reason behind this is that both the variables a and b are integers. Hence, the output should also be an integer. So, the compiler neglects the term after the decimal point and shows 2 instead of 2.25 as the output of the program.

A modulo operator can only be used with integers. 

Using modulo operator (%), you can compute the remainder of any integer. When a=7 is divided by b=5, the remainder is 2. If we want the result of our division operator in decimal values, then either one of the operands should be a floating-point number. 

Suppose a = 7.0, b = 2.0, c = 5, and d = 3, the output will be:

// When either one of the operands is a floating-point number

a/b = 3.50  

a/d = 2.33 

c/b = 1.66  

// when both operands are integers 

c/d = 1

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Increment/Decrement Operator With Example

C programming has basically two operators which can increment ++ and decrement -- the value of a variable. It can change the value of an operand (constant or variable) by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation. These two operators are unary operators, which means they can only operate on a single operand. For example, ++x and x++ means x=x+1 or --x and x−− means x=x-1. 

There is a slight distinction between ++ or −− when written before or after any operand. 

If we use the operator as a pre-fix, it adds 1 to the operand, and the result is assigned to the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to the variable on the left i.e., it first returns the original value, and then the operand is incremented by 1.

Operator

Description

++

This increment operator increases the integer value by 1.

--

This decrement operator decreases the integer value by 1.

Here is an example demonstrating the working of increment and decrement operator:

// Examples of increment and decrement operators

#include <stdio.h>

int main()

{

    int a = 11, b = 90;

    float c = 100.5, d = 10.5;

    printf("++a = %d \n", ++a);

    printf("--b = %d \n", --b);

    printf("++c = %f \n", ++c);

    printf("--d = %f \n", --d);

    return 0;

}

 Output:

++a = 12

--b = 89

++c = 101.500000

--d = 9.500000

In the above code example, the increment and decrement operators ++ and -- have been used as prefixes. Note that these two operators can also be used as postfixes like a++ and a-- when required.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Assignment Operator With Example

An assignment operator is mainly responsible for assigning a value to a variable in a program. Assignment operators are applied to assign the result of an expression to a variable. This operator plays a crucial role in assigning the values to any variable. The most common assignment operator is =. 

C language has a collection of shorthand assignment operators that can be used for C programming. The table below lists all the assignment operators supported by the C language:

Operator

Description

Example

=

Assign

Used to assign the values from right side of the operands to left side of the operand.

C = A + B will assign the value of A + B to C.

+=

Add then assign

Adds the value of the right operand to the value of the left operand and assigns the result to the left operand.

C += A is same as C = C + A

-=

Subtract then assign

Subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.

C -= A is same as C = C - A

*=

Multiply then assign

Multiplies the value of the right operand with the value of the left operand and assigns the result to the left operand.

C *= A is same as C = C * A

/=

Divide then assign

Divides the value of the left operand with the value of the right operand and assigns the result to the left operand.

C /= A is same as C = C / A

%=

Modulus then assign

Takes modulus using the values of the two operands and assigns the result to the left operand.

C %= A is same as C = C % A

<<=

Left shift and assign

Used for left shift AND assignment operator.

C <<= 4 is same as C = C << 4

>>=

Right shift and assign

Used for right shift AND assignment operator.

C >>= 5 is same as C = C >> 5

&=

Bitwise AND assign

Used for bitwise AND assignment operator.

C &= 7 is same as C = C & 7

^=

Used for bitwise exclusive OR and assignment operator.

C ^= 6 is same as C = C ^ 6

|=

Used for bitwise inclusive OR and assignment operator.

C |= 9 is same as C = C | 9

The below example explains the working of assignment operator.

// Examples of assignment operators

#include <stdio.h>

int main()

{

    int a = 7, b;

    b = a;      // b is 7

    printf("b = %d\n", b);

    b += a;     // b is 14 

    printf("b = %d\n", b);

    b -= a;     // b is 7

    printf("b = %d\n", b);

    b *= a;     // b is 49

    printf("b = %d\n", b);

    b /= a;     // b is 7

    printf("c = %d\n", c);

    b %= a;     // b = 0

    printf("b = %d\n", b);

    return 0;

}

Output:

b = 7

b = 14 

b = 7

b = 49 

b = 7

b = 0

Free Course: Programming Fundamentals

Learn the Basics of ProgrammingEnroll Now
Free Course: Programming Fundamentals

Relational Operator With Example

Relational operators are specifically used to compare two quantities or values in a program. It checks the relationship between two operands. If the given relation is true, it will return 1 and if the relation is false, then it will return 0. Relational operators are heavily used in decision-making and performing loop operations.

The table below shows all the relational operators supported by C. Here, we assume that the variable A holds 15 and the variable B holds the 25.

Operator

Description

Example

==

It is used to check if the values of the two operands are equal or not. If the values of the two operands are equal, then the condition becomes true.

(A == B) is not true.

!=

It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true.

(A != B) is true.

>

It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true.

(A > B) is not true.

<

It is used to check if the value of left operand is less than the value of right operand. If the left operand is lesser, then the condition becomes true.

(A < B) is true.

>=

It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true.

(A >= B) is not true.

<=

It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true.

(A <= B) is true.

Below is an example showing the working of the relational operator:

// Example of relational operators

#include <stdio.h>

int main()

{

    int x = 8, y = 10;

   printf("%d == %d is False(%d) \n", x, y, x == y);

   printf("%d != %d is True(%d) \n ", x, y, x != y);

   printf("%d > %d is False(%d)\n ",  x, y, x > y);

   printf("%d < %d is True (%d) \n", x, y, x < y);

   printf("%d >= %d is False(%d) \n", x, y, x >= y);

   printf("%d <= %d is True(%d) \n", x, y, x <= y);

    return 0;

}

Output:

8 == 10 is False(0)

8 != 10 is True(1)

8 > 10 is False(0)

8 < 10 is True(1)

8 >= 10 is False(0)

8 <=10 is True(1) 

All the relational operators work in the same manner as described in the table above.

Logical Operator With Example

In the C programming language, we have three logical operators when we need to test more than one condition to make decisions. These logical operators are: 

  • && (meaning logical AND)
  • || (meaning logical OR)  
  • ! (meaning logical NOT)

An expression containing a logical operator in C language returns either 0 or 1 depending upon the condition whether the expression results in true or false. Logical operators are generally used for decision-making in C programming.

The table below shows all the logical operators supported by the C programming language. We are here assuming that the variable A holds 7 and variable B holds 3.

Operator

Description

Example

&&

This is the AND operator in C programming language. It performs logical conjunction of two expressions. (If both expressions evaluate to True, then the result is True. If either of the expression evaluates to False, then the result is False)

((A==7) && (B>7)) equals to 0

||

It is the NOT operator in C programming language. It performs a logical disjunction on two expressions. (If either or both of the expressions evaluate to True, then the result is True)

((A==7) || (B>7)) equals to 1

!

It is the Logical NOT Operator in C programming language. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false and vice versa.

!(A && B) is true

Following is the example that easily elaborates the working of the relational operator:

// Working of logical operators

#include <stdio.h>

int main()

{

    int a = 15, b = 15, c = 20, results;

    results = (a == b) && (c > b);

    printf("(a == b) && (c > b) is %d \n", results);

    results = (a == b) && (c < b);

    printf("(a == b) && (c < b) is %d \n", results);

    results = (a == b) || (c < b);

    printf("(a == b) || (c < b) is %d \n", results);

    results = (a != b) || (c < b);

    printf("(a != b) || (c < b) is %d \n", results);

    results = !(a != b);

    printf("!(a != b) is %d \n", results);

    results = !(a == b);

    printf("!(a == b) is %d \n", results);

    return 0;

}

Output:

(a == b) && (c > b) is 1 

(a == b) && (c < b) is 0 

(a == b) || (c < b) is 1 

(a != b) || (c < b) is 0 

!(a != b) is 1 

!(a == b) is 0 

  • (a == b) && (c > 15) evaluates to 1 because both the operands (a == b) and (c > b) are 1 (true).
  • (a == b) && (c < b) evaluates to 0 because of the operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because of the operand (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both the operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because the operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because the (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Bitwise Operator With Example

Bitwise operators are the operators which work on bits and perform the bit-by-bit operation. Mathematical operations like addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and easier to implement during computation and compiling of the program.

Bitwise operators are especially used in C programming for performing bit-level operations. C programming language supports a special operator for bit operation between two variables. 

The truth tables for &, |, and ^ is provided below:

p

q

p & q

p | q

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

Here, we will assume that A = 50 and B = 25 in binary format as follows.

A = 00110010

B = 00011001

-----------------

A&B = 00010000

A|B  = 00111011

A^B = 00101011

~A  = 11001101

The table provided below demonstrates the bitwise operators supported by C. Assume variable 'A' holds 50 and variable 'B' holds 25.

Operator

Description

Example

&

Binary AND Operator. 

It copies a bit to the result if it exists in both the operands.

(A & B) = 16, i.e. 00010000

|

Binary OR Operator. 

It copies a bit if and only if it exists in either operand.

(A | B) = 59, i.e. 00111011

^

Binary XOR Operator. 

It copies the bit only if it is set in one operand but not both.

(A ^ B) = 43, i.e. 00101011

~

Binary One's Complement Operator. 

It is unary and has the effect of 'flipping' bits.

(~A ) = ~(50), i.e,. -0111101

<<

Binary Left Shift Operator. 

The value of the left operands is moved left by the number of bits specified by the right operand.

A << 2 = 200 i.e. 11001000

>>

Binary Right Shift Operator. 

The value of the left operands is moved right by the number of bits specified by the right operand.

A >> 2 = 12 i.e., 00001100

Misc Operator With Example

Besides all the other operators discussed above, the C programming language also offers a few other important operators including sizeof, comma, pointer(*), and conditional operator (?:).

Operator

Description

Example

sizeof()

The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

sizeof(a), where a is integer, will return 4.
sizeof(b), where b is float, will return 4.
sizeof(c), where c is double, will return 8.
sizeof(d), where d is integer, will return 1.

&

It returns the address of a memory location of a variable.

&a; returns the actual address of the variable.
It can be any address in the memory like 4, 70,104.

*

Pointer to a variable.

*a; It points to the value of the variable.

? :

conditional operator (?: in combination) to construct conditional expressions.

If Condition is true ? then value X : otherwise value Y will be returned as output.

Operator Precedence in C

Operator precedence is also one of the features in the C programming language which helps to determine the grouping of terms in an expression and decides how an expression is evaluated as per the provided expressions. Some operators have higher precedence than others and some have lower precedence than others. For example, in C Language, the multiplication operator has higher precedence than the addition operator.

Example:

For expression x = 7 + 4 * 2 , x is assigned 15 and not 22 because Multiplication operator * has higher precedence than the addition operator +. So, it first multiplies 4 with 2 and then adds 7 into the expression.

Provided below is a table for better understanding of operator precedence. As we can see that the operators with the highest precedence appear at the top of the table and those with the lowest precedence appear at the bottom of the table. Within an expression in a C program, operators with higher precedence will be evaluated first and the operators with lower precedence will be evaluated later.

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

In this article on Operators in C, we have illustrated almost all the Operators in C with proper examples. The article starts with a brief introduction to Operators in C followed by elaborating the various types of Operators in C. We have provided a brief overview of all the Operators in C programming language and explained the basic introduction of the arithmetic operator, increment/decrement operator, assignment operator, relational operator, logical operator, bitwise operator, special operator, and also the operator precedence. After the overview, we have also illustrated the topic with an example for a better understanding of the topic. Some other important operators under the heading miscellaneous operators which are very useful in C programming have been discussed as well. 

We hope through this article you could gain some knowledge on Operators in C and learned how we can use it in our software development projects.

To know more about the Operators in C, you can enroll in the Post-Graduate Program in Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to the Operators in C and other related concepts, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.