Friday 23 September 2011

Predict the output or error(s) for the following:

main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;

for(j=0;j<5;j++) {
printf(” %d “,*c);
++q;     }

for(j=0;j<5;j++){

printf(” %d “,*p);

++p;     }
}

Answer:

2 2 2 2 2 2 3 4 6 5

Explanation: 

Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

No comments:

Post a Comment