# C Expressions

A formula is one that operands are connected to each with the help of operators to calculate the value. An operand could be an expression reference, a variable or array element, or an integer.

Let’s take a look at the following example

``a-b;  ``

In the above example, the character minus (“-” in the above expression)) represents an operator and a and is the pair of operands.

The following four kinds of expressions within C:

• Arithmetic expressions
• Expressions that are related
• Logical expressions
• Expressions with conditions

Every type of expression uses specific types of operands and utilizes a certain group of operators. A particular expression’s evaluation results in a particular number.

For instance:

``x = 9/2 + a-b;  ``

The whole above line is not an expression. The part that follows the equals represents an expression.

Contents

## Arithmetic Expressions

Arithmetic expressions are expressions composed of arithmetic operators and operands. Arithmetic expressions compute an int or double, floating or int.

If an expression only contains integral operands, it is referred to as a pure integer expression. If it is only composed of real operands. In this case, it is referred to for its pure expression and if it includes both real and integral operands, it’s known for mixed-mode.

Assessment of Arithmetic Expressions

The formulas are evaluated by performing only one task at a. The order of precedence and the associativity of operators determine the order of the evaluation of each operation.

If individual actions are being performed in HTML0, the following scenarios can occur:

• If both operands are integers Arithmetic is done, and the output of the operation is an integer number. For example, 3/2 would result in 1. 1.5 because the fractional portion is not taken into consideration.
• If both operands are of the type floating, then arithmetic would be done and the outcome of the operation will be an actual number. For instance, 2.0/2.0 will yield 1.0 and not 1.
• If one of the operands is an integer while another is of the type real then the mixed arithmetic is executed. In this instance the first operand is converted to an actual operand and then arithmetic done to create the real value. For instance, 6/2.0 will yield 3.0 because the initial number of six is transformed to 6.0 and the arithmetic process is then performed to create 3.0.

Let’s look at an example to understand.6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)

## Expressions of Relationship

• The term “relational” refers to a term used to evaluate two operands.
• A condition that is used to determine if the decision should be made or not.
• When using relational expressions a numerical value is not able to be compared with strings.
• The result of a relational expression could be either a zero or non-zero value. In this case, zero corresponds to false, while the zero value is equivalent to a true.

Let’s look at a basic example:

``````#include <stdio.h>
int main()
{

int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}  ``````

Output

## Logical Expressions

• A logic expression is one which computes an un-zero or zero value.
• It’s a complicated test to make the right decision.

Let’s look at some examples of logic behind the expressions.

Let’s look at a basic program for an “&&” operator.

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

Output

Let’s see a simple example of the “| |” operator

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

Output

## Conditional Expressions

• An expression that is conditional can be one that will return 1 if the condition is true, otherwise it returns 0.
• A conditional operator may also be called a ternary operator.

A Syntax for Conditional operator

Imagine exp1 is exp1, exp2, and exp3. three different expressions.

exp1? exp2: exp3

The expression above is a conditional one that is determined by the value of exp1’s expression. In the event that the condition in exp1 is true, the expression that is finalized as a conditional expression will be expressed by exp2 or by exp3.

Let’s look at it with an illustration.

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

Output