C expressions
In this article, we will discuss C expressions with its types, syntax, and examples.
C expressions are essential elements of the C programming language, which represent calculations or operations that result in a value. A meaningful combination of constants, variables, operators, and function calls can form an expression that can be used to carry out a variety of calculations and operations.
Here are some essential C expressions elements and concepts:
Constants: During a program, constants are fixed values that do not change. They can be character constants, such as single-quoted characters, or numeric constants, such as integers or floating-point numbers.
Variables: Variables are places where values are stored. They can be given values that can be utilized in expressions and are specified with a data type.
Expression Types in C:
In the C programming language, expressions are divided into THREE types. They are as follows:
- Infix Expression
- Postfix Expression
- Prefix Expression
The above classification is based on the operator position in the expression.
Infix Expression:
The expression in which the Operator is used between operands is called an infix expression.
The infix expression has the following general structure.
![C expressions](https://static.tutorialandexample.com/c/c-expressions1.png)
Operand1 Operator Operand2
Postfix Expression:
Postfix expression is the expression in which the Operator is used after operands.
The postfix expression has the following general structure.
![C expressions](https://static.tutorialandexample.com/c/c-expressions2.png)
Operand1 Operand2 Operator
Prefix Expression:
The expression in which the Operator is used before operands is called a prefix expression.
The prefix expression has the following general structure.
![C expressions](https://static.tutorialandexample.com/c/c-expressions3.png)
Operator Operand1 Operand2
Operators: Symbols known as operators carry out operations on one or more operands to obtain results. C offers a variety of operators:
- Arithmetic Operators
- Relational Operators
- Logical operators
- Bitwise Operators
- Conditional Operator (Ternary Operator)
- Special Operators
![C expressions](https://static.tutorialandexample.com/c/c-expressions4.png)
1.Arithmetic Operators:
Basic mathematical operations on numeric operands are carried out in C via arithmetic operators. Using these operators, you can carry out addition, subtraction, multiplication, division, and modulus operations. The C's arithmetic operators are listed here.
Arithmetic Operators are classified into three types
- Binary Arithmetic Operators
- Compound Arithmetic Operators
- Unary Arithmetic Operators
Binary Arithmetic Operators:
Binary Arithmetic Operators will perform their operation on two operands
Following are the binary Arithmetic Operators
Operator | Name | Example |
+ | Addition | 10+20=30 |
_ | Subtraction | 20-3=17 |
* | Multiplication | 10*3=30 |
/ | Division | 10/3=3.0000 |
% | Remainder | 10%3=1 |
In this program, we make use of several Binary Operators.
- + is a binary operator, which is used to sum two operands to yield a resultant value.
- - is a binary operator used to subtract two operands to yield a resultant value.
- * is a binary operator multiplying two operands to yield a resultant value.
- / is a binary operator that divides two operands to yield a resultant value.
- % is a binary operator used to find the remainder of two operands when divided.
Example:
Let's take an example to understand the use of Binary Arithmetic Operators:
#include <stdio.h> //header file section
int main() //main section
{
int a = 10;
int b = 3;
int l = a + b;
int m = a - b;
int n = a * b;
int o = a / b;
int p = a % b;
printf("Addition of a and b = %d ", l);
printf("\nSubtraction of a and b = %d ", m);
printf("\nMultiplication of a and b = %d ", n);
printf("\nDivision of a and b = %d ", o);
printf("\nRemainder of a and b = %d ", p);
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions5.png)
Compound Arithmetic Operators:
The following are the compound arithmetic operators:
Operator | Name | Example |
+= | Compound addition | x+=10 |
-= | Compound subtraction | x-=10 |
*= | Compound multiplication | x*=10 |
/= | Compound division | x/=10 |
%= | Compound remainder | x%=10 |
Example:
Let's take an example to understand the use of compound arithmetic operators:
#include <stdio.h>
int main() {
int num1 = 25;
int num2 = 5;
// Compound addition assignment
num1 += num2; // Equivalent to: num1 = num1 + num2
printf("After compound addition: num1 = %d\n", num1);
// Compound subtraction assignment
num1 -= num2; // Equivalent to: num1 = num1 - num2
printf("After compound subtraction: num1 = %d\n", num1);
// Compound multiplication assignment
num1 *= num2; // Equivalent to: num1 = num1 * num2
printf("After compound multiplication: num1 = %d\n", num1);
// Compound division assignment
num1 /= num2; // Equivalent to: num1 = num1 / num2
printf("After compound division: num1 = %d\n", num1);
// Compound modulus assignment
num1 %= num2; // Equivalent to: num1 = num1 % num2
printf("After compound modulus: num1 = %d\n", num1);
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions6.png)
Unary Operators in C:
The operation will be performed on only one operand in unary arithmetic operators.
The following are the two types of unary Arithmetic operators
- Increment/decrement operator
- Size of Operator.
Increment/decrement operator:
Increment operator:
- It is known as an increment operator.
- If you perform an increment operation on an operand, the value of that operand is increased by 1.
Example: int a = 2;
a = ++a;
Note: either pre or post-increment, the value of the operant is to be increased by one
The values differ whenever we assign a pre- or post-increment to an expression.
Case1:
After the assignment operator, if we have an operand, assign the operand value to the left side variable and increase the value of the operand.
Example:
a=100
b=a++
Answers:
a=101
b=100
Case2:
After the assignment operator, if we have an operator, increase the value of the operant and assign the increase of value to the left side of the variable.
Example:
a=100
b=++a
Answers
a=101
b=101
Example:
Let's take an example to understand the use of increment operator:
#include <stdio.h> //header file section
int main() //main section
{
int a = 5;
printf("The value of a = %d ", a);
a = ++a;
printf("\nThe value of a = %d ", a);
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions7.png)
Decrement operator:
- It is the decrement operator.
- If you perform a decrement operator, the value of the operant is decrement by 1.
- If you perform pre-decrement or post-decrement on an operation, the value is decreased by 1.
Example:
x=10
X- -
Answer = 9
Case1:
After the assignment operator, if we have an operand, assign the operand value to the lift side variable and increase the value of the operand.
Example:
a=100
b=a- -
Answers
a=99
b=100
Case2:
After the assignment operator, if we have an operator, increase the value of the operant and assign the increase of value to the left side of the variable.
Example:
a=100
b=- -a
Answers
a=99
b=99
Example:
Let's take an example to understand the use of the decrement operator:
#include <stdio.h> //header file section
int main() //main section
{
int a = 5;
printf("The value of a = %d ",a);
a = --a;
printf("\nThe value of a = %d ",a);
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions8.png)
Relational Operators:
Generally, in C programming, we use operators such as +, -, *, and / for various mathematical operations, so they are used to compare the various operands. The above-given operators are called relational operators. The result of any relational operator is a Boolean value of 0 and 1.
Various Types of Relational Operators:
The below-mentioned are different types of operators:
Operator | Meaning | Example |
< | Less than | 10 < 20 -> T = 1 |
> | Greater than | 10 > 20 -> F = 0 |
<= | Less than or equal to | 10<=20 -> T = 1 |
>= | Greater than or equal to | 10>=20 -> F = 0 |
== | Equals to | 10==20 -> F = 0 |
!= | Not Equals to | 10! = 20 -> T = 0 |
C Programs for the above relational operators:
Let us understand the functions above programs using some programs:
1.Less than (<) Operator:
The less than Operator, denoted by the symbol "<", determines if the first operand is smaller than the second. If so, it gives a valid result. Otherwise, false is returned.
Symbol: <
Example:
Let's take an example to understand the use of less than (<) operator:
#include<stdio.h>
void main()
{
int a, b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a<b)
{
printf("%d is less than %d",a,b);
}
else
{
printf("%d is greater than %d",a,b);
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions9.png)
2.Greater than (>) Operator:
The greater than Operator, denoted by the symbol ">", determines if the first operand is greater than the second. If so, it gives a true result. Otherwise, false is returned.
Example:
10 > 20 => T = 1.
Symbol: >
Let's take an example to understand the use of greater than (>) operator:
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("%d is greater than %d",a,b);
}
else
{
printf("%d is less than %d",a,b);
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions10.png)
3.Less than or equal to (<=) Operator:
The less-than-or-equal-to Operator, denoted by the symbol "<=", determines if the first operand is less than or equal to the second operand. If so, it gives a true or false response.
Example:
100 <= 200 => T = 1.
Symbol: <=
Let's take an example to understand the use of less than or equal to (<=) operator:
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("%d is greater than %d",a,b);
}
else
{
printf("%d is less than %d",a,b);
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions11.png)
4.Greater than or equal to (>=) Operator:
The greater than or equal to Operator, denoted by the symbol '>=', determines whether the first operand is greater than or equal to the second. If so, it returns true; otherwise, it returns false.
Example:
50 >= 50 => T = 1.
Symbol:
>=
Let's take an example to understand the use of greater than or equal to (>=) operator:
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a>=b)
{
printf("%d is greater than or equals %d",a,b);
}
else
{
printf("%d is less than or equals %d",a,b);
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions12.png)
5.Equals to (==) Operator: Equals to Operator check whether values are equal from the given operands. Equal to Operator given Boolean value one if it holds.
Example:
50 == 50 => T = 1.
Symbol: ==
Let's take an example to understand the use equal to (==) operator:
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a==b)
{
printf("%d is equals to %d",a,b);
}
else
{
printf("%d not equals to %d",a,b);
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions13.png)
6.Not Equals to (!=) Operator:
Not Equals to Operator checks values are equal or not from the given operands. Not Equal to Operator given Boolean value one if it holds.
Example:
60! = 50 => T = 1.
Symbol: !=
Let's take an example to understand the use not equal to (!=) operator:
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
if(a!=b)
{
printf("%d is not equals to %d",a,b);
}
else
{
printf("%d equals to %d",a,b);
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions14.png)
The above given examples are the basic relational operators that can be used in various programs. Now let us make a program that covers all the relational operators in one program and observe the output.
C program to implement all the relational Operators:
We will give two numbers in the example below and check all the relational operators. If it is satisfied, then the output will be one else. Otherwise, the output is 0.
#include<stdio.h>
void main()
{
int a,b,x;
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
x=(a<b);
printf("a<b=%d\n",x);
x=(a>b);
printf("a>b=%d\n",x);
x=(a<=b);
printf("a<=b=%d\n",x);
x=(a>=b);
printf("a>=b=%d\n",x);
x=(a==b);
printf("a==b=%d\n",x);
x=(a!=b);
printf("a!=b=%d\n",x);
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions15.png)
Relational operators can also be used to compare the strings
Relational operators can also be used to compare the strings.
Let us understand it using a C program.
Example:
#include <stdio.h>
int main() {
char* a = "tutorial";
char* b = "examples";
if (a == b)
{
printf("The given two strings are equal");
}
else if (a > b)
{
printf("a is greater than b");
}
else
{
printf("a is smaller than b");
}
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions16.png)
Relational operators have a wide range of applications in computer programming.
Logical Operators (&&, ||, !):
The logical operators are the symbols that combine multiple conditions into one condition. The following table provides information about logical operators.
Operator | Meaning | Example |
&& | Logical AND - Returns TRUE if all conditions are TRUE; otherwise, returns FALSE | 10 < 5 && 12 > 10 is FALSE |
|| | Logical OR - Returns FALSE if all conditions are FALSE; otherwise, returns TRUE | 10 < 5 || 12 > 10 is TRUE |
! | Logical NOT - Returns TRUE if the condition is FLASE and returns FALSE if it is TRUE | !(10 < 5 && 12 > 10) is TRUE |
⇒ Logical AND - Returns TRUE only if all conditions are TRUE; if any is FALSE, then the complete condition becomes FALSE.
⇒ Logical OR - Returns FALSE only if all conditions are FALSE; if any conditions are TRUE, then the complete condition becomes TRUE.
Example:
Let's take an example to understand the use logical operators:
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 20;
int num3 = 0;
// Logical AND
if (num1 > 0 && num2 > 0) {
printf("Logical AND: Both num1 and num2 are greater than 0.\n");
} else {
printf("Logical AND: At least one of num1 or num2 is not greater than 0.\n");
}
// Logical OR
if (num1 > 0 || num2 > 0) {
printf("Logical OR: At least one of num1 or num2 is greater than 0.\n");
} else {
printf("Logical OR: Neither num1 nor num2 is greater than 0.\n");
}
// Logical NOT
if (!num3) {
printf("Logical NOT: num3 is 0.\n");
} else {
printf("Logical NOT: num3 is not 0.\n");
}
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions17.png)
Bitwise Operators (&, |, ^, ~, >>, <<):
The bitwise operators perform bit-level operations in the c programming language. When we use the bitwise operators, the operations are performed based on the binary values. The following table describes all the bitwise operators in the C programming language.
Two variables, A and B, are A = 25 (11001) and B = 20 (10100).
Operator | Meaning | Example |
& | The result of Bitwise AND is 1 if all the bits are 1; otherwise, it is 0. | A & B ⇒ 16 (10000) |
| | The result of Bitwise OR is 0 if all the bits are 0; otherwise, it is 1. | A | B ⇒ 29 (11101) |
^ | The result of Bitwise XOR is 0 if all the bits are the same; otherwise, it is 1. | A ^ B ⇒ 13 (01101) |
~ | The result of Bitwise once complement is the negation of the bit (Flipping). | ~A ⇒ 6 (00110) |
<< | The Bitwise left shift operator shifts all the bits to the left by the specified number of positions. | A << 2 ⇒ 100 (1100100) |
>> | The Bitwise right shift operator shifts all the bits to the right by the specified number of positions. | A >> 2 ⇒ 6 (00110) |
Example:
Let's take an example to understand the use Bitwise operators:
#include <stdio.h>
int main() {
unsigned int num1 = 10; // Binary: 1010
unsigned int num2 = 5; // Binary: 0101
// Bitwise AND
unsigned int result_and = num1 & num2;
printf("Bitwise AND: %u\n", result_and); // Output: 0 (Binary: 0000)
// Bitwise OR
unsigned int result_or = num1 | num2;
printf("Bitwise OR: %u\n", result_or); // Output: 15 (Binary: 1111)
// Bitwise XOR
unsigned int result_xor = num1 ^ num2;
printf("Bitwise XOR: %u\n", result_xor); // Output: 15 (Binary: 1111)
// Bitwise NOT (Unary)
unsigned int result_not_num1 = ~num1;
printf("Bitwise NOT (num1): %u\n", result_not_num1); // Output: 4294967285 (Binary: 11111111111111111111111111110101)
unsigned int result_not_num2 = ~num2;
printf("Bitwise NOT (num2): %u\n", result_not_num2); // Output: 4294967290 (Binary: 11111111111111111111111111111010)
// Bitwise Left Shift
unsigned int result_left_shift = num1 << 2;
printf("Bitwise Left Shift: %u\n", result_left_shift); // Output: 40 (Binary: 101000)
// Bitwise Right Shift
unsigned int result_right_shift = num1 >> 2;
printf("Bitwise Right Shift: %u\n", result_right_shift); // Output: 2 (Binary: 10)
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions18.png)
Conditional Operator:
The conditional Operator is also called a ternary operator because it requires three operands. This Operator is used for decision-making. In this Operator, we first verify a condition, and then perform one operation out of the two based on the condition result. If the condition is TRUE, the first option is performed; if the condition is FALSE, the second option is performed. The conditional Operator is used with the following syntax.
Condition? TRUE Part : FALSE Part;
Here's an example program that demonstrates the use of conditional operators in C:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
(num % 2 == 0)? printf("The given number is even") : printf("The given number is odd");
return 0;
}
Output:
![C expressions](https://static.tutorialandexample.com/c/c-expressions19.png)
Special Operators (sizeof, pointer, comma, dot, etc.):
The following are the special operators in a c programming language.
sizeof Operator:
This Operator is used to find the size of the memory (in bytes) allocated for a variable. This Operator is used with the following syntax.
sizeof(variableName);
Example:
sizeof(A); ⇒ The result is 2 if A is an integer.
Pointer operator (*):
This Operator is used to define pointer variables in a c programming language.
Comma operator (,):
This Operator is used to separate variables while declaring, separate the expressions in function calls, etc.
Dot operator (.):
This Operator is used to access members of a structure or union.
C Operator Precedence and Associativity:
Operator Precedence:
The order in which operators are evaluated in an expression is determined by their operator precedence. Every Operator in the C programming language has precedence (priority). The Operator with the highest precedence is evaluated first, and the Operator with the lowest precedence is evaluated last when multiple operators exist in an expression.
Operator Associativity:
The order in which operators with equal precedence are evaluated in an expression is determined by operator associativity. When an expression in the c programming language has multiple operators with equal precedence, we use associativity to determine the order in which those operators are evaluated.
Below table shows the operator precedence and associativity in the C programming language.
Precedence | Operator | Operator Meaning | Associativity |
1 | () [] -> . | function call array reference structure member access structure member access | Left to Right |
2 | ! ~ + - ++ -- & * sizeof (type) | negation 1's complement Unary plus Unary minus increment operator decrement operator address of Operator pointer returns size of a variable type conversion | Right to Left |
3 | * / % | multiplication division remainder | Left to Right |
4 | + - | addition subtraction | Left to Right |
5 | << >> | left shift right shift | Left to Right |
6 | < <= > >= | less than less than or equal to greater than greater than or equal to | Left to Right |
7 | operators == != | equal to not equal to | Left to Right |
8 | & | bitwise AND | Left to Right |
9 | ^ | bitwise EXCLUSIVE OR | Left to Right |
10 | | | bitwise OR | Left to Right |
11 | && | logical AND | Left to Right |
12 | || | logical OR | Left to Right |
13 | ?: | conditional Operator | Left to Right |
14 | Operators = *= /= %= += -= &= ^= |= <<= >>= | assignment assign multiplication assign division assign remainder assign addition assign subtraction assign bitwise AND assign bitwise XOR assign bitwise OR assign left shift assign right shift | Right to Left |
15 | , | separator | Left to Right |
The operator precedence in the table above increases from bottom to top and reduces from top to bottom.
C Expression Evaluation:
An expression is assessed in the C programming language based on the operator precedence and associativity. When more than one Operator is present in an expression, each one is assessed based on its precedence and associativity. The Operator with the highest precedence is evaluated first, followed by the Operator with the lowest.
The precedence and associativity of the operators in an expression are used to evaluate it.
Let's look at the following straightforward example expression to learn how expression evaluation works in C.
10 + 4 * 3 / 2
Three operators, +, *, and /, are present in the previous phrase. Multiplication and division have a higher precedence than addition among these three operators, although addition has a lower precedence. As a result, addition is assessed after division and multiplication by the operator precedence. Since division and multiplication have the same precedence, associativity is used to evaluate them. In this case, multiplication and division are associated from left to right. Thus, multiplication, division, and addition operations are performed in that order. The above statement is therefore evaluated in the following order: *, /, and +. The evaluation is done as follows...
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
The expression is evaluated to 16..