C Expressions
Syntax
expression1 <operator> expression2
where operator
is a C operator, and expression1
and expression2
are expressions.
Example
#include <stdio.h>
int main() {
int x = 5, y = 3, z;
z = x + y;
printf("The sum of %d and %d is %d\n", x, y, z);
z = x - y;
printf("The difference between %d and %d is %d\n", x, y, z);
z = x * y;
printf("The product of %d and %d is %d\n", x, y, z);
z = x / y;
printf("The quotient of %d divided by %d is %d\n", x, y, z);
z = x % y;
printf("The remainder of %d divided by %d is %d\n", x, y, z);
return 0;
}
Output
The sum of 5 and 3 is 8
The difference between 5 and 3 is 2
The product of 5 and 3 is 15
The quotient of 5 divided by 3 is 1
The remainder of 5 divided by 3 is 2
Explanation
C expressions are made up of one or more operands (values, variables, or functions) and one or more operators (symbols that represent an operation). An example of an expression is x + y
, where x
and y
are the operands, and +
is the operator. In this example, the +
operator performs the addition operation on the operands.
Use
C expressions are used extensively in C programming to perform arithmetic and logical operations, to manipulate variables, and to control program flow using control statements. Arithmetic expressions, which use arithmetic operators to perform mathematical calculations, are commonly used in calculations and formulas. Logical expressions, which use logical operators to evaluate the truth or falsity of a statement, are commonly used in conditional statements and loops.
Important Points
- C expressions are made up of one or more operands and one or more operators.
- Binary operators take two operands, while unary operators take one operand.
- The order in which expressions are evaluated is determined by operator precedence and associativity.
- Parentheses can be used to override the default order of evaluation.
- Some operators have side effects, which can cause unexpected behavior if not used correctly.
Summary
Expressions are the building blocks of C programs, allowing programmers to perform calculations, manipulate variables, and control program flow. Understanding the syntax and use of C expressions is critical to becoming a skilled C programmer. With this knowledge, you can write programs that perform complex calculations, make intelligent decisions, and interact with the world around us.