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.

struct ok
   int ok;
int main()
   // int ok; // This Statement will give you an Error.
   printf("Value of ok.ok is %d\n",ok.ok);
      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