Saturday, May 31, 2008

Unknown C more and more

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 does the format specifier %n of printf function do? 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. What is the output of the following program?

#include <stdio.h>
#include <stdlib.h>

#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))

#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
int main()
{
/* The powers of 10 */
int pot[] = {
0001,
0010,
0100,
1000
};
int i;

for(i=0;i<SIZEOF(pot);i++)
PrintInt(pot[i]);
return 0;
}

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?

#include <stdio.h>
int gcd(int u,int v)
{
int t;
while(v > 0)
{
if(u > v)
{
t = u;
u = v;
v = t;
}
v = v-u;
}
return u;
}

int main()
{
int x,y;
printf("Enter x y to find their gcd:");
while(scanf("%d%d",&x, &y) != EOF)
{
if(x >0 && y>0)
printf("%d %d %d\n",x,y,gcd(x,y));
printf("Enter x y to find their gcd:");
}
printf("\n");
return 0;
}

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?

int VarArguments(...)
{
/*....*/
return 0;
}

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

#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = 100;
int *p;
p = (int*)malloc(sizeof(int));
*p = 10;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return 0;
}

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

1 comment:

Anonymous said...

Sorry LemonSquash, But you are not sharing, you are jst printing the information.it would have been great, if you could have explained what all those you have so called 'shared'.These questions can be found any where, but It would have been great,if solutions are there..
thanks any ways for so called SHARING informations