An Absolute Guide to Know Everything on Expressions in C

Expressions in C programing language combine operands, operators, and variables. The evaluations of an expression in C happen according to the operator's precedence. Once the expressions are processed, the result will be stored in the variable.   

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Are Expressions in C?

Expressions are the combination of variables, operands, and operators. The result would be stored in the variable once the expressions are processed based on the operator's precedence.

Expression Example:

c=a+b

a-b+c

a+b-(a*c)

Expressions_In_C_1

Types of Expressions in C 

  • Arithmetic expressions
  • Relational expressions
  • Logical expressions
  • Conditional expressions

You will go through each of them in detail.

Arithmetic Expressions:

The arithmetic expression is evaluated in specific order considering the operator's precedence, and the result of an expression will be based on the type of variable.

Example: 

Expressions_In_C_2.

From the given above arithmetic expressions example, let's consider the values of operands to be a=2,b=3, and c=4.

After substitution of values into the given expression, we get:

Z = 2 + 3 - (2 * 4)

Below given steps are in order of precedence in which the operators of an expression must be evaluated.

Step1: First, Priority is given to the parenthesis, so whatever is inside the parenthesis will be evaluated first, a*c (2*4 = 8).

Step 2: The next level of precedence is given to + and - operator, so next a+b (2+3 = 5) is evaluated 

Step 3: a+b will be subtracted from a*c i.e; 5 - 8 = -3.

Step 4: The final value after evaluation of an arithmetic expression will be stored into the variable Z, according to the example value three is stored into the variable Z that is z = -3

Look at the below-given figure to understand the arithmetic expressions evaluation in a better way.

Expressions_In_C_3

Example Program Using Arithmetic Expressions:

#include <stdio.h>

void main()

{

    int a=2,b=3,c=4,z;

    z=a+b-(a*c);

    printf("Result= %d",z);

Output:

Expressions_In_C_4

Followed by arithmetic expressions, you will learn all about relational expressions.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Relational Expressions:

Relational operators >, <, ==,!= etc are used to compare 2 operands. Relational expressions consisting of operands, variables, operators, and the result after evaluation would be either true or false.

Example:

C = a > b

a*b == a+b

a+b*c > a*b+c

Expressions_In_C_5

From the given above relational expression example, let's consider the values of operands to be a=3, b=2, and c=1.

After substitution of values into the given expression, we get:

Z = 3 * 2 > 3 + 1

Below given steps are in order of precedence in which the operators of an expression must be evaluated.

Step 1: It first evaluates a*b (3*2=6).

Step 2: Next, computer evaluate the expression a+c (3+1 =4)

Step 3: Then the values are compared with each other, that is, six> 4

Since the value six is greater than 4, the output will be true; in C programming, true is represented by one, and false represents zero.

Step 4: The result one is stored in the variable Z

Look at the below-given figure to understand the evaluation of the relational expression in a better way.

Expressions_In_C_6.

Example Program Using Relational Expressions:

#include <stdio.h>

void main()

{

    int a=3,b=2,c=1,z;

    z=a*b>a+c;

    printf("Result= %d",z);

}

Output:

Expressions_In_C_7

After relational expressions, you have logical expressions.

Logical Expressions:

Relational expressions and arithmetic expressions are connected with the logical operators, and the result after an evaluation is stored in the variable, which is either true or false.

Example:

C= (a+b)>c && a<b 

a>b || b>a

From the given below logical expression example, let's consider the values of operands to be a=2, b=4, and c=3.

Expressions_In_C_8

After substitution of values into the given expression, we get:

Z = (2 + 4) > 3 && 2 < 4

Below given steps are in order of precedence in which the operators of an expression must be evaluated.

Step 1: Computer first evaluates the expression a+b (2+4=6) and then checks whether a+b is greater than c (6>3), which is true.

Step 2: It then evaluates a < b (2<4) true.

Step 3: Finally, it evaluates both expressions using the logical AND operator, represented by the symbol &&. The result true(1) is stored in the variable Z.

Look at the below-given figure to understand the evaluation of the logical expression in a better way.

Expressions_In_C_9

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example Program Using Logical Expressions:

#include <stdio.h>

void main()

{

    int a=2,b=4,c=3,z;

     z= (a+b) >c && a<b; 

    printf("result=%d",z);

}

Output:

/Expressions_In_C_10

Up next, we have conditional expressions.

Conditional Expressions:

The general syntax of conditional expression is:

Exp1? Exp2: Exp3

From the given above expressions, the first expression (exp1) is conditional, and if the condition is satisfied, then expression2 will be executed; otherwise, expression3 will be performed.

Example:

2<3? 2 : 3

a*b>c ? true : false

From the given below conditional expressions example, let's consider the values of operands to be a=5, b=3, and c=1.

Expressions_In_C_11.

After substitution of values into the given expression, we get:

Z = (5 * 3) < 1 ? ‘true’ : ‘false’

Below given steps are in order of precedence in which the operators of an expression must be evaluated.

Step 1: First, it evaluates the expression1 a*b (5*3=15).

Step 2: Next, it checks whether the condition is satisfied, a*b < c (15 > 1), false.

Step 3: Since the condition is false, expression3 will be executed and return the value one as an output stored in the variable Z.

Look at the below-given figure to understand the evaluation of the conditional expressions in a better way.

Expressions_In_C_12. 

#include <stdio.h>

void main()

{   

    int a=5, b=3, c=1,z;

    z = (5 * 3) < 1 ? 1 : 0;

    printf("Result = %d",z);

}

Output:

Expressions_In_C_13

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Next Steps

"Data Structures in C" can be your next topic. So far, you have learned the expressions in the C programming language. The following fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in building a career in the field of software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages used today? If yes, our Post Graduate Program in Full Stack Development should prove to be just the right thing for your career. Explore the course and enroll soon.

If you have any questions regarding the "Expressions in C" tutorial, please let us know in the comment section below. Our experts will get back to you ASAP.

About the Author

Hoor Sania SHoor Sania S

Hoor is a Bangalore-based Research Analyst. She has a knack for learning computer languages and technologies. In her free time, she enjoys dancing and singing.

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