C Programming - C Preprocessor - Discussion

2. 

What will be the output of the program?

#include<stdio.h>
#define SQUARE(x) x*x

int main()
{
    float s=10, u=30, t=2, a;
    a = 2*(s-u*t)/SQUARE(t);
    printf("Result = %f", a);
    return 0;
}

[A]. Result = -100.000000
[B]. Result = -25.000000
[C]. Result = 0.000000
[D]. Result = 100.000000

Answer: Option A

Explanation:

The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.

Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,

=> a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .

=> a = 2 * (10 - 30 * 2) / 2 * 2;

=> a = 2 * (10 - 60) / 2 * 2;

=> a = 2 * (-50) / 2 * 2 ;

=> a = 2 * (-25) * 2 ;

=> a = (-50) * 2 ;

=> a = -100;

Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

Hence the output of the program is -100


Srivibha said: (Oct 31, 2010)  
a = 2 * (-50) / 2 * 2 ;
As per c language *,/,% has same precedence.
so it should evaluate 2*(-50) first. but why its evaluating (-50)/2 first?

Maithili said: (Dec 5, 2010)  
But,
2*(-50)/2*2
= 2*(-25)/2
= -25

( # E T # @ N said: (Feb 8, 2011)  
But C language considers from left 2 right associative

2*(-50)/2*2
-100/2*2
-50*2
-100

C H said: (Jul 28, 2011)  
When we define the macro, we should define it with a ( ) to avoid the confusion.

This is a simple mistake for programmer.

#define SQUARE(x) (x*x)

Then the answer will be -25 .

Pruthvi said: (Aug 16, 2011)  
As / and * have equal priority then we should conside associativity na... as it is from left to right then how come we divide it first..??

Akhila said: (Nov 6, 2011)  
But C language considers from left 2 right associative

2*(-50)/2*2
-100/2*2
-50*2
-100

Rupinderjit said: (Dec 28, 2011)  
@Chetan is quite right in his explanation.

Sonalia said: (Dec 29, 2011)  
As C language considers from left 2 right associative.

it should be as fallows.

2*(-50)/2*2
-100/2*2
-50*2
-100

Rupinderjit said: (May 20, 2012)  
Don't the negative sign first be represented by 2's complement and then proceed afterwards?

Jessie said: (Aug 30, 2012)  
The given explanation is wrong.becoz *,/ have same priority.It has left 2 right associative.
(i.e)
The left operand should not be involved in any operations with other operators.
in 2*(-50)/2*2
the left operand of first * is not involved in anything.so it should be operated first.
-100/2*2
-50*2
-100

Rancy said: (Oct 7, 2012)  
Can anybody tell the order of operators according to there precedence ?

Manoj Shnakar Reddy Karri said: (Dec 10, 2012)  
@Rancy yeah I can tell about the order of operators according to there precedence and it is as follows:

Summary of Operators and Precedence

The highest priority operators are listed first.

Operator Operation Evaluated.

() parentheses left to right
[] square brackets left to right

++ increment right to left
-- decrement right to left
(type) cast operator right to left
* the contents of right to left
& the address of right to left
- unary minus right to left
~ one's complement right to left
! logical NOT right to left

* multiply left to right
/ divide left to right
% remainder (MOD) left to right

+ add left to right
- subtract left to right

>> shift right left to right
<< shift left left to right

> is greater than left to right
>= greater than or equal to left to right
<= less than or equal to left to right
< less than left to right

== is equal to left to right
!= is not equal to left to right

& bitwise AND left to right
^ bitwise exclusive OR left to right
| bitwise inclusive OR left to right
&& logical AND left to right
|| logical OR left to right

= assign right to left
+= add assign right to left
-= subtract assign right to left
*= multiply assign right to left
/= divide assign right to left
%= remainder assign right to left
>>= right shift assign right to left
<<= left shift assign right to left
&= AND assign right to left
^= exclusive OR assign right to left
|= inclusive OR assign right to left

Sri Bhargavi said: (Jan 2, 2013)  
What is the need of return statement?

Sankari said: (Aug 24, 2013)  
Can anybody explain?

2*(-50)/2*2.

-100/2*2.

In this step precedence is first go for multiplication only.

-100/4 = -25.

Vinod said: (May 12, 2014)  
#include <stdio.h>
#include <string.h>
#define PRODUCT(x) ( x * x )
main( )
{
int i = 3, j, k ;
j = PRODUCT( i++ ) ;
k = PRODUCT ( ++i ) ;

printf ( "\n%d %d", j, k ) ;
}


It is showing 12 and 49 as output how come 49 instead of 42 can anyone explain please?

Prakash Mishra said: (Jul 6, 2014)  
x*x
i++ *i++

Since this is postfix so no increment will be take place until assignment.

So j=3*3=9;

And after assignment i will increase to 4,5 so 5 will be considered.

After that,
++i *++i= is the prefix so values will be increase first then it will multiply.

So values increase like this 6,7 so 7 is last increment so 7 will be considered.

Finally 7*7 = 49.

Veera said: (Dec 10, 2016)  
Please explain the order of Operators.

Varun said: (Dec 23, 2017)  
@Prakash Mishra.

According to your explanation for 49 j should evaluate to 9 but it evaluates to 12. Can anybody else explain that?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.