Expression, Statements and Comments in C Programming | C Programming
- Get link
- X
- Other Apps
Expression, Statements, and Comments in C Programming
Arithmetic C Expressions in C Programming
An expression is a combination of variables constants and operators written according to the syntax of the C language. In C every expression evaluates to a value i.e., every expression results in some value of a certain type that can be assigned to a variable. Some examples of C expressions are shown in the table given below.
Algebraic C Expression in C programming
Expression
a x b - ca * b – c
(m + n) (x + y) (m + n) * (x + y)
(ab / c)a * b / c
3x2 +2x + 13*x*x+2*x+1
(x / y) + cx / y + c
Evaluation of Expressions in C Programming
Expressions are evaluated using an assignment statement of the form
Variable = expression;
Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first and then replaces the previous value of the variable on the left-hand side. All variables used in the expression must be assigned values before evaluation is attempted.
Example of evaluation statements are
x = a * b - c
y = b / c * a
z = a - b / c + d;
The following program illustrates the effect of the presence of parenthesis in expressions.
Output
x = 10.00
y = 7.00
z = 4.00
Precedence in Arithmetic Operators in C Programming
An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators. There are two distinct priority levels of arithmetic operators in C.
High priority * / %
Low priority + -
Rules for evaluation of an expression in C Programming
- First, parenthesized sub-expressions left to right are evaluated.
- If the parenthesis is nested, the evaluation begins with the innermost sub-expression.
- The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions.
- The associability rule is applied when two or more operators of the same precedence level appear in the sub-expression.
- Arithmetic expressions are evaluated from left to right using the rules of precedence.
- When Parenthesis is used, the expressions within parenthesis assume the highest priority.
Operator precedence and associativity
Each operator in C has precedence associated with it. The precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of these levels. The operators of higher precedence are evaluated first. The operators of the same precedence are evaluated from right to left or from left to right depending on the level. This is known as the associativity property of an operator.
The table given below gives the precedence of each operator.
Statements and Comments
- Expressions combine variables and constants to create new values.
- Statements are expressions, assignments, function calls, or control flow statements that makeup C programs.
- Comments are used to give additional useful information about a C Program.
- All the comments will be put inside /*...*/ as given in the example above.
- A comment can span through multiple lines.
- Multi line Comments use /*…*/
- Single Line Comments use //
- Get link
- X
- Other Apps
Comments
Post a Comment
Subscribe Us and Thanks for visiting blog.