This “bug” is really “buggy”

Did you ever ran a loop with counter as unsigned int then you must have faced this problem consider the following for loop which is expected to print numbers from 16 to 0

unsigned int i ;

for(i = 16;i>=0;i–){

printf(“%d “,i);

}

This loop doesn’t give output as 16 15 ……… 0 but runs on for ever.   Can you guess the reason(once give it a try!!!). Here it goes  once the value of i becomes 0 when i — is done the value changes from 0 to max value of unsigned int(depends upon the architecture of your system) and hence the value of 1 can never go below zero. Hence it’s an infinite loop 🙂

“EXTERN” keyword in “C”

First I would like to write What exactly does a declaration or definition of a variable mean:

Declartion and Definition:

In c declaration tells the compiler that you are going to use a variable name var(say) and it is of type int(say). This is what exactly a declaration does. Remember that just declaration doesn’t allocate any memory to the variable. What definition does is It allocates the memory block to the variable and assigns some value if specified. Most of the times you write int var you are actually declaring as well as defining it i.e you are telling the compiler that you are going use a variable name var which is used to refer to an integer and it allocate the sufficient memory if you give int var =0 it also assigns it some value. You are allowed to declare a variable as many times as you wish but you can define it only once. Once you define a variable you cannot redefine again.

So how do we declare a variable without defining it. Here comes our “extern” key word. extern int var just declares the variable without defining it you can later declare and define or even assign using int var =0;

Just to add, in c only uninitialized global variables are all made to zero but not local variables. The reason can be killing of performance if it is to initialise all the local variables each time corresponding function is called. But global variables are initialised only once and it’s done by CRT( C Run Time) 🙂