Interview Questions

C Back

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]);
}

Ans: 

Compiler error: Lvalue required in function main

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

main()
{
int i=1;
while (i<=5)
{
printf("%d",i);
if (i>2) goto here; i++;
}
}
fun() { here: printf("PP");
}

Ans: 

Compiler error: Undefined label 'here' in function main.

Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main..

main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}

Ans: 

H

* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so its value is H. Again & references it to an address and * dereferences it to the value H..

main()
{
int i=400,j=300;
printf("%d..%d");
}

Ans: 

400..300

Printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assignments given in the program,then printf will take garbage values..

void main()
{
char far *farther,*farthest;
printf("%d..%d",sizeof(farther),sizeof(farthest));
}

Ans: 

4..2

The second pointer is of char type and not a far pointer.

Enum colors {BLACK,BLUE,GREEN}
main() {
printf("%d..%d..%d",BLACK,BLUE,GREEN);
return(1);
}

Ans: 

0..1..2

Enum assigns numbers starting from 0, if not explicitly defined.

main() {
clrscr();
}
clrscr();

Ans: 

No output/error

The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).

main()
{
printf("%p",main);
}

Ans: 

Some address will be printed.

Function names are just addresses (just like array names are addresses).main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers..

#include #define a 10 main()
{
#define a 50 printf("%d",a);
}

Ans: 

50

The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken..

#define square(x) x*x main()
{
int i;
i = 64/square(4);
printf("%d",i);
}

Ans: 

64

The macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64.