Even though we work for some years on any language, sometimes, we will not aware of some basic concepts of languages, which is becasue of we never use those in our coding styles. For example, if we are working on C language since 5 years and suddenly if you want to use snprintf() function, definitely you will go for Google...!!!
When i wanted to share a variable between two different C files, what i have declared a variable in a common header file for both the files. While testing the application, in one file where the value being assigned, the value was fine and in another file, the value of the variable is always 0. I was wondering..!! Thinking like, how can it happen...?
After all, discussed with my guide-for-all lecturere and then only i came to know that, when we include a header file in 2 different C files, scope of the variables will become limited to those files and those are separate for both the files. That means, if we declare a variable g_temp in abc.h file if we include ab.c in a.c and b.c, g_temp will be different for both the files and hence values are local to those files.
If we declare the variable in one C file and if we use extern keyword to refer to that, then it will work fine.
Otherwise, we have to have a function in one C file and to get and set the value of a global variable which is defined in the same file.
Another scenario:
In the same way, we will not aware of some basic concepts even though we are working on the same technology, becasue we have not used those concepts particularly. For example, in C language, we use switch case statements extensively. But somebody asks if we put one printf statement before the first case statement, what will happen ? We ll test manually or we ll go for google.
Recently only i came across about the above. If we put any statement before the first case statement, that will not be executed.
#define SUCCESS 1
int state=SUCCESS;
switch(State)
{
printf("Before the case statements starts");
case SUCCESS:
printf("Testing the switch case");
break;
default:
printf("default case");
}
In the above code, the printf statement before the case statement starts, will never executes. This looks very very simple, but i came to know very recently. May be this is obvious.
Keep knowing new things... Gud Luck...