Expressions in C

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

advertisement
advertisement

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).

advertisement

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.

advertisement

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:

  1. #include <stdio.h>
  2. #define R2Y 1.64     /* Rupee to Yen */ 
  3. #define Y2R 0.6093   /* Yen to Rupee */
  4.  
  5. int main(void)
  6. {
  7.     int choice;
  8.     float yens, rupees;
  9.  
  10.     printf("user, wanna to convert Yen to Rupee, enter 1 or\nif you "
  11.            "wanna to convert Rupee to Yen, enter 2\nor enter q to "
  12.            "quit\n");
  13.  
  14.     while (scanf("%d", &choice) == 1) {
  15.         if (choice == 1) {
  16.             printf("user, enter what many YENs u wanna to convert to "
  17.                    "Rupees...\n");
  18.             scanf("%f", &yens);
  19.             printf("%.2f YENs equal %.6f Rupees!\n\n", yens, 
  20.                    yens * Y2R);
  21.         } 
  22.         else if (choice == 2) {
  23.             printf("user, enter what many RUPEEs u wanna to convert to "
  24.                    "YENs...\n");
  25.             scanf("%f", &rupees);
  26.             printf("%.2f RUPEEs equal %.6f YENs!\n\n",
  27.                     rupees, rupees * R2Y);
  28.         }
  29.         printf("user, wanna to convert Yen to Rupee, enter 1 or \nif "
  30.                "you wanna to convert Rupee to Yen, enter 2\nor enter q "
  31.                "to quit\n");
  32.      }
  33.      printf("Thank You!\n");
  34.      return 0;	
  35. }

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.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.