Thursday 3 January 2013

C/C++ Puzzles: PART - 16

Identifier Names can be Confusing
  
Names given to identifiers are often confusing to beginners in C/C++ programming. But sometimes even expert programmers will have their share of trouble with some of the valid identifier usage in C/C++. For example consider the code given below. It is a perfectly valid C code.

#include<stdio.h>
struct ok
{
   int ok;
}ok;
int main()
{
   // int ok; // This Statement will give you an Error.
   ok.ok=111;
   printf("Value of ok.ok is %d\n",ok.ok);
   ok:printf("Hello!!!\n");
   if(ok.ok==111)
   {
      ok.ok=222;
      goto ok;
   }
 }


Download the C Program. 
Download the C++ Program. 

The same identifier name ok is used as the structure name, a structure member, structure variable and a label for goto statement. Such a repeated use of same identifier is allowed in C/C++ until conflicts occur. If you add the statement int ok; to the program there arises a conflict and thus you will get an error. Otherwise there is no error. 

The Output is Shown Below

No comments:

Post a Comment