Top C Tips
History of C
Developed by Brain Kernighan and Dennis Ritchie of AT&T bell labs in 1972. In 1983 the American National Standards Institute began the standardisation process. In 1989 the International standards organisation continued the standardisation process.In 1990 a standard was finalised, known simply as "Standard C". Everything before this is known as "K&R C".
Boolean data type in C?!
There is no boolean data type in C, integers are used instead of that.
In general, the unary operators have higher precedence than binary operators.
Stack in C
C uses a stack to store local variables, it is also used when
passing parameters to functions.C stores local variables on stack. Global variables may be declared. These are not stack based, but they are placed in the data segment. Special keywords exist to specify where local variables are stored,
auto - place on the stack
static - place in the data segment.
register - place in a CPU register.
Way of writing pointer
ptr is similar to * ( ptr + 2 )
Pointer with NULL
We can declare invalid pointer by,
short *p= NULL;
we can use 'NULL' to define a invalid pointer.
Defining Volatile Variable
volatile int* ptr;
This declaration tells pointer is volatile.
int* volatile ptr;
This declaration tells actual address stored in the pointer is volatile.
Swaping two numbers
x^= y^= x^= y
1. sizeof(void) will give compilation error ( Not an allowed type )
2. sizeof('\0') is 1 ( tested in 32bit Compiler )
3. sizeof(NULL) is 4 byte ( tested in 32bit Compiler )
4. sizeof structure which contains one function pointer is 4byte ( Tested in 32bit Compiler )
typedef struct tagemp
int (*pfn)(int a, int b);
int main( void )
printf( "Structure size %d\n", sizeof( stemp ) );
To set a particular bit of a given number
#define SET_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) _NO_
To reset a particular bit of a given number. If a bit is 1, then we have to set 0 otherwise 1.
#define RESET_BIT( _X_, _NO_ ) ~( ( 1<<(_X_-1) ) ) & _NO_
Swapping a bit with another
#define SWAP_BIT( _X_, _NO_ ) ( 1<<(_X_-1)) ^ _NO_
sizeof(stemp) will be 8 bytes due to structure padding. Tested in 32bit compiler. This result may vary based on compiler.
ANSI C standard predefined macros
__LINE__ Inserts the current source code line number in your code.
__FILE__ Inserts the current source code filename in your code.
__DATE__ Inserts the current date of compilation in your code.
__TIME__ Inserts the current time of compilation in your code.
__STDC__ Is set to 1 if you are enforcing strict ANSI C conformity.
__cplusplus__ Is defined if you are compiling a C++ program.
printf( "My program name %s at line number %d", __FILE__, __LINE__ );
Don't do this ever
unsigned int ab, condition=0;
void main( void )
if( condition == 0 )
ab = -1;
ab = 0;
printf( "%x\n", ab );
if( ab < -1 )
printf( "Value of ab = %d %d\n", ab, sizeof(ab) );
printf( "Wrong\n" );
From the above code, we are assigning -1 to an unsigned integer. The value of variable 'ab' will get 0xffffffff once we assign -1 on it. So the condition will become wrong.
Beware of using unsigned integer data type.
Beware of sprintf
unsigned int ab=10;
void main( void )
sprintf( name, "%s", ab );
printf( "%s\n", name );
From the above sprintf code, We have converted a numeric value to string and stored in a character buffer. But, instead of giving sprintf( name, "%d", ab ); we gave sprintf( name, "%s", ab );. So that the variable 'name' will get the invalid string value. This will cause program to hang.
Beware of it.
Who will initialize the static & global variables?
Linker will initialize the value for static & global variables.
malloc(sizeof(0)) will return?
It will return a valid pointer.
Can we have constant volatile variable?
Yes, We can have a volatile pointer.
We cannot declare global register variables.
We can calculate stack overheads by analyzing the addresses of local variables.
16 bit value 0x1234 , convert into 0x1004
0x1234 & 0xF00F = 0x1004
how will u get 0x12 from 0x1234
Right Shift 8 times
( 0x1234 >> 8 ) = 0x12
- An integer
- A pointer to an integer
- A pointer to a pointer to an integer
- An array of 10 integers
- An array of 10 pointers to integers
- A pointer to an array of 10 integers
- A pointer to a function a that takes an integer argument and returns an integer
- An array of 10 pointers to functions that take an integer argument and return an integer