Expressions in C
Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand might be a variable, a function reference or a constant, or an array element.
Expression is a combination of operators and operands that reduces a single value from a more complex one. The operation is performed on the data item, which is created as an operand.
An operator indicates the operations that will be performed on the data. An expression in the C standard is also defined as two or more operands connected by one operator and are said to perform an operation that a programmer will define.
The operator is represented using symbols such as “+,” “-”, “/”, “*”, and so on. The C language form operators join separate individual constants, array elements, and present variables.
In other words, an expression is a sequence of operands and operators that defines a computation. It is provided by evaluating the expression, such as the evaluation of 2 + 2 yields the result 4.
The result may generate side effects as sometimes, user-defined print functions give the standard output stream and designate functions and objects. Value categories classify expression by their values and order of evaluation of arguments, and subexpression specifies the order in which intermediate results are obtained.
For example, “ a + b ”, in this expression, the addition character (+) is an operator, while the characters ‘a’ and ‘b’ are the operands.
Types of Expression in C
There are four types of expression in the C standard, which are as discussed below:
- Arithmetic expressions
- Relational expressions
- Logical expressions
- Conditional expressions
All these expressions take specific types of operands and then use a specific set of operators. The evaluation of the particular expression produces a specific value. For instance, “ x = 9 / 2 + a * b ”, is not an expression. The part after the equal symbol is considered as an expression.
? Arithmetic Expression
An arithmetic expression is an expression that contains operands and arithmetic operators. It computes a value of type int, float, and double. When the expression consists of only the integral operands, it is known as a pure integer expression.
When it contains the real operands, it is referred to as pure, authentic expression, and when it consists of both the integral operands and real operands, it is referred to as mixed-mode expression. An arithmetic expression is evaluated by performing one operation at a time.
The precedence and associativity of the operators decide the order of the evaluation of the individual operations.
The Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Increment (++), and Decrement (–) operators are referred to as the “Arithmetic expressions”.
Syntax:
A + B;
A - B;
A / B;
A * B;
A % B;
A++;
A--;
? Relational expression
The relational expression is an expression that is used to compare the two operands. It is referred to as a condition used to decide whether the action should be undergone. A
numeric value cannot be compared to a string value in the relational expression.
The result of the relational expression can be either a zero or a non-zero value. Hence, the zero value is the equivalent to a false, and a non-zero value is always equivalent to a true value.
“ == , !=, >, <, >=, <=” are referred to as relational expression.
Syntax:
A == B;
A != B;
A > B;
A < B;
A >= B;
A <= B;
? Logical Expression
The logical expression is an expression that computes either a zero or a non-zero value. A logical expression is considered to be a complex test condition while taking a decision.
Logical AND (&&), logical OR (||), and Logical NOT (!) are the operators that come under logical operators.
Each of the logical operators falls into its precedence groups. A ‘logical AND’ has higher precedence than ‘logical OR’. Both the precedence groups are lower than the group containing the equality operators.
The associativity is left to right. The C language also includes the unary operator that negates the value of the logical expression. The associativity of the negation operator is right to left.
Syntax:
A && B;
A || B;
A ! B;
? Conditional Expressions
A conditional expression is an expression that always returns ‘1’ if the condition specified is true. Else will return ‘0’ if the condition is false. A conditional operator is also known as a ternary operator. It is used to perform the conditional check.
Syntax:
expression 1 ? expression 2 : expression 3
Where,
expression 1, expression 2, and expression 3 are the three expressions specified.
It is evaluated based on the value of the expression 1.
Suppose the condition of the expression 1 satisfies and is valid. In that case, the final condition is represented by the expression 2; else will be represented by expression 3.
“ ?, :” are considered to represent the conditional expressions.
E.g.:
( x + 2 = 10) ? ‘True’ : ‘False’
This expression makes use of the conditional operator. A conditional expression frequently appears on the right-hand side of a simple assignment statement. The resulting value of the conditional expression is assigned to the identifier on the left.
The conditional operator has its precedence, just above the assignment operator. The associativity is right to left.