Tuesday 25 December 2012

C/C++ Puzzles: PART - 4


No Initialization for External Variables

Consider the program given below.

#include<stdio.h>
int main()
{
    extern int aaa=10;
    printf("Value is %d\n",aaa);
    return 0;
}

Download 

When you compile the program you will get the following error message, " 'aaa' has both 'extern' and initializer". Reason? You should not initialize an externally declared variable. 

The version given below corrects the error.

#include<stdio.h>
int main()
{
    extern int aaa;
    printf("Value is %d\n",aaa);
    return 0;
}
aaa=333;

The Figure Given below shows the Output


No comments:

Post a Comment