#include<stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int a=4,z;
z=PRODUCT(a+1);
printf("z=%d\n",z);
return 0;
}
Solution
z=9
The expected output is 25. But the preprocessor replaces z=PRODUCT(4+1) as z=(4+1*4+1).* has higher preceddence than + . so the output is 4+4+1=>z=9
The expected output is 25. But the preprocessor replaces z=PRODUCT(4+1) as z=(4+1*4+1).* has higher preceddence than + . so the output is 4+4+1=>z=9
2.What is the output of the following program?
#include<stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int a=4,x,y;
x=PRODUCT(a++);
y=PRODUCT(++a);
printf("X=%d\t Y=%d\n",x,y);
return 0;
}
Solution
X=16 Y=64
3.What is the output of the following program?
#include<stdio.h>
#define SEMI ;
int main()
{
int x=4 SEMI ;
printf("X=%d\n",x)SEMI
return 0 SEMI
}
Solution
x=4
The program will not produce error.C statements should end with a semicolon. The compiler will take the 2nd ; as a empty statement in line 5.
The program will not produce error.C statements should end with a semicolon. The compiler will take the 2nd ; as a empty statement in line 5.
4.What is the output of the following program?
#include<stdio.h>
#define CHAR1 char
int main()
{
typedef char CHAR2;
CHAR1 x;
CHAR2 y;
x=255;
y=255;
printf("X=%d\tY=%d\n",x,y);
return 0;
}
Solution
X=-1 Y=-1
default value of char is signed char.so the value above 127 are taken as the 2's complement value
default value of char is signed char.so the value above 127 are taken as the 2's complement value
5.What is the output of the following program?
#include<stdio.h>
#define SIZE sizeof(int)
int main()
{
int i=-1;
if(i<SIZE)
printf("True\n");
else
printf("False\n");
return 0;
}
Solution
False
6.What is the output of the following program?
#include<stdio.h>
#define SIZE(a) (&a+1) - &a
int main()
{
float x;
printf("X=%d\n",SIZE(x));
return 0;
}
Solution
X=1
I cant understand the solution to the 2nd one. Any help??
ReplyDeleteme too
ReplyDelete//for the 2nd problem
ReplyDeleteConsider the 2nd program with added printf statements
#include
#define PRODUCT(x) (x*x)
int main()
{
int a=4,x,y;
x=PRODUCT(a++);
printf("a=%d\n",a);
y=PRODUCT(++a);
printf("a=%d\n",a);
printf("X=%d\t Y=%d\n",x,y);
return 0;
}
the output of the above program is ./a.out
a=6
a=8
X=16 Y=64
The Preprocessed file is given below
int main()
{
int a=4,x,y;
x=(a++*a++);
printf("a=%d\n",a);
y=(++a*++a);
printf("a=%d\n",a);
printf("X=%d\t Y=%d\n",x,y);
return 0;
}
initially a value is 4
x=(a++*a++);-----> a++ will assign a first then only increment the value so x=a*a=4*4=16 after that 4++ will give 5 and again a++=>5++=>6
So before the statement y=PRODUCT(++a); a value is 6. now y=(++a*++a); In this ++a will first increment the value of a and then assign the value. already a value is 6. So the first ++a=>++6 will incrment a to 7 and next ++a will increment 7 to 8. now a*a=8*8=64
y value is 64.
thanks for programming
ReplyDeletefrom gaurav and sameer