C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 2)
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;
}
Result = -100.000000
Result = -25.000000
Result = 0.000000
Result = 100.000000
Answer: Option
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

Discussion:
18 comments Page 1 of 2.

Sankari said:   1 decade ago
Can anybody explain?

2*(-50)/2*2.

-100/2*2.

In this step precedence is first go for multiplication only.

-100/4 = -25.
(7)

Jessie said:   1 decade ago
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

Varun said:   8 years ago
@Prakash Mishra.

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

Veera said:   9 years ago
Please explain the order of Operators.

Prakash Mishra said:   1 decade ago
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.

Vinod said:   1 decade ago
#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?

Sri bhargavi said:   1 decade ago
What is the need of return statement?

MANOJ SHNAKAR REDDY KARRI said:   1 decade ago
@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

Rancy said:   1 decade ago
Can anybody tell the order of operators according to there precedence ?

Srivibha said:   1 decade ago
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?


Post your comments here:

Your comments will be displayed after verification.