Examples of Expressions

We have already seen the precedence of arithmetic operators. We have expressions for different calculations in algebraic form, and in our programs we write them in the form of C statements. Let’s discuss some more examples to get a better understanding.

 

We know about the quadratic equation in algebra, that is y = ax2 + bx + c. The quadratic equation in C will be written as y = a * x * x + b * x + c. In C, it is not an equation but an assignment statement. We can use parentheses in this statement, this will make the expression statement easy to read and understand. Thus we can rewrite it as y = a * (x * x) + (b * y) + c.

Note that we have no power operator in C, just use * to multiply the same value.

 

Here is another expression in algebra:  x = ax + by + cz2. In C the above expression will be as:

        x = a * x + b * y + c * z * z

The * operator will be evaluated before the + operator. We can rewrite the above statement with the use of parentheses. The same expressions can be written as:

        x = (a * x) + (b * y) + c * ( z * z)

 

Lets have an other expression in algebra as x = a(x + b(y + cz2)). The parentheses in this equation forces the order of evaluation. This expression will be written in C as:

        x = a * (x + b * (y + c * z * z))

 

 

 

Previous

 

 

TOC

 

 

Next