Pages

Friday, December 19, 2008

About printf Part 2




1.
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.

2.
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

Explanation:
'{' introduces new block and thus new scope. In the innermost block i is declared as, const volatile unsigned which is a valid declaration. i is assumed of type int. So printf prints 30. In the next block, i has value 20 and so printf prints 20. In the outermost block, i is declared as extern, so no storage space is allocated for it. After compilation is over the linker resolves it to global variable i (since it is the only variable visible there). So it prints i's value as 10.

3.
aaa() {
printf("hi");
}

bbb(){
printf("hello");
}

ccc(){
printf("bye");
}

main(){

int (*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[2]=ccc;
ptr[2]();
}

Answer:
bye

Explanation:
ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc.

4.
main(){
FILE *ptr;
char i;
ptr=fopen("zzz.c","r");
while((i=fgetc(ptr))!=EOF)
printf("%c",i);
}

Answer:
Contents of zzz.c.

5.
void main(){
void *v;
int integer=2;
int *i=&integer;
v=i;
printf("%d",(int*)*v);
}

Answer:
Compiler Error. We cannot apply indirection on type void*.

Explanation:
Void pointer is a generic pointer type. No pointer arithmetic can be done on it. Void pointers are normally used for,

1>
Passing generic pointers to functions and returning such pointers.
2>
As a intermediate pointer type.
3>
Used when the exact pointer type will be known at a later point of time.

6.
void main(){
while(1){
if(printf("%d",printf("%d")))
break;
else
continue;
}
}

Answer:
Garbage values.

Explanation:
The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement.

7.
main(){
unsigned int i=10;
while(i-->=0)
printf("%u ",i);
}

Answer:
9 8 7 6 5 4 3 2 1 0 65535 65534…..

Explanation:
Since i is an unsigned integer it can never become negative. So the expression i-- >=0 will always be true, leading to an infinite loop.

8.
main(){
int a[10];
printf("%d",*a+1-*a+3);
}

Answer:
4

Explanation:
*a and -*a cancels out. The result is as simple as 1 + 3 = 4 !

9.
main(){
unsigned int i=65000;
while(i++!=0);
printf("%d",i);
}

Answer:
1

Explanation:
Note the semicolon after the while statement. When the value of i becomes 0 it comes out of while loop. Due to post-increment on i the value of i while printing is 1.

10.
void pascal f(int i,int j,int k){
printf(“%d %d %d”,i, j, k);
}

void cdecl f(int i,int j,int k){
printf(“%d %d %d”,i, j, k);
}

main(){
int i=10;
f(i++,i++,i++);
printf(" %d\n",i);
i=10;
f(i++,i++,i++);
printf(" %d",i);
}

Answer:
10 11 12 13

12 11 10 13

Explanation:
Pascal argument passing mechanism forces the arguments to be called from left to right. cdecl is the normal C argument passing mechanism where the arguments are passed from right to left.

11.
What is the output of the program given below:

main(){
signed char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}

Answer:
-128

Explanation:
Notice the semicolon at the end of the for loop. THe initial value of the i is set to 0. The inner loop executes to increment the value from 0 to 127 (the positive range of char) and then it rotates to the negative value of -128. The condition in the for loop fails and so comes out of the for loop. It prints the current value of i that is -128.

12.
main(){
unsigned char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);

}

Answer:
infinite loop.

Explanation:
The difference between the previous question and this one is that the char is declared to be unsigned. So the i++ can never yield negative value and i>=0 never becomes false so that it can come out of the for loop.

13.
main(){

char i=0;
for(;i>=0;i++) ;
printf("%d\n",i);
}

Answer:
Behavior is implementation dependent.

Explanation:
The detail if the char is signed/unsigned by default is implementation dependent. If the implementation treats the char to be signed by default the program will print –128 and terminate. On the other hand if it considers char to be unsigned by default, it goes to infinite loop.

Rule:

You can write programs that have implementation dependent behavior. But dont write programs that depend on such behavior.


That's it for today...
Thanks for reading.

No comments: