Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger


Sunday, 13 July 2008

C Interview Questions and Answers 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

What does static variable mean?
there are 3 main uses for the static.
1. If you declare within a function:
It retains the value between function calls

2.If it is declared for a function name:
By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files

3. Static for global variables:
By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file

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.

What are the differences between malloc() and calloc()?
There are 2 differences.
First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable).
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

No comments:

Post a Comment