Monday, March 10, 2008

C Programming questions/puzzles

The following is a simple C program to read a date and print the date. Run it and explain the behaviour


#include <stdio.h>


int
main()
{

int
day,month,year;

printf("Enter the date (dd-mm-yyyy) format including -'s:");

scanf("%d-%d-%d",&day,&month,&year);

printf("The date you have entered is %d-%d-%d\n",day,month,year);

return
0;
}


 

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)?


#include <stdio.h>


int
main()
{

int
n;

printf("Enter a number:\n");

scanf("%d\n",n);


printf("You entered %d
\n",n);

return
0;
}


 

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.


#include <stdio.h>


#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))


int
FiveTimes(int
a)
{

int
t;

t
=
a<<2
+
a;

return
t;
}


int
main()
{

int
a
=
1, b
=
2,c
=
3;

PrintInt(FiveTimes(a));

PrintInt(FiveTimes(b));

PrintInt(FiveTimes(c));

return
0;
}


 

Is the following a valid C program?


#include <stdio.h>


#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))


int
max(int
x, int
y)
{
(x
>
y) ? return x :
return
y;
}


int
main()
{

int
a
=
10, b
=
20;

PrintInt(a);

PrintInt(b);

PrintInt(max(a,b));
}


 

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.


#include <stdio.h>


int
main()
{

int
i;

int
n
=
20;

for( i
=
0; i
<
n; i-- )

printf("-");

return
0;
}

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?


#include <stdio.h>


int
main()
{

int*
ptr1,ptr2;

ptr1
=
malloc(sizeof(int));

ptr2
=
ptr1;

*ptr2
=
10;

return
0;
}


 

What is the output of the following program?


#include <stdio.h>


int
main()
{

int
cnt
=
5, a;


do {

a
/=
cnt;
} while (cnt
--);


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

return
0;
}


 

What is the output of the following program?


#include <stdio.h>


int
main()
{

int
i
=
6;

if( ((++i
<
7) && ( i++/6)) || (++i
<=
9))
;

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

return
0;
}


 

What is the bug in the following program?


#include <stdlib.h>


#include <stdio.h>


#define SIZE
15


int
main()
{

int
*a, i;


a
=
malloc(SIZE*sizeof(int));


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

*(a
+
i) =
i
*
i;

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

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

free(a);

return
0;
}


 

Is the following a valid C program? If so, what is the output of it?


#include <stdio.h>


int
main()
{

int
a=3, b
=
5;


printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);

printf(&a["WHAT%c%c%c
%c%c
%c !\n"], 1["this"],

2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);

return
0;
}


 

What is the output of the following, if the input provided is:
Life is beautiful


#include <stdio.h>


int
main()
{

char
dummy[80];

printf("Enter a string:\n");

scanf("%[^a]",dummy);

printf("%s\n",dummy);

return
0;
}


 

Note : This question has more to do with Linker than C language

We have three files a.c, b.c and main.c respectively as follows:
a.c
---

int
a;

b.c
---

int
a
=
10;

main.c
------

extern
int
a;

int
main()

{


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


return
0;

}

Let's see what happens, when the files are compiled together:

bash$ gcc a.c b.c main.c

bash$ ./a.out

a = 10

Hmm!! no compilation/linker error!!! Why is it so??


 

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.


#define offsetof(a,b) ((int)(&(((a*)(0))->b)))


 

The following is the macro implementation of the famous, Triple xor swap.


#define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))

What are the potential problems with the above macro?


 

What is the use of the following macro?


#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:


int
IAddOverFlow(int*
result,int
a,int
b)
{

/* ... */

}

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?


#define ROUNDUP(x,n) ((x+n-1)&(~(n-1)))


 

Most of the C programming books, give the following example for the definition of macros.


#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??)


char
c;

/* ... */


if(isupper(c++))
{

/* ... */

}

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.


 

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?


int
VarArguments(...)
{

/*....*/


return
0;
}


 

Write a C program to find the smallest of three integers, without using any of the comparision operators.


 

What does the format specifier %n of printf function do?


 

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 difference between the following two C statements?


const
char
*p;

char*
const
p;


 

What is the difference between memcpy and memmove?


 

What is the format specifiers for printf to print double and float values?


 

Write a small C program to determine whether a machine's type is little-endian or big-endian.


 

Write a C program which prints Hello World! without using a semicolon!!!