Operators in C - GeeksforGeeks (2024)

In C language, operators are symbols that represent operations to be performed on one or more operands. They are the basic components of the C programming. In this article, we will learn about all the built-in operators in C with examples.

What is a C Operator?

An operator in C can be defined as the symbol that helps us to perform some specific mathematical, relational, bitwise, conditional, or logical computations on values and variables. The values and variables used with operators are called operands. So we can say that the operators are the symbols that perform operations on operands.

Operators in C - GeeksforGeeks (1)

For example,

c = a + b;

Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.

Types of Operators in C

C language provides a wide range of operators that can be classified into 6 types based on their functionality:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Other Operators

1. Arithmetic Operations in C

The arithmetic operators are used to perform arithmetic/mathematical operations on operands. There are 9 arithmetic operators in C language:

S. No.

Symbol

OperatorDescription

Syntax

1

+

Plus

Adds two numeric values.

a + b

2

Minus

Subtracts right operand from left operand.

a – b

3

*

Multiply

Multiply two numeric values.

a * b

4

/

Divide

Divide two numeric values.

a / b

5

%

Modulus

Returns the remainder after diving the left operand with the right operand.

a % b

6

+

Unary Plus

Used to specify the positive values.

+a

7

Unary Minus

Flips the sign of the value.

-a

8

++

Increment

Increases the value of the operand by 1.

a++

9

Decrement

Decreases the value of the operand by 1.

a–

Example of C Arithmetic Operators

C
// C program to illustrate the arithmatic operators#include <stdio.h>int main(){ int a = 25, b = 5; // using operators and printing results printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); printf("a % b = %d\n", a % b); printf("+a = %d\n", +a); printf("-a = %d\n", -a); printf("a++ = %d\n", a++); printf("a-- = %d\n", a--); return 0;}

Output

a + b = 30a - b = 20a * b = 125a / b = 5a % b = 0+a = 25-a = -25a++ = 25a-- = 26

2. Relational Operators in C

The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison.

These are a total of 6 relational operators in C:

S. No.

Symbol

OperatorDescription

Syntax

1

<

Less thanReturns true if the left operand is less than the right operand. Else falsea < b

2

>

Greater thanReturns true if the left operand is greater than the right operand. Else falsea > b

3

<=

Less than or equal toReturns true if the left operand is less than or equal to the right operand. Else falsea <= b

4

>=

Greater than or equal toReturns true if the left operand is greater than or equal to right operand. Else falsea >= b

5

==

Equal toReturns true if both the operands are equal.a == b

6

!=

Not equal toReturns true if both the operands are NOT equal.a != b

Example of C Relational Operators

C
// C program to illustrate the relational operators#include <stdio.h>int main(){ int a = 25, b = 5; // using operators and printing results printf("a < b : %d\n", a < b); printf("a > b : %d\n", a > b); printf("a <= b: %d\n", a <= b); printf("a >= b: %d\n", a >= b); printf("a == b: %d\n", a == b); printf("a != b : %d\n", a != b); return 0;}

Output

a < b : 0a > b : 1a <= b: 0a >= b: 1a == b: 0a != b : 1

Here, 0 means false and 1 means true.

3. Logical Operator in C

Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false.

S. No.

Symbol

OperatorDescription

Syntax

1

&&

Logical ANDReturns true if both the operands are true.

a && b

2

||

Logical ORReturns true if both or any of the operand is true.

a || b

3

!

Logical NOTReturns true if the operand is false.

!a

Example of Logical Operators in C

C
// C program to illustrate the logical operators#include <stdio.h>int main(){ int a = 25, b = 5; // using operators and printing results printf("a && b : %d\n", a && b); printf("a || b : %d\n", a || b); printf("!a: %d\n", !a); return 0;}

Output

a && b : 1a || b : 1!a: 0

4. Bitwise Operators in C

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing.

There are 6 bitwise operators in C:

S. No.

Symbol

OperatorDescription

Syntax

1

&

Bitwise ANDPerforms bit-by-bit AND operation and returns the result.a & b

2

|

Bitwise ORPerforms bit-by-bit OR operation and returns the result.a | b

3

^

Bitwise XORPerforms bit-by-bit XOR operation and returns the result.a ^ b

4

~

Bitwise First ComplementFlips all the set and unset bits on the number.~a

5

<<

Bitwise LeftshiftShifts the number in binary form by one place in the operation and returns the result.a << b

6

>>

Bitwise RightshilftShifts the number in binary form by one place in the operation and returns the result.a >> b

Example of Bitwise Operators

C
// C program to illustrate the bitwise operators#include <stdio.h>int main(){ int a = 25, b = 5; // using operators and printing results printf("a & b: %d\n", a & b); printf("a | b: %d\n", a | b); printf("a ^ b: %d\n", a ^ b); printf("~a: %d\n", ~a); printf("a >> b: %d\n", a >> b); printf("a << b: %d\n", a << b); return 0;}

Output

a & b: 1a | b: 29a ^ b: 28~a: -26a >> b: 0a << b: 800

5. Assignment Operators in C

Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.

The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators.

In C, there are 11 assignment operators :

S. No.

Symbol

OperatorDescription

Syntax

1

=

Simple AssignmentAssign the value of the right operand to the left operand.

a = b

2

+=

Plus and assignAdd the right operand and left operand and assign this value to the left operand.

a += b

3

-=

Minus and assignSubtract the right operand and left operand and assign this value to the left operand.

a -= b

4

*=

Multiply and assignMultiply the right operand and left operand and assign this value to the left operand.

a *= b

5

/=

Divide and assignDivide the left operand with the right operand and assign this value to the left operand.

a /= b

6

%=

Modulus and assignAssign the remainder in the division of left operand with the right operand to the left operand.

a %= b

7

&=

AND and assignPerforms bitwise AND and assigns this value to the left operand.

a &= b

8

|=

OR and assignPerforms bitwise OR and assigns this value to the left operand.

a |= b

9

^=

XOR and assignPerforms bitwise XOR and assigns this value to the left operand.

a ^= b

10

>>=

Rightshift and assignPerforms bitwise Rightshift and assign this value to the left operand.

a >>= b

11

<<=

Leftshift and assignPerforms bitwise Leftshift and assign this value to the left operand.

a <<= b

Example of C Assignment Operators

C
// C program to illustrate the arithmatic operators#include <stdio.h>int main(){ int a = 25, b = 5; // using operators and printing results printf("a = b: %d\n", a = b); printf("a += b: %d\n", a += b); printf("a -= b: %d\n", a -= b); printf("a *= b: %d\n", a *= b); printf("a /= b: %d\n", a /= b); printf("a %= b: %d\n", a %= b); printf("a &= b: %d\n", a &= b); printf("a |= b: %d\n)", a |= b); printf("a >>= b: %d\n", a >> b); printf("a <<= b: %d\n", a << b); return 0;}

Output

a = b: 5a += b: 10a -= b: 5a *= b: 25a /= b: 5a %= b: 0a &= b: 0a |= b: 5)a >>= b: 0a <<= b: 160

6. Other Operators

Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here:

sizeof Operator

  • sizeof is much used in the C programming language.
  • It is a compile-time unary operator which can be used to compute the size of its operand.
  • The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
  • Basically, the sizeof the operator is used to compute the size of the variable or datatype.

Syntax

sizeof (operand)

To know more about the topic refer to this article.

Comma Operator ( , )

  • The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
  • The comma operator has the lowest precedence of any C operator.
  • Comma acts as both operator and separator.

Syntax

operand1 , operand2 

To know more about the topic refer to this article.

Conditional Operator ( ? : )

  • The conditional operator is the only ternary operator in C++.
  • Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we will execute and return the result of Expression2 otherwise if the condition(Expression1) is false then we will execute and return the result of Expression3.
  • We may replace the use of if..else statements with conditional operators.

Syntax

operand1 ? operand2 : operand3;

To know more about the topic refer to this article.

dot (.) and arrow (->) Operators

  • Member operators are used to reference individual members of classes, structures, and unions.
  • The dot operator is applied to the actual object.
  • The arrow operator is used with a pointer to an object.

Syntax

structure_variable . member;

and

structure_pointer -> member;

To know more about dot operators refer to this article and to know more about arrow(->) operators refer to this article.

Cast Operator

  • Casting operators convert one data type to another. For example, int(2.2000) would return 2.
  • A cast is a special operator that forces one data type to be converted into another.
  • The most general cast supported by most of the C compilers is as follows − [ (type) expression ].

Syntax

(new_type) operand;

To know more about the topic refer to this article.

addressof (&) and Dereference (*) Operators

  • Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
  • The pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var.

To know more about the topic refer to this article.

Example of Other C Operators

C
// C Program to demonstrate the use of Misc operators#include <stdio.h>int main(){ // integer variable int num = 10; int* add_of_num = &num; printf("sizeof(num) = %d bytes\n", sizeof(num)); printf("&num = %p\n", &num); printf("*add_of_num = %d\n", *add_of_num); printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20); printf("(float)num = %f\n", (float)num); return 0;}

Output

sizeof(num) = 4 bytes&num = 0x7ffe2b7bdf8c*add_of_num = 10(10 < 5) ? 10 : 20 = 20(float)num = 10.000000

Unary, Binary and Ternary Operators in C

Operators can also be classified into three types on the basis of the number of operands they work on:

  1. Unary Operators: Operators that work on single operand.
  2. Binary Operators: Operators that work on two operands.
  3. Ternary Operators: Operators that work on three operands.

Operator Precedence and Associativity in C

In C, it is very common for an expression or statement to have multiple operators and in these expression, there should be a fixed order or priority of operator evaluation to avoid ambiguity.

Operator Precedence and Associativity is the concept that decides which operator will be evaluated first in the case when there are multiple operators present in an expression.

The below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom.

Precedence

Operator

Description

Associativity

1

()

Parentheses (function call)

left-to-right

[]

Brackets (array subscript)

left-to-right

.

Member selection via object name

left-to-right

->

Member selection via a pointer

left-to-right

a++ , a–

Postfix increment/decrement (a is a variable)

left-to-right

2

++a , –a

Prefix increment/decrement (a is a variable)

right-to-left

+ , –

Unary plus/minus

right-to-left

! , ~

Logical negation/bitwise complement

right-to-left

(type)

Cast (convert value to temporary value of type)

right-to-left

*

Dereference

right-to-left

&

Address (of operand)

right-to-left

sizeof

Determine size in bytes on this implementation

right-to-left

3

* , / , %

Multiplication/division/modulus

left-to-right

4

+ , –

Addition/subtraction

left-to-right

5

<< , >>

Bitwise shift left, Bitwise shift right

left-to-right

6

< , <=

Relational less than/less than or equal to

left-to-right

> , >=

Relational greater than/greater than or equal to

left-to-right

7

== , !=

Relational is equal to/is not equal to

left-to-right

8

&

Bitwise AND

left-to-right

9

^

Bitwise XOR

left-to-right

10

|

Bitwise OR

left-to-right

11

&&

Logical AND

left-to-right

12

||

Logical OR

left-to-right

13

?:

Ternary conditional

right-to-left

14

=

Assignment

right-to-left

+= , -=

Addition/subtraction assignment

right-to-left

*= , /=

Multiplication/division assignment

right-to-left

%= , &=

Modulus/bitwise AND assignment

right-to-left

^= , |=

Bitwise exclusive/inclusive OR assignment

right-to-left

<<=, >>=

Bitwise shift left/right assignment

right-to-left

15

,

expression separator

left-to-right

To know more about operator precedence and associativity, refer to this article – Operator Precedence and Associativity in C

Conclusion

In this article, the points we learned about the operator are as follows:

  • Operators are symbols used for performing some kind of operation in C.
  • There are six types of operators, Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
  • Operators can also be of type unary, binary, and ternary according to the number of operators they are using.
  • Every operator returns a numerical value except logical, relational, and conditional operator which returns a boolean value (true or false).
  • There is a Precedence in the operators means the priority of using one operator is greater than another operator.

FAQs on C Operators

Q1. What are operators in C?

Answer:

Operators in C are certain symbols in C used for performing certain mathematical, relational, bitwise, conditional, or logical operations for the user.

Q2. What are the 7 types of operators in C?

Answer:

There are 7 types of operators in C as mentioned below:

  • Unary operator
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise operator
  • Assignment operator
  • Conditional operator

Q3. What is the difference between the ‘=’ and ‘==’ operators?

Answer:

‘=’ is a type of assignment operator that places the value in right to the variable on left, Whereas ‘==’ is a type of relational operator that is used to compare two elements if the elements are equal or not.

Q4. What is the difference between prefix and postfix operators in C?

Answer:

In prefix operations, the value of a variable is incremented/decremented first and then the new value is used in the operation, whereas, in postfix operations first the value of the variable is used in the operation and then the value is incremented/decremented.

Example:

b=c=10;
a=b++; // a==10
a=++c; // a==11

Q5. What is the Modulo operator?

Answer:

The Modulo operator(%) is used to find the remainder if one element is divided by another.

Example:

a % b (a divided by b)
5 % 2 == 1


`; tags.map((tag)=>{ let tag_url = `videos/${getTermType(tag['term_id__term_type'])}/${tag['term_id__slug']}/`; tagContent+=``+ tag['term_id__term_name'] +``; }); tagContent+=`
Operators in C - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 5609

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.