Wednesday, June 17, 2009

Tackling C Interviews


Last Sunday I met an interesting person who is supposedly my relative. He works as an Interviewer and a Technical Analyst as well. I was a lot more interested when he told me that his field of interest is also C/UNIX. We spoke for a while about general things and this is something I grabbed from him during the short convo. I am hoping to interact a lot more with him in the near future J it’s very hard to find people who really “LOVE” the technology and are passionate about C or UNIX. I know people who do hundreds and thousand lines of coding even without knowing the basic mechanism of how something work “internally”. Well… as I always say, “We need not know everything; just know things in detail”.


This is a bit lengthier. Read it when you are relatively free.

Finally, here it is…The Simple Question

The moment I ask anyone what is the different between a Structure and a Union in C, they immediately pop up one of the below answers,

“I think Structure is memory waste while Union saves memory”

“Oh, that’s simple! In Union only one item can be accessed while in Structure all the items have their own individual memory”

“Hmmm, the size of a Union will be the size of the biggest variable”

None of those answers were wrong. Now, let me ask you something more…

Consider 5 variables with the below sizes:

Var1 – 2
Var2 – 4
Var3 – 2
Var4 – 1
Var5 – 8

As per the above definitions, each and every Variable in the structure has its own independent memory locations. So,

Size of Structure = Var1 + Var2 + Var3 + Var4 + Var5 = 17

Var1 | Var2 | Var3 | Var4 | Var5 |

If it were a Union , then it will look like below, assuming that Var5 is the biggest variable (in terms of size)


Size of this Union = Size of the biggest variable = Var5 = 8.

This is how it gets tricky

Now, how the other variables are accommodated? We say that at any point of time only one variable can be retrieved from the Union . What will happen to the existing var5 if a new value is assigned to a variable, say Var4? How will this single memory segment of size 8 be made available for rest of the variables as well?

Various irrelevant answers

You will be surprised to read… these are some of the REAL answers which I got from experienced people (especially in C).

Answer 1:

“I think Var1 will be stored first. It is just of size 2. Then Var2 is just 4 so it will also be stored, Var3 is 2 and the Var4 is just 1. But…. But…. The fifth variable var5 is too big to fit in there…I think…. Well… It’s something… ”


I think that is the functionality of Union . It will take care of it all by itself


X: I think C Compiler handles it or something to do with the Computer memory management?

Y: Say what? Computer memory management????

X: No... I meant the Compiler Memory Management… May be…

There really were many other such ridiculous answers. I am not humiliating anyone here. I myself was like this during my early days. However, once you get a hold and start working in C, should we not be taking some pain to know its mechanism? We sure should!

So how does Union work?

This is where people go wrong! Just knowing the definition will NOT do. Know its mechanism and that is what the Interviewer is expecting from you! Also, to explain these basic stuff you don not need a C Bible or Dennis Ritchie. All you need is a C Compiler. Trust me, most of the basic things can be learnt only by experiencing – not only in the Programming world but also in the real world! Go for it! Experience it! Enlighten yourself! The below short & simple program will make you understand how Union really differs from the Structures.

% cat structunion.c
#include <stdio.h>
#include <stdlib.h>
typedef union
int Wind_Chill;
char Heat_Index;
} Condition;
typedef struct
float temp;
Condition feels_like;
} Temperature;

void main()
Temperature *tmp;
tmp = (Temperature *)malloc(sizeof(Temperature));
printf("\nAddress of Temperature = %u", tmp);
printf("\nAddress of temp = %u, feels_like = %u", &(*tmp).temp, &(*tmp).feels_like);
printf("\nWind_Chill = %u, Heat_Index= %u\n", &((*tmp).feels_like).Wind_Chill, &((*tmp).feels_like).Heat_Index);

% cc -o structunion structunion.c

% ./structunion
Address of Temperature = 165496
Address of temp = 165496, feels_like = 165500
Wind_Chill = 165500, Heat_Index= 165500

Wasn’t that clear? To add on, let us consider the below quote from ANSI/ISO 9899:1990 (the ANSI C standard) Section
Structure and union specifiers have the same form. [ . . . ] The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit-field, then to the unit in which it resides), and vice versa.

Do It Yourself

Grab a C Compiler and try executing the below snippet. You will be more clear.

#include <stdio.h>
#include <stdlib.h>
/* Declares union */
union one{
char one;
int two;
float three;
/* Declares structure */
struct two{
char one;
int two;
float three;
int main(void)
/* Uses tag names to create structure S and union U. */
struct two S;
union one U;
/* Outputs object sizes to screen. */
printf("%d is the size of S, as structure.\n", sizeof(S));
printf("%d is the size of U, as union.\n\n", sizeof(U));
/* Loads values into S and U, as below. */ = 'A';
S.two = 3645;
S.three = 678.32; = 'A';
U.two = 3645;
U.three = 678.32;
/* Echos values to screen, so conclusions are easier. */
printf("The values 'A', 3645 and 678.32 have been loaded"
"in structure S members and\n union U members both.\n\n");
/* Shows values now in S members. */
printf("%c is equal to the value\n",;
printf("%d is equal to the value S.two\n", S.two);
printf("%3.2f is equal to the value S.three\n\n", S.three);
/* Shows the value now stored as corrupted and good. */
printf("%c is equal to the value\n",;
puts("Oops! Doesn't look like an 'A'!\n");
printf("%d is equal to the value U.two\n", U.two);
puts("Oops! Doesn't look like 3645 either!\n");
printf("%3.2f is equal to the value U.three\n\n", U.three);
/* Non-pertinent stuff for running under Windows and stuff */
return 0;

“Know the mechanism” – This helps you fine tune your programming skills and makes you an outstanding programmer. It helps you outshine in any interview as well for mechanism is what the Interviewers are after (Unless and until the one that is interviewing you is comparatively dumb haha)