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 C language. In C every expression evaluates to a value i.e., every expression result 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 – c  a * b – c  
(m + n) (x + y) (m + n) * (x + y)                        
(ab / c)  a * b / c                        
3x2 +2x + 1  3*x*x+2*x+1                        
(x / y) + c  x / 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 presence of parenthesis in expressions.      
main ()    
{
float a, b, c x, y, z;    
a = 9;    
b = 12;   
 c = 3;    
x = a – b / 3 + c * 2 – 1;    
y = a – b / (3 + c) * (2 – 1);    
z = a – (b / (3 + c) * 2) – 1;    
printf (“x = %f\n”, x);    
printf (“y = %f\n”, y);    
printf (“z = %f\n”, z);    
}      
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 expression  in C Programming  

  • First parenthesized sub expression left to right are evaluated.
  • If 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 are used, the expressions within parenthesis assume highest priority. 

    

Operator precedence and associativity      

Each operator in C has a 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 same precedence are evaluated from right to left or from left to right depending on the level. This is known as associativity property of an operator.      

The table given below gives the precedence of each operator.

Operator precedence

 Statements and Comments

  • Expressions combine variables and constants to create new values.
  • Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
  • Comments are used to give additional useful information inside 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 //


Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours