21
NovExpressions in C Programming - Types of Expressions in C ( With Examples )
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
There are four types of Expressions in C programming:
- Arithmetic Expression
- Relational Expression
- Logical Expression
- Conditional Expression
- Arithmetic Expression in C
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 aninteger
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 areal
value. - If one operand is of type
integer
and another of typereal
, the first operand is converted into areal
operand, and then the operation is performed, producing areal
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 * 2 | 8 is divided by 4, giving a value 2. |
12/8 +8 * 2 | 6 is multiplied by 2, giving a value 12. |
1 + 8 * 2 | 12 is divided by 8, giving a value 1. |
1 + 16 | 8 is multiplied by 2, giving a value 16. |
17 | 1 is added to 16, giving a value 17. |
- 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
- 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
- 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.