If you are a beginner in C language and you are confused about the usage of expression statements in C, then this article is your guide.
Expression Statement in C
While working in C language, variables are used to store values, strings, alphabets, and other such data. To assign any value to a variable or you can say to store any value inside a variable, the expression statement is used.
How to Use Expression Statement in C
The expression statement can be of different types that can be used to assign value to a variable or print a value at the output. It can be a numerical expression like addition, subtraction, division, or multiplication.
Example 1:
An example for the simplest expression statement is given below:
The above expression statement assigns a value to a variable. Keep in mind that while using any expression statement in C it is important to add a semicolon at the end of it otherwise the statement will show an error while running the code.
Example 2:
There are many other expression statements in C where more than one variable/operand are present and an operation is applied to them:
For instance;
Other operators in C expression statement are:
a = b - c; //Subtraction
a = b / c; //Division
a = b * c ; //Multiplication
Example 3:
Another common expression statement in C is the expression for increment. If a user wants to increase the variable value by one, they can follow the below-mentioned expression statement, the a++ works the same as a=a+1;
Example 4:
There is another expression statement that prints the value or message on the output window. This expression statement is assigned as follows:
These were just a few examples, by using these same operators many other expression statements can be created to perform different tasks through programs.
Here is a complete C code that uses the expression statements.
int main()
{
//expression statements
int a=2;
printf(“The value of a is: %d”,a);
}
Output:
Conclusion
Expression statements in C are the major part of any C code whether to assign the value to a variable perform different mathematical operations or printing the value at the output. The above-mentioned guidelines provide useful examples for C programmers to learn about these expression statements.