C Programming Operators and Expressions - Programtopia (2023)

In this Section, you will learn about Operators in C Programming (all valid operators available in C), expressions (combination of operators, variables and constants) and precedence of operators (which operator has higher priority and which operator has lower priority).

  1. C Operators
  2. Expressions in C
  3. C Operator Precedence

C Operators

Operators are the symbols which tell the computer to execute certain mathematical or logical operations. A mathematical or logical expression is generally formed with the help of an operator. C programming offers a number of operators which are classified into 8 categories viz.

  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Assignment operators
  5. Increment and Decrement operators
  6. Conditional operators
  7. Bitwise operators
  8. Special operators

1. Arithmetic Operators

C programming language provides all basic arithmetic operators: +, -, *, / and %.

C Programming Operators and Expressions - Programtopia (1)

Note: ‘/’ is integer division which only gives integer part as result after division. ‘%’ is modulo division which gives the remainder of integer division as result.

Some examples of arithmetic operators are:

  • a + b
  • a – b
  • a * b
  • a / b
  • a % b

In these examples, a and b are variables and are called operands.

Note: ‘%’ cannot be used on floating data type.

2. Relational Operators

Relational operators are used when we have to make comparisons. C programming offers 6 relational operators.

C Programming Operators and Expressions - Programtopia (2)

Relational expression is an expression which contains the relational operator. Relational operators are most commonly used in decision statements like if, while, etc. Some simple relational expressions are:

  • 1 < 5
  • 9 != 8
  • 2 > 1+3

Note: Arithmetic operators have higher priority than relational operators meaning that if arithmetic expressions are present on two sides of a relational operator then arithmetic expressions will be calculated first and then the result will be compared.

3. Logical Operators

Logical operators are used when more than one conditions are to be tested and based on that result, decisions have to be made. C programming offers three logical operators. They are:

C Programming Operators and Expressions - Programtopia (3)

(Video) Practical-11 | Program to implement Single Inheritance | MSBTE | OOP 22316

For example:

a < 18 || a> 60

An expression which combines two or more relational expressions is known as logical expression.

Note: Relative precedence of relational and logical operators are as follows

Highest precedence!
> >= < <=
== !=
Lowest precedence||

4. Assignment Operators

Assignment operators are used to assign result of an expression to a variable. ‘=’ is the assignment operator in C. Furthermore, C also allows the use of shorthand assignment operators. Shorthand operators take the form:

var op = exp;

where var is a variable, op is arithmetic operator, exp is an expression. In this case, ‘op=’ is known as shorthand assignment operator.

The above assignment

var op = exp;

is the same as the assignment

var = var op exp;

Consider an example:

x += y;

Here, the above statement means the same as

x = x + y;

Note: Shorthand assignment can be used with all arithmetic operators.

5. Increment and Decrement Operators

C programming allows the use of ++ and operators which are increment and decrement operators respectively. Both the increment and decrement operators are unary operators. The increment operator ++ adds 1 to the operand and the decrement operator – subtracts 1 from the operand. The general syntax of these operators are:

Increment Operator: m++ or ++m;

Decrement Operator: m–or –m;

In the example above, m++ simply means m=m+1; and m– simply means m=m-1;

(Video) CS304 GDB no.1 Solution Spring 2021|| gdb CS304 OOP Solution SPRING 2021 By Abid Farooq Bhutta

Increment and decrement operators are mostly used in for and while loops.

++m and m++ performs the same operation when they form statements independently but they function differently when they are used in right hand side of an expression.

++m is known as prefix operator and m++ is known as postfix operator. A prefix operator firstly adds 1 to the operand and then the result is assigned to the variable on the left whereas a postfix operator firstly assigns value to the variable on the left and then increases the operand by 1. Same is in the case of decrement operator.

For example,


In this case, the value of X and Y will be 6.



In this case, the value of Y will be 10 and the value of X will be 11.

6. Conditional Operator

The operator pair “?” and “:” is known as conditional operator. These pair of operators are ternary operators. The general syntax of conditional operator is:

expression1 ? expression2 : expression3 ;

This syntax can be understood as a substitute of if else statement.

For example,

a = 3 ;b = 5 ;

Consider an if else statement as:

if (a > b)x = a ;elsex = b ;

Now, this if else statement can be written by using conditional operator as:

x = (a > b) ? a : b ;

7. Bitwise Operator

In C programming, bitwise operators are used for testing the bits or shifting them left or right. The bitwise operators available in C are:

C Programming Operators and Expressions - Programtopia (4)

8. Special Operators

C programming supports special operators like comma operator, sizeof operator, pointer operators (& and *) and member selection operators (. and ->). The comma operator and sizeof operator are discussed in this section whereas the pointer and member selection operators are discussed in later sections.

1. Comma Operator

The comma operator can be used to link the related expressions together. A comma linked expression is evaluated from left to right and the value of the right most expression is the value of the combined expression.

For example:

 x = (a = 2, b = 4, a+b)

In this example, the expression is evaluated from left to right. So at first, variable a is assigned value 2, then variable b is assigned value 4 and then value 6 is assigned to the variable x. Comma operators are commonly used in for loops, while loops, while exchanging values, etc.

2 .Sizeof() operator

The sizeof operator is usually used with an operand which may be variable, constant or a data type qualifier. This operator returns the number of bytes the operand occupies. Sizeof operator is a compile time operator. Some examples of use of sizeof operator are:

x = sizeof (a);y = sizeof(float);

The sizeof operator is usually used to determine the length of arrays and structures when their sizes are not known. It is also used in dynamic memory allocation.

9.C Expressions

Arithmetic expression in C is a combination of variables, constants and operators written in a proper syntax. C can easily handle any complex mathematical expressions but these mathematical expressions have to be written in a proper syntax. Some examples of mathematical expressions written in proper syntax of C are:

Note: C does not have any operator for exponentiation.

10.C Operator Precedence

At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then the arithmetic expression is evaluated from left to right. There are two priority levels of operators in C.

High priority: * / %
Low priority: + –

The evaluation procedure of an arithmetic expression includes two left to right passes through the entire expression. In the first pass, the high priority operators are applied as they are encountered and in the second pass, low priority operations are applied as they are encountered.

Suppose, we have an arithmetic expression as:

x = 9 – 12 / 3 + 3 *2 - 1

This expression is evaluated in two left to right passes as:

First Pass

Step 1: x = 9-4 + 3 * 2 – 1Step 2: x = 9 – 4 + 6 – 1Second PassStep 1: x = 5 + 6 – 1Step 2: x = 11 – 1Step 3: x = 10

But when parenthesis is used in the same expression, the order of evaluation gets changed.

For example,

x = 9 – 12 / (3 + 3) * (2 – 1)

When parentheses are present then the expression inside the parenthesis are evaluated first from left to right. The expression is now evaluated in three passes as:

First Pass

Step 1: x = 9 – 12 / 6 * (2 – 1)Step 2: x= 9 – 12 / 6 * 1

Second Pass

Step 1: x= 9 – 2 * 1Step 2: x = 9 – 2

Third Pass

Step 3: x= 7

There may even arise a case where nested parentheses are present (i.e. parenthesis inside parenthesis). In such case, the expression inside the innermost set of parentheses is evaluated first and then the outer parentheses are evaluated.

For example, we have an expression as:

x = 9 – ((12 / 3) + 3 * 2) – 1

The expression is now evaluated as:

First Pass:

Step 1: x = 9 – (4 + 3 * 2) – 1Step 2: x= 9 – (4 + 6) – 1Step 3: x= 9 – 10 -1

Second Pass

Step 1: x= - 1 – 1Step 2: x = -2

Note: The number of evaluation steps is equal to the number of operators in the arithmetic expression.


What is operators and expressions in C programming? ›

Expressions perform specific actions, based on an operator, with one or two operands. An operand can be a constant, a variable or a function result. Operators are arithmetic, logical, and relational. As with C, some operators vary in functionality according to the data type of the operands specified in the expression.

What are the 8 operators in C program? ›

These operators are responsible for performing arithmetic or mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), the remainder of the division (%), increment (++), decrement (–).

What is the priority order among (* %) (+ -) and (=) C operators? ›

1) What is the Priority among (*, /, %), (+, -) and (=) C Operators.? Explanation: Assignment operator in C has the least priority.

What type of operators are these <= >= == !=? ›

Relational Operators

It is used to compare two numbers by checking whether they are equal or not, less than, less than or equal to, greater than, greater than or equal to. == (Equal to)– This operator is used to check if both operands are equal. !=

What is an expression in C example? ›

An expression in C is a combination of operands and operators – it computes a single value stored in a variable. The operator denotes the action or operation to be performed. The operands are the items to which we apply the operation.

Why expressions and operators are used in C language? ›

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.

What are the 4 types of functions in C? ›

What are the different categories of functions in C Programming?
  • Functions without arguments and without return values.
  • Functions without arguments and with return values.
  • Functions with arguments and without return values.
  • Functions with arguments and with return values.
Mar 9, 2021

What are the 7 types of operators? ›

The different types of operators are arithmetic operators, assignment operators, comparison operators, logical operators, identity operators, membership operators, and boolean operators.

What are the 5 different types of operators? ›

  • arithmetic operators.
  • relational operators.
  • logical operators.

What is expression operands and operators? ›

An "operand" is an entity on which an operator acts. An "expression" is a sequence of operators and operands that performs any combination of these actions: Computes a value. Designates an object or function. Generates side effects.

What is expression statement in C? ›

There are three different classes of statements in C: expression statements, compound statements, and control statements. An expression statement consists of an expression followed by a semicolon. The execution of such a statement causes the associated expression to be evaluated.

What are expressions in programming? ›

In programming, an expression is a value, or anything that executes and ends up being a value. It is necessary to understand that a value is unique. For example, const , let , 2 , 4 , s , a , true , false , and world are values because each of them is unique in meaning or character.

What is type of expression in C? ›

There are four types of expressions exist in C:

Arithmetic expressions. Relational expressions. Logical expressions. Conditional expressions.

Top Articles
Latest Posts
Article information

Author: Stevie Stamm

Last Updated: 11/26/2022

Views: 6010

Rating: 5 / 5 (60 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.