Wednesday, February 22, 2012

C question set 2

(Q-6)

main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t;
    for (i=0;i<=4;i++)
    printf("%s",names[i]);
}
Answer: Compiler error: Lvalue required in function main
Explanation:

Array names are pointer constants. So it cannot be modified.

(Q-7)

void main()
{
int i=5;
printf("%d",i++ + ++i);
}
Answer: Output Cannot be predicted exactly.
Explanation:

Side effects are involved in the evaluation of i

(Q-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

(Q-9)

#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Answer: 100


(Q-10)

main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:  1
Explanation:  before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0
(false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).




No comments:

Post a Comment