Features of c which wont be found in books
size of structure and pragma pack(4) 

This question is asked most of the time in C interviews and is not properly described in most of the books I have read. Reason behind the size of the structure, in some of the books it is written that some padding is added which is OS dependent but its not the whole truth.whenever we write some code like this : continue reading
unusual format specifiers.
Guyz I assume that most of the format specifiers of C which are frequently used are known by everyone of us e.g %d for int , %f for float , %c for char , %s for string , %ld for longint ,%lf for double etc so guyz here I am to talk about some other format specifier which are not usually seen in books or not taught in normal classes. so here is the list try to use them in C program, and let me know ,continue readingdynamic memory allocation.
hey guyz you would have done dynamic memory allocation several times, while making program for linked list, queues etc but have you ever tried this what I am going to show you? ok first things first a sample code for allocating heap memory and assigning its address to an integer pointer. then increasing the pointer by 1. this program compiles successfully but generates error on run time . why any guess? what effect does increasing the pointer has on free function.Continue reading.Inline Functions
Although youve already learned about basic functions in c++, there is more: the inline function. Inline functions are not always important, but it is good to understand them. The basic idea is to save time at a cost in space. Once you define an inline function, using the inline keyword, whenever you call that function the compiler will replace the function call with the actual code from the function.inline keyword may make function definintion is written on the place where function is being called to save the overhead of calling the function at runtime and optimize the run time of program.but compiler may or may not honor the request to make a function inline for reasons given below
1 if a function contains loop.
2 if function is recursive.
3 if function is has several lines.
4 if function declaration and definition is in seperate translation unit ( seperate file ).
inline int max(int a, int b)
{
return (a > b) ? a : b;
}
Then, a statement such as the following:
a = max(x, y);
may be transformed into a more direct computation:
a = (x > y) ? x : y;
Warning: it must be used very carefully because if we make some big function inline it can cause the code size to be increased and inturn the memory requirement of program and their run time will also be more.so it must be used for very small function in comparison to which function calling overhead is much higher than the definition itself.
volatile keyword
Volatile is keyword which may be a bit difficult to understand if one has no idea about low level programming.whenever we write a code like this#include<stdio.h>
int i =1;
int main()
{
while( i) // compile will automatically place the 1 instead of i while compiling
{
//some code;
}
}
compile will automatically place the 1 instead of i while compiling,since compiler always tries to optimize the code as much as possible and when it sees that nothing is modifying i and its value will always be 1 so it will write 1 inside while instead of i, but in low level programming value of i can be changed by some other external factor like some registers, some devices etc but since compiler has written 1 in while so would have no effect. So to stop this phenomena from happening we use volatile keyword which tells compiler there is something else which might change this variable other than this program so dont optimize it.
volatile int i =1;
int main()
{
while( i) // compile will not place the 1 instead of i.
{
//some code;
}
}
calling c and c++ function from eachother
it is a important one but for this one must understand the function overloading concept of c++. since due to the name mangling concept it becomes almost impossible to call a c function form C++ program and vice versa , but thanx to GCC that we have a feature named as extern "C". which give us a get around to this problem.let us see how...
since all of us know that c++ has unique feature named as function overloading which is achieved with the help of concept of name-mangling and this is the only reason we are restrained to call any c function defined in other file of c which is in binary format.so we use extern "C" compiler directive to stop the name-mangling mechanism and it let the name of function remain same as it is ,and if we have a source file of both the program than it is rather easy to implement by just compiling both the file together using g++ . but if we are calling a function of c file which is in binary format continue reading
since all of us know that c++ has unique feature named as function overloading which is achieved with the help of concept of name-mangling and this is the only reason we are restrained to call any c function defined in other file of c which is in binary format.so we use extern "C" compiler directive to stop the name-mangling mechanism and it let the name of function remain same as it is ,and if we have a source file of both the program than it is rather easy to implement by just compiling both the file together using g++ . but if we are calling a function of c file which is in binary format continue reading
__fpurge( stdin)
it flushes keyboard buffer, why is it necessary to flush the buffer?here is the reason: In a situation where we repetitively( in loop) use scanf to scan a char and press enter. scanf processes the request with the pressing of enter, but this enter is also stored in keyboard buffer and also considered as a character by scanf, so next time scanf comes scanning for character, it will not give you the chance to enter any character, because it will consider value of enter key as a char , and you will might endup frustrating that why your program is not working.so __fpurge( stdin) flushes keyboard buffer and lets you enter the next char.#include<stdio.h>
int main()
{
char c, i;
for ( i =0 ; i < 10; i++)
{
printf(" enter a char ");
__fpurge(stdin);
scanf("%c", &c);
printf( "char you entered: %c ",c);
}
}
try to run this program without using __fpurge(stdin) and you will get the idea easily.
printf( __func__)
last but not the least this line has its own importance while debugging the code.In which ever function this line is placed it will print the name of that function in terminal while running.this way one get to know the control flow and can easily debug the program.download file now