Monday, March 10, 2008

C C++ Programming questions/puzzles

What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check it out again)

#include <stdio.h>


 

int
main()

{


float
f=0.0f;


int
i;


 


for(i=0;i<10;i++)


f
=
f
+
0.1f;


 


if(f
==
1.0f)


printf("f is 1.0 \n");


else


printf("f is NOT 1.0\n");


 


return
0;

}


 

I thought the following C program is perfectly valid (after reading about the comma operator in C). But there is a mistake in the following program, can you identify it?

#include <stdio.h>


 

int
main()

{


int
a
=
1,2;


printf("a : %d\n",a);


return
0;

}


 

What would be the output of the following C program? (Is it a valid C program?)

#include <stdio.h>

int
main()

{


int
i=43;


printf("%d\n",printf("%d",printf("%d",i)));


return
0;

}


 


void
duff(register
char
*to, register
char
*from, register
int
count)
{

register
int
n=(count+7)/8;

switch(count%8){

case
0: do{ *to++
=
*from++;

case
7: *to++
=
*from++;

case
6: *to++
=
*from++;

case
5: *to++
=
*from++;

case
4: *to++
=
*from++;

case
3: *to++
=
*from++;

case
2: *to++
=
*from++;

case
1: *to++
=
*from++;
}while( --n
>0);
}
}

Is the above valid C code? If so, what is it trying to acheive and why would anyone do something like the above?


 

Here is yet another implementation of CountBits. Verify whether it is correct (how do you that???). If so, find out the logic used.


int
CountBits(unsigned
int
x)
{

int
count=0;

while(x)
{

count++;

x
=
x&(x-1);
}

return
count;
}


 

Are the following two function prototypes same?


int
foobar(void);

int
foobar();

The following programs should be of some help in finding the answer: (Compile and run both the programs and see what happens)
Program 1:


#include <stdio.h>


void
foobar1(void)
{

printf("In foobar1\n");
}


void
foobar2()
{

printf("In foobar2\n");
}


int
main()
{

char
ch
=
'a';

foobar1();

foobar2(33, ch);

return
0;
}

Program 2:


#include <stdio.h>


void
foobar1(void)
{

printf("In foobar1\n");
}


void
foobar2()
{

printf("In foobar2\n");
}


int
main()
{

char
ch
=
'a';

foobar1(33, ch);

foobar2();

return
0;
}


 

What's the output of the following program and why?


#include <stdio.h>


int
main()
{

float
a
=
12.5;

printf("%d\n", a);

printf("%d\n", *(int
*)&a);

return
0;
}


 

The following is a small C program split across files. What do you expect the output to be, when both of them compiled together and run?
File1.c


int
arr[80];

File2.c


extern
int
*arr;

int
main()
{

arr[1] =
100;

return
0;
}


 

Explain the output of the following C program (No, the output is not 20).


#include<stdio.h>


int
main()
{

int
a=1;

switch(a)
{ int
b=20;

case
1: printf("b is %d\n",b);

break;

default:printf("b is %d\n",b);

break;
}

return
0;
}


 

What is the output of the following program? (Again, it is not 40, (if the size of integer is 4)).


#define SIZE
10


void
size(int
arr[SIZE])
{

printf("size of array is:%d\n",sizeof(arr));
}


int
main()
{

int
arr[SIZE];

size(arr);

return
0;
}


 

The following is a simple c program, in which there is a function called Error to display errors. Can you see a potential problem with the way Error is defined?


#include <stdlib.h>


#include <stdio.h>


void
Error(char*
s)
{

printf(s);

return;
}


int
main()
{

int
*p;

p
=
malloc(sizeof(int));

if(p
==
NULL)
{

Error("Could not allocate the memory\n");

Error("Quitting....\n");

exit(1);
}

else

{

/*some stuff to use p*/

}

return
0;
}


 

What is the differnce between the following function calls to scanf?(Please notice the space carefully in the second call. Try removing it and observe the behaviour of the program)


#include <stdio.h>


int
main()
{

char
c;

scanf("%c",&c);

printf("%c\n",c);


scanf(" %c",&c);

printf("%c\n",c);


return
0;
}