This C Tutorial Explains C Expressions.

**What is Expression in C?**

Expression in C is a combination of operators and operands that specify an operation to perform. The value of the expression is the result of the operation.

**Example:**

50; /* an integer constant */ 543.0009; /* a real constant */ x + y; /* sum y to x */ hall >= room; /* a relational exp */ value = a * b / (c * d + a) /* an arithmetic exp */

Operators are symbols that tell the compiler what to do with the operands. The operands are the objects on which the operator acts. They can be constants, variables, or expressions. For example:

x + y; /* plus '+' is a binary operator operates on two operands x and y and */ /* computes their sum */

**Types of Expressions in C**

Expressions in C can be broadly classified into four categories:

## Arithmetic Expressions in C

These expressions perform arithmetic operations on both literals and variables. Some of the commonly used arithmetic operators are **+ (addition), – (subtraction), * (multiplication), / (division) and % (modulus)**. For example, the expression 5+6 will add 5 and 6 to give the result 11.

Arithmetic expressions can also make use of assignment operator (=) to assign a value to a variable. For example, the expression c=5+6 will assign the value 11 to the variable c.

#include <stdio.h> int main() { int a = 5, b = 6, c; c = a + b; printf("%d", c); }

**Output:**

11

## Relational Expressions in C

These expressions compare two values or variables and return either true or false based on the outcome of the comparison. The commonly used relational operators are **== (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to) and <= (less than or equal to)**.

For example, the expression 5>6 will return false as 5 is not greater than 6 while the expression 6>=6 will return true as 6 is greater than or equal to 6.

#include <stdio.h> int main() { int a = 5, b = 6; if(a>b) { printf("true"); }printf("false"); }

**Output:**

false

## Logical Expressions in C

These expressions are used to test certain conditions and produce boolean outputs (true or false). Logical expressions are often used in control flow statements such as if-else and switch-case to execute a certain block of code only if a particular condition is satisfied.

**Example:**

#include <stdio.h> int main() { int i = 1; if (i++ && (i == 1)) printf("Yes\n"); else printf("No\n"); }

**Output:**

No

## Conditional Expressions in C

The conditional expression is a operator that takes three operands. The first operand is a boolean expression, and the second and third operands are values. Conditional operator, also known as the ternary operator.

**Syntax:**

expression1 ? expression2 : expression3;

**Example:**

#include <stdio.h> void main() { int a = 8; int b = 7; int c = a < b ? a = b : b++; printf("%d", c); }

**Output:**

7

## Operator Precedence

Every C expression has a Numeric value. There are certain Rules regarding the evaluation of expressions. The Operator Precedence & its Associativity determine in which order & which operators to be evaluated first, next and so on in a given expression. For example:

x = 20; /* assignment operator '=' assigns value to right of this to the */ /* variable left of this. result of exp x = 20; is 20 */ y = 20; z = x + y; /* z gets value 40 */ x + y; /* this expression results 40 */ x > y; /* relational expression results 0 here */

Relational Expressions, Logical Expressions have values either True or False that is 1 or 0. C99 standard provides a **new type _Bool which allows only two values 0 & 1 for false & true.**

A C program comprises of Statements each of which, mostly, in turn comprises of expressions. For example:

`#include <stdio.h>`

`#define R2Y 1.64 /* Rupee to Yen */`

`#define Y2R 0.6093 /* Yen to Rupee */`

int main(void)

`{`

int choice;

float yens, rupees;

printf("user, wanna to convert Yen to Rupee, enter 1 or\nif you "

`"wanna to convert Rupee to Yen, enter 2\nor enter q to "`

"quit\n");

while (scanf("%d", &choice) == 1) {

if (choice == 1) {

printf("user, enter what many YENs u wanna to convert to "

"Rupees...\n");

scanf("%f", ¥s);

printf("%.2f YENs equal %.6f Rupees!\n\n", yens,

yens * Y2R);

`}`

else if (choice == 2) {

printf("user, enter what many RUPEEs u wanna to convert to "

"YENs...\n");

scanf("%f", &rupees);

printf("%.2f RUPEEs equal %.6f YENs!\n\n",

rupees, rupees * R2Y);

`}`

printf("user, wanna to convert Yen to Rupee, enter 1 or \nif "

`"you wanna to convert Rupee to Yen, enter 2\nor enter q "`

"to quit\n");

`}`

printf("Thank You!\n");

return 0;

`}`

In the above program, we used return value of scanf() fun to terminate the loop. When we enter q to quit, scanf() failed to read & returns 0 and therefore loop terminates.

A **C expression may also contain sub expression.** For example:

x + y * (a = b / c); /* (a = b / c) is a sub-exp */ /* In the above exp., sub-exp is evaluated first because parentheses have */ /* higher precedence. */ /* The result of sub-exp is value of the variable to the left side of the */ /* assignment operator.*/ /* result is then multiplied to the value of y. Then the product is added */ /* to x */

**Sanfoundry Global Education & Learning Series – 1000 C Tutorials.**

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Buy Computer Science Books

**Additional Resources:**