What are Expressions in C?
An expression is composed of operators and operands. The operands may be either constants or variables. All expressions can be evaluated to get a result. This result is known as the value returned by the expression.
Table of Contents
On the basis of the operators used, expressions are mainly classified into arithmetic expressions, relational expressions, and logical expressions.
Types of Expressions in C
Following are the types of expression in C:
Arithmetic Expressions in C
An expression in which only arithmetic operators are used is called arithmetic expression. The operands are numeric data and they may be variables or constants. The value returned by these expressions is also numeric.
Arithmetic expressions are further classified into integer expressions, floating-point (real) expressions, and constant expressions:
Integer Expressions in C
If an arithmetic expression contains only integer operands, it is called integer expression and it produces an integer result after performing all the operations given in the expression.
For example, if x and y are integer variables, some integer expressions and their results are shown in Table. Note that all the above expressions produce integer values as the results.
Floating Point Expressions in C (Real Expressions)
An arithmetic expression that is composed of only floating-point data is called floating-point or real expression and it returns a floating-point result after performing all the operations given in the expression.
The table shows some real expressions and their results, assuming that x and y are floating-point variables:
It can be seen that all the above expressions produce floating-point values as the results. In an arithmetic expression, if all the operands are constant values, then it is called constant expression. The expression 20+ 5/2.0 is an example. The constants like 15, 3 . 14, ‘A’ are also known as constant expressions.
Relational Expressions in C
When relational operators are used in an expression, it is called relational expression and it produces Boolean type results like True (1) or False (0). In these expressions, the operands are numeric data.
Let us see some examples of relational expressions in Table:
We know that arithmetic operators have higher priority than relational operators. So when arithmetic expressions are used on either side of a relational operator, arithmetic operations will be carried out first and then the results are compared.
The table contains some expressions in which both arithmetic and relational operators are involved. Though they contain mixed types of operators, they are called relational expressions since the final result will be either True or False.
Logical Expressions in C
Logical expressions combine two or more relational expressions with logical operators and produce either True or False as the result. A logical expression may contain constants, variables, logical operators, and relational operators.
Let us see some examples in Table:
As seen in Table, though some expressions consist of arithmetic and relational operators in addition to logical operators, the expressions are considered logical expressions. This is because the operation carried out, at last, will be the logical operation and the result will be either True or False.