The following questions were collected by Gowri Kumar.
Refer his website for the latest version: http://www.gowrikumar.com/
The following C program segfaults of IA-64,
but works fine on IA-32.
1 int main()
2 {
3 int* p;
4 p = (int*)malloc(sizeof(int));
5 *p = 10;
6 return 0;
7 }
Why does it happen so?
The expected output of the following C program
is to print the elements in the array. But when
actually run, it doesn't do so.
1 #include<stdio.h>
2
3 #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
4 int array[] = {23,34,12,17,204,99,16};
5
6 int main()
7 {
8 int d;
9
10 for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
11 printf("%d\n",array[d+1]);
12
13 return 0;
14 }
Find out what's going wrong.
The following program doesn't "seem" to print "hello-out".
(Try executing it)
1 #include <stdio.h>
2 #include <unistd.h>
3 int main()
4 {
5 while(1)
6 {
7 fprintf(stdout,"hello-out");
8 fprintf(stderr,"hello-err");
9 sleep(1);
10 }
11 return 0;
12 }
What could be the reason?
1 #include <stdio.h>
2 #define f(a,b) a##b
3 #define g(a) #a
4 #define h(a) g(a)
5
6 int main()
7 {
8 printf("%s\n",h(f(1,2)));
9 printf("%s\n",g(f(1,2)));
10 return 0;
11 }
Just by looking at the program one "might" expect the output to be, the
same for both the printf statements.
But on running the program you get it as:
bash$ ./a.out
12
f(1,2)
bash$
Why is it so?
1 #include<stdio.h>
2 int main()
3 {
4 int a=10;
5 switch(a)
6 {
7 case '1':
8 printf("ONE\n");
9 break;
10 case '2':
11 printf("TWO\n");
12 break;
13 defa1ut:
14 printf("NONE\n");
15 }
16 return 0;
17 }
If you expect the output of the above program to be
NONE, I would request you to check it out!!
Here is a small piece of program(again just 14 lines of program)
which counts the number of bits set in a number.
|
Input
|
Output
|
|
0
|
0(0000000)
|
|
5
|
2(0000101)
|
|
7
|
3(0000111)
|
1 int CountBits (unsigned int x )
2 {
3 static unsigned int mask[] = { 0x55555555,
4 0x33333333,
5 0x0F0F0F0F,
6 0x00FF00FF,
7 0x0000FFFF
8 } ;
9
10 int i ;
11 int shift ; /* Number of positions to shift to right*/
12 for ( i =0, shift =1; i < 5; i ++, shift *= 2)
13 x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
14 return x;
15 }
Find out the logic used in the above program.
1 void duff(register char *to, register char *from, register int count)
2 {
3 register int n=(count+7)/8;
4 switch(count%8){
5 case 0: do{ *to++ = *from++;
6 case 7: *to++ = *from++;
7 case 6: *to++ = *from++;
8 case 5: *to++ = *from++;
9 case 4: *to++ = *from++;
10 case 3: *to++ = *from++;
11 case 2: *to++ = *from++;
12 case 1: *to++ = *from++;
13 }while( --n >0);
14 }
15 }
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.
1 int CountBits(unsigned int x)
2 {
3 int count=0;
4 while(x)
5 {
6 count++;
7 x = x&(x-1);
8 }
9 return count;
10 }
Are the following two function prototypes same?
1 int foobar(void);
2 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:
1 #include <stdio.h>
2 void foobar1(void)
3 {
4 printf("In foobar1\n");
5 }
6
7 void foobar2()
8 {
9 printf("In foobar2\n");
10 }
11
12 int main()
13 {
14 char ch = 'a';
15 foobar1();
16 foobar2(33, ch);
17 return 0;
18 }
Program 2:
1 #include <stdio.h>
2 void foobar1(void)
3 {
4 printf("In foobar1\n");
5 }
6
7 void foobar2()
8 {
9 printf("In foobar2\n");
10 }
11
12 int main()
13 {
14 char ch = 'a';
15 foobar1(33, ch);
16 foobar2();
17 return 0;
18 }
What's the output of the following program and why?
1 #include <stdio.h>
2 int main()
3 {
4 float a = 12.5;
5 printf("%d\n", a);
6 printf("%d\n", *(int *)&a);
7 return 0;
8 }
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
1 int arr[80];
File2.c
1 extern int *arr;
2 int main()
3 {
4 arr[1] = 100;
5 return 0;
6 }
Explain the output of the following C program
(No, the output is not 20).
1 #include<stdio.h>
2 int main()
3 {
4 int a=1;
5 switch(a)
6 { int b=20;
7 case 1: printf("b is %d\n",b);
8 break;
9 default:printf("b is %d\n",b);
10 break;
11 }
12 return 0;
13 }
What is the output of the following program?
(Again, it is not 40, (if the size of integer is 4)).
1 #define SIZE 10
2 void size(int arr[SIZE])
3 {
4 printf("size of array is:%d\n",sizeof(arr));
5 }
6
7 int main()
8 {
9 int arr[SIZE];
10 size(arr);
11 return 0;
12 }
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?
1 #include <stdlib.h>
2 #include <stdio.h>
3 void Error(char* s)
4 {
5 printf(s);
6 return;
7 }
8
9 int main()
10 {
11 int *p;
12 p = (int*)malloc(sizeof(int));
13 if(p == NULL)
14 {
15 Error("Could not allocate the memory\n");
16 Error("Quitting....\n");
17 exit(1);
18 }
19 else
20 {
21 /*some stuff to use p*/
22 }
23 return 0;
24 }
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)
1 #include <stdio.h>
2 int main()
3 {
4 char c;
5 scanf("%c",&c);
6 printf("%c\n",c);
7
8 scanf(" %c",&c);
9 printf("%c\n",c);
10
11 return 0;
12 }
What is the potential problem with the following C program?
1 #include <stdio.h>
2 int main()
3 {
4 char str[80];
5 printf("Enter the string:");
6 scanf("%s",str);
7 printf("You entered:%s\n",str);
8
9 return 0;
10 }
What is the output of the following program?
1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 i = 10;
6 printf("i : %d\n",i);
7 printf("sizeof(i++) is: %d\n",sizeof(i++));
8 printf("i : %d\n",i);
9 return 0;
10 }
Why does the following program give a warning? (Please remember
that sending a normal pointer to a function requiring const pointer
does not give any warning) 1 #include <stdio.h>
2 void foo(const char **p) { }
3 int main(int argc, char **argv)
4 {
5 foo(argv);
6 return 0;
7 }
What is the output of the following program?
1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 i = 1,2,3;
6 printf("i:%d\n",i);
7 return 0;
8 }
The following is a piece of code which implements
the reverse Polish Calculator. There is a(are) serious(s) bug in
the code. Find it(them) out!!!
Assume that the function getop returns the appropriate return values
for operands, opcodes, EOF etc..
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX 80
5 #define NUMBER '0'
6
7 int getop(char[]);
8 void push(double);
9 double pop(void);
10 int main()
11 {
12 int type;
13 char s[MAX];
14
15 while((type = getop(s)) != EOF)
16 {
17 switch(type)
18 {
19 case NUMBER:
20 push(atof(s));
21 break;
22 case '+':
23 push(pop() + pop());
24 break;
25 case '*':
26 push(pop() * pop());
27 break;
28 case '-':
29 push(pop() - pop());
30 break;
31 case '/':
32 push(pop() / pop());
33 break;
34 /* ...
35 * ...
36 * ...
37 */
38 }
39 }
40 }
The following is a simple program which implements a minimal
version of banner command available on most *nix systems.
Find out the logic used in the program.
1 #include<stdio.h>
2 #include<ctype.h>
3
4 char t[]={
5 0,0,0,0,0,0,12,18,33,63,
6 33,33,62,32,62,33,33,62,30,33,
7 32,32,33,30,62,33,33,33,33,62,
8 63,32,62,32,32,63,63,32,62,32,
9 32,32,30,33,32,39,33,30,33,33,
10 63,33,33,33,4,4,4,4,4,4,
11 1,1,1,1,33,30,33,34,60,36,
12 34,33,32,32,32,32,32,63,33,51,
13 45,33,33,33,33,49,41,37,35,33,
14 30,33,33,33,33,30,62,33,33,62,
15 32,32,30,33,33,37,34,29,62,33,
16 33,62,34,33,30,32,30,1,33,30,
17 31,4,4,4,4,4,33,33,33,33,
18 33,30,33,33,33,33,18,12,33,33,
19 33,45,51,33,33,18,12,12,18,33,
20 17,10,4,4,4,4,63,2,4,8,
21 16,63
22 };
23
24 int main(int argc,char** argv)
25 {
26
27 int r,pr;
28 for(r=0;r<6;++r)
29 {
30 char *p=argv[1];
31
32 while(pr&&*p)
33 {
34 int o=(toupper(*p++)-'A')*6+6+r;
35 o=(o<0||o>=sizeof(t))?0:o;
36 for(pr=5;pr>=-1;--pr)
37 {
38 printf("%c",( ( (pr>=0) && (t[o]&(1<<pr)))?'#':' '));
39
40 }
41 }
42 printf("\n");
43 }
44 return 0;
45 }
46
The following is the offset macros which is used many a times.
Figure out what is it trying to do and what is the advantage of
using it.
1 #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
The following is the macro implementation of the famous,
Triple xor swap.
1 #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))
What are the potential problems with the above macro?
What does the format specifier %n of printf function do?
What is the use of the following macro?
1 #define DPRINTF(x) printf("%s:%d\n",#x,x)
Let's say you were asked to code a function IAddOverFlow which takes three
parameters, pointer to an integer where the result is to be stored, and
the two integers which needs to be added. It returns 0 if there is
an overflow and 1 otherwise:
1 int IAddOverFlow(int* result,int a,int b)
2 {
3 /* ... */
4 }
So, how do you code the above function?
(To put in a nutshell, what is the logic you use for overflow detection?)
What does the following macro do?
1 #define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))
Most of the C programming books, give the following example for
the definition of macros.
1 #define isupper(c) (((c) >= 'A') && ((c) <= 'Z'))
But there would be a serious problem with the above definition of
macro, if it is used as follows (what is the problem??)
2 char c;
3 /* ... */
4 if(isupper(c++))
5 {
6 /* ... */
7 }
But most of the libraries implement the isupper
(declared in ctypes.h) as a macro (without any side effects).
Find out how isupper() is implemented on your system.
What is the output of the following program?
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
5
6 #define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
7 int main()
8 {
9 /* The powers of 10 */
10 int pot[] = {
11 0001,
12 0010,
13 0100,
14 1000
15 };
16 int i;
17
18 for(i=0;i<SIZEOF(pot);i++)
19 PrintInt(pot[i]);
20 return 0;
21 }
The following is the implementation of the Euclid's
algorithm for finding the G.C.D(Greatest Common divisor)
of two integers.
Explain the logic for the below implementation and think
of any possible improvements on the current implementation.
BTW, what does scanf function return?
1 #include <stdio.h>
2 int gcd(int u,int v)
3 {
4 int t;
5 while(v > 0)
6 {
7 if(u > v)
8 {
9 t = u;
10 u = v;
11 v = t;
12 }
13 v = v-u;
14 }
15 return u;
16 }
17
18 int main()
19 {
20 int x,y;
21 printf("Enter x y to find their gcd:");
22 while(scanf("%d%d",&x, &y) != EOF)
23 {
24 if(x >0 && y>0)
25 printf("%d %d %d\n",x,y,gcd(x,y));
26 printf("Enter x y to find their gcd:");
27 }
28 printf("\n");
29 return 0;
30 }
31
Also implement a C function similar to the above to
find the GCD of 4 integers.
I hope you know that ellipsis (...) is used to specify
variable number of arguments to a function.
(What is the function prototype declaration for printf?)
What is wrong with the following delcaration?
1 int VarArguments(...)
2 {
3 /*....*/
4 return 0;
5 }
Write a C function which does the addition of two integers
without using the '+' operator. You can use only the bitwise
operators.(Remember the good old method of implementing
the full-adder circuit using the or, and, xor gates....)
How do you print I can print % using the printf function?
(Remember % is used as a format specifier!!!)
What's the output of the following program. (No, it's not 10!!!)
1 #include <stdio.h>
2 #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
3 int main()
4 {
5 int y = 100;
6 int *p;
7 p = (int*)malloc(sizeof(int));
8 *p = 10;
9 y = y/*p; /*dividing y by *p */;
10 PrintInt(y);
11 return 0;
12 }
What's the difference between the following two C statements?
1 const char *p;
2 char* const p;
What is the difference between memcpy and memmove?
What is the format specifiers for printf to print double and float values?
The following is a simple C program to read a date
and print the date. Run it and explain the behaviour
1 #include <stdio.h>
2 int main()
3 {
4 int day,month,year;
5 printf("Enter the date (dd-mm-yyyy) format including -'s:");
6 scanf("%d-%d-%d",&day,&month,&year);
7 printf("The date you have entered is %d-%d-%d\n",day,month,year);
8 return 0;
9 }
The following is a simple C program to read and
print an integer. But it is not working properly.
What is(are) the mistake(s)?
1 #include <stdio.h>
2 int main()
3 {
4 int n;
5 printf("Enter a number:\n");
6 scanf("%d\n",n);
7
8 printf("You entered %d \n",n);
9 return 0;
10 }
The following is a simple C program which tries to multiply
an integer by 5 using the bitwise operations. But it doesn't do so.
Explain the reason for the wrong behaviour of the program.
1 #include <stdio.h>
2 #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
3 int FiveTimes(int a)
4 {
5 int t;
6 t = a<<2 + a;
7 return t;
8 }
9
10 int main()
11 {
12 int a = 1, b = 2,c = 3;
13 PrintInt(FiveTimes(a));
14 PrintInt(FiveTimes(b));
15 PrintInt(FiveTimes(c));
16 return 0;
17 }
Is the following a valid C program?
1 #include <stdio.h>
2 #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
3 int max(int x, int y)
4 {
5 (x > y) ? return x : return y;
6 }
7
8 int main()
9 {
10 int a = 10, b = 20;
11 PrintInt(a);
12 PrintInt(b);
13 PrintInt(max(a,b));
14 }
Write a small C program to determine whether a
machine's type is little-endian or big-endian.
The following is a piece of C code, whose intention
was to print a minus sign 20 times. But you can notice
that, it doesn't work.
1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 int n = 20;
6 for( i = 0; i < n; i-- )
7 printf("-");
8 return 0;
9 }
Well fixing the above code is straight-forward.
To make the problem interesting, you have to fix
the above code, by changing exactly one character.
There are three known solutions. See if you can get
all those three.
What's the mistake in the following code?
1 #include <stdio.h>
2 int main()
3 {
4 int* ptr1,ptr2;
5 ptr1 = (int*)malloc(sizeof(int));
6 ptr2 = ptr1;
7 *ptr2 = 10;
8 return 0;
9 }
What is the output of the following program?
1 #include <stdio.h>
2 int main()
3 {
4 int cnt = 5, a;
5
6 do {
7 a /= cnt;
8 } while (cnt --);
9
10 printf ("%d\n", a);
11 return 0;
12 }
What is the output of the following program?
1 #include <stdio.h>
2 int main()
3 {
4 int i = 6;
5 if( ((++i < 7) && ( i++/6)) || (++i <= 9))
6 ;
7 printf("%d\n",i);
8 return 0;
9 }
What is the bug in the following program?
1 #include <stdlib.h>
2 #include <stdio.h>
3 #define SIZE 15
4 int main()
5 {
6 int *a, i;
7
8 a = (int*) malloc(SIZE*sizeof(int));
9
10 for (i=0; i<SIZE; i++)
11 *(a + i) = i * i;
12 for (i=0; i<SIZE; i++)
13 printf("%d\n", *a++);
14 free(a);
15 return 0;
16 }
Is the following a valid C program? If so, what is the output
of it?
1 #include <stdio.h>
2 int main()
3 {
4 int a=3, b = 5;
5
6 printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
7 printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
8 2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
9 return 0;
10 }
Write a C program which prints Hello World! without using
a semicolon!!!
What is the output of the following, if the input provided is:
Life is beautiful
1 #include <stdio.h>
2 int main()
3 {
4 char dummy[80];
5 printf("Enter a string:\n");
6 scanf("%[^a]",dummy);
7 printf("%s\n",dummy);
8 return 0;
9 }
Write a C program to find the
smallest of three integers, without using any of the
comparision operators.