Saturday 19 May 2012

C Technical Questions


1.
void main()
{
            int  const p=5;
            printf("%d",++p);
}

Answer:
Compiler error: Cannot modify a constant value.



2.
main()
{
            char s[ ]="man";
            int i;
            for(i=0;s[ i ];i++)
            printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}

Answer:
mmmm
aaaa
nnnn

Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.


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



4.
main()
{
            static int var = 5;
            printf("%d ",var--);
            if(var)
                        main();
}

Answer:
5 4 3 2 1 


5.
main()
{
            int i=-1,j=-1,k=0,l=2,m;
            m=i++&&j++&&k++||l++;
            printf("%d %d %d %d %d",i,j,k,l,m);
}

Answer:
0 0 1 3 1
 7.  
main()
{
            char *p;
            printf("%d %d ",sizeof(*p),sizeof(p));
}

 
Answer:
1 2

Explanation:
   P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

No comments:

Post a Comment