Expressions in C Programming - Types of Expressions in C ( With Examples )

Expressions in C Programming - Types of Expressions in C ( With Examples )

10 Jun 2024
Beginner
8.08K Views
12 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Expressions in C: An Overview

C is one of the most popular and widely used programming language due to its straightforward syntax and vast range of capabilities. With C online training, it’s possible for novice programmers to quickly pick up the basics and then go on to create something truly amazing. So let’s take some time out today to explore how we can use expressive features from the C tutorial by exploring variable declarations, scope rules, data types, etc. so that your next big project feels like creating something extraordinary!

What is Expression in C language?

An expression in C language is a combination of symbols, numbers, and/or text that produces a particular result. These expressions are evaluated and can be used to assign values to variables, perform mathematical operations, or execute different actions such as comparison and Boolean logic.

Expressions in C consist of C language constants (numbers or strings), C language operators (arithmetic, assignment, relational), and C language variables representing values stored in computer memory.

Types of Expressions in C

types of expressions in c language

There are four types of Expressions in C programming:

  1. Arithmetic Expression
  2. Relational Expression
  3. Logical Expression
  4. Conditional Expression

  1. Arithmetic Expression in C

An arithmetic expression consists of operands and arithmetic operators. It performs computations on the int, float, or double type values. Arithmetic operations can be performed in a single line of code or multiple lines combined with arithmetic operations such as addition, subtraction, multiplication, and division.

The following types of arithmetic expressions are there:

  • integer expression - an expression containing only integral operands,
  • real expression - an expression containing only real operands
  • mixed mode expression - an expression containing both integral and real operands

The arithmetic expression makes it easier to calculate computationally intensive tasks by programming each operation step-by-step. The precedence and associativity of operators decide the order of the evaluation of individual operations.

Type of results after evaluation of an arithmetic expression:

  • When both the operands are of integer type, the result of the operation would be an integer value. The fractional part of the result is ignored.
  • When both the operands are of float data type, the result of the operation would be a real value.
  • If one operand is of type integer and another of type real, the first operand is converted into a real operand, and then the operation is performed, producing a real value as the result.

Example

Evaluation of expression Description of each operation
6*2/( 2+1 * 2/3 +6) +8 * (8/4) An expression is given.
6*2/(2+2/3 + 6) + 8 * (8/4) 2 is multiplied by 1, giving the value 2.
6*2/(2+0+6) + 8 * (8/4) 2 is divided by 3, giving a value 0
6*2/ 8+ 8 * (8/4) 2 is added to 6, giving a value 8.
6*2/8 + 8 * 28 is divided by 4, giving a value 2.
12/8 +8 * 26 is multiplied by 2, giving a value 12.
1 + 8 * 212 is divided by 8, giving a value 1.
1 + 168 is multiplied by 2, giving a value 16.
171 is added to 16, giving a value 17.

  1. Relational Expression in C

Relational expressions use comparison operators such as '>' (greater than) and '<' (less than) to compare two operands. The result of the comparison is a boolean value i.e. 0(false) or non-zero (true).

It is a condition that decides whether the action should be taken or not. Relational expressions are used in decision-making statements and looping statements in the C language.

Relational Expression Description
x%2 = = 0 The given condition checks whether the x is an even number or not. This relational expression shows the result as the value 1 if x is an even number otherwise as the value 0
a!=b This relational expression is used to check if a is not equal to b and it results in 1 if a is not equal to b otherwise 0.
a+b = = x+y It is used to check if this particular expression "a+b" is equal to the expression "x+y"
a>=9 It is used to check if the value of a is greater than or equal to 9.

Example


#include <stdio.h>
int main()
 {
 int x = 5, y = 10;
 if (x == y) {
 printf("x is equal to y\n");
 } else {
 printf("x is not equal to y\n");
 }
 return 0;
 }

The above code in C Compiler checks if x is equal to y or not

Read More - Top 50 Mostly Asked C Interview Questions and Answers

Output

x is not equal to y

  1. Logical Expression in C

Logical expressions in C are a powerful tool for controlling the logic of the flow of the program. They are made by combining as many relational expressions as the programmer wants. It then determines if certain statements or groups of statements should be executed or not.

Logical Expressions Description
( x > 4 ) && ( x < 6 ) This logical expression is used as a test condition to check if the x is greater than 4 and the x is less than 6. The result of the condition is true only when both conditions are true.
x > 10 || y <11 This logical expression is used as a test condition to check if x is greater than 10 or y is less than 11. The result of the test condition is true if either of the conditions holds true value.
! ( x > 10 ) && ( y = = 2 ) This logical expression is used as a test condition to check if x is not greater than 10 and y is equal to 2. The result of the condition is true if both the conditions are true

Example


#include <stdio.h>
int main() 
{ 
 int x = 10; 
 int y = 2; 
 if ( (x >10) || (y<5)) 
 { 
 printf("Condition is true"); 
 } 
 else 
 printf("Condition is false"); 
 return 0; 
}

Output

Condition is true

  1. Conditional Expression in C

The conditional expression consists of three operands. It returns 1 if the condition is true otherwise 0. We had seen the conditional operator in the section, Operators in C. We will look at ternary operators in detail in the section Ternary Operator in C.

Example


#include <stdio.h> 
#include <string.h> 
int main() 
{ 
 int age = 29; 
 char status; 
 status = (age>22) ? 'M': 'U'; 
 if(status == 'M') 
 printf("Married"); 
 else 
 printf("Unmarried"); 
 return 0; }

Output

Married
Summary

If you're looking to enhance your expression skills in C programming, earning a C certification can be highly beneficial. Writing expressions in C programming can be a challenging process, requiring patience, determination, and a thorough understanding of how the language works. As an experienced programmer, obtaining a C certification showcases your expertise in this area.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course Jun 16 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
.NET Microservices Certification Training Jun 23 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this