Custom Search

Saturday, March 10, 2007

C Review - I

1. main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
Answer :
three
Explanation :
The default case can be placed anywhere inside the loop. It is executed only when all
other cases doesn't match.

2. main()
{
int c=2;
printf("c=%d",c);
}
Answer:
c=2;
Explanation:
Here unary minus (or negation) operator is used twice. Same maths rules applies, ie.
minus * minus= plus.

3. main()
{
printf("\nab");
printf("\bsi");
printf("\rha");
}
Answer:
hai
Explanation:
\n newline
\b backspace
\r linefeed


4. main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}
Answer:
compiler dependent
one answer is
45545
Explanation:
The arguments in a function call are pushed into the stack from left to right. The
evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result.


5. #define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}
Answer:
64
Explanation:
the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4
. Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64


6. #include
#define a 10
main()
{
#define a 50
printf("%d",a);
}
Answer:
50
it takes most recent value


7. #define clrscr() 100
main()
{
clrscr();
printf("%d\n",clrscr());
}
Answer:
100


8. main()
{
int i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}
Answer:
1
Explanation:
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input
which should have been scanned successfully. So number of items read is 1.


9. main()
{
main();
}
Answer:
Runtime error : Stack overflow.
Explanation:
main function calls itself again and again. Each time the function is called its return
address is stored in the call stack. Since there is no condition to terminate the function
call, the call stack overflows at runtime. So it terminates the program and results in an
error.


10. main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}
Answer:
2 5 5
Explanation:
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable.
In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0'
termination character). The third sizeof is similar to the second one.


11. main()
{
char not;
not=!2;
printf("%d",not);
}
Answer:
0
Explanation:
! is a logical operator. In C the value 0 is considered to be the boolean value FALSE,
and any nonzero
value is considered to be the boolean value TRUE. Here 2 is a nonzero
value so
TRUE. !TRUE is FALSE (0) so it prints 0.


12. #define FALSE 1
#define TRUE 1
#define NULL 0
main() {
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}
Answer:
TRUE
Explanation:
The input program to the compiler after processing by the preprocessor is,
main(){
if(0)
puts("NULL");
else if(1)
puts("TRUE");
else
puts("FALSE");
}
Preprocessor doesn't replace the values given inside the double quotes. The check by if
condition is boolean value false so it goes to else. In second if 1
is boolean value true hence "TRUE"
is printed.

14.
int i=10;
main()
{
extern int i;
{
int i=20;
{
const volatile unsigned i=30;
printf("%d",i);
}
printf("%d",i);
}
printf("%d",i);
}
Answer:
30,20,10


15. main()
{
int i=-1;
-i;
printf("i = %d, -i= %d \n",i,-i);
}
output: i=-1, i=1


16.
main()
{
const int i=4;
float j;
j = ++i;
printf("%d %f", i,++j);
}
Answer:
Compiler error
Explanation:
i is a constant. you cannot change the value of constant


17. main()
{
int i=_l_abc(10);
printf("%d\n",--i);
}
int _l_abc(int i)
{
return(i++);
}
Answer:
9
Explanation:
return(i++) it will first return i and then increments. i.e. 10 will be returned.


18. void main()
{
int k=ret(sizeof(float));
printf("\n here value is %d",++k);
}
int ret(int ret)
{
ret += 2.5;
return(ret);
}
answer : 7


19. void main()
{
unsigned giveit=-1;
int gotit;
printf("%u ",++giveit);
printf("%u \n",gotit=--giveit);
}
Answer:
0 65535


20. void main()
{
printf(“%d”,printf(“%s”,”hello”));
}
ans : hello5


21. void main()
{
int i;
char a[]="\0";
if(printf("%s\n",a))
printf("Ok here \n");
else
printf("Forget it\n");
}
Answer:
Ok here
Explanation:
Printf will return how many characters does it print. Hence printing a null character
returns 1 which makes the if statement true, thus "Ok here" is printed.


22. main()
{
unsigned int i=10;
while(i>=0)
printf("%u ",i);
}
answer : infinte loop, observe unsigned int


23.
main()
{
int x,y=2,z,a;
if(x=y%2) z=2;
a=2;
printf("%d %d ",z,x);
}
Answer:
Garbagevalue 0


24. main()
{
int i=5;
printf("%d",++i++);
}
Answer:
Compiler error: Lvalue required in function main
Explanation:
++i yields an rvalue. For postfix ++ to operate an lvalue is required.


25. main()
{
int i=5;
printf(“%d”,i=++i ==6);
}
Answer:
1
Explanation:
The expression can be treated as i = (++i==6), because == is of higher precedence
than = operator. In the inner expression, ++i is equal to 6 yielding true(1). Hence the
result.


26. void main()
{
int i=10, j=2;
int *ip= &i, *jp = &j;
int k = *ip/*jp;
printf(“%d”,k);
}
ans: unexpected end of line in the file at line 5


27. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Answer:
I hate U



Specify all the ways that u know.
1. write a program to swap 2 numbers without using additional operator.
i) +,
limitations : overflow and underflow
ii)*, /
limitations : overflow and underflow
if one number is zero it wont work.
iii)xor ^
perfect.

3. write a program to find a number is power of 2 or not
i) taking a/2 and doing till we get 1 or not divisible
ii) using logs
iii)n&n1==0

if u have any doubts or u neeed any clarifications send me a mail to suman.knr@gmail.com

No comments: