Your Ad Here

Friday, November 14, 2008

C Interview Questions Answers - Vol 3

What will be the result of the following code?
#define TRUE 0 // some code while(TRUE) { // some code }
This will not go into the loop as TRUE is defined as 0.


What will be printed as the result of the operation below:
int x;
int modifyvalue()
{
return(x+=10);
}

int changevalue(int x)
{
return(x+=1);
}

void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%d\n",x);

x++;
changevalue(x);
printf("Second output:%d\n",x);
modifyvalue();
printf("Third output:%d\n",x);

}


Answer: 12 , 13 , 13


What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\n”,x,y);

}

Answer: 11, 16





What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0)
printf(“Tech Preparation\n”);
printf(“Tech Preparation\n”);

}

Answer: Two lines with “Tech Preparation” will be printed.


What will the following piece of code do
int f(unsigned int x)
{
int i;
for (i=0; x!0; x>>=1){
if (x & 0X1)
i++;
}
return i;
}

Answer: returns the number of ones in the input parameter X


What will happen in these three cases?
if(a=0){
//somecode
}
if (a==0){
//do something
}
if (a===0){
//do something
}


What are x, y, y, u
#define Atype int*
typedef int *p;
p x, z;
Atype y, u;

Answer: x and z are pointers to int. y is a pointer to int but u is just an integer variable


Advantages of a macro over a function?
Macro gets to see the Compilation environment, so it can expand __ __TIME__ __FILE__ #defines. It is expanded by the preprocessor.

For example, you can’t do this without macros
#define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR)

PRINT( 5+6*7 ) // expands into printf(”5+6*7=%d”, 5+6*7 );

You can define your mini language with macros:
#define strequal(A,B) (!strcmp(A,B))

Macros are a necessary evils of life. The purists don’t like them, but without it no real work gets done.

No comments: