I was doing an assignment where this problem in a different scenario occurred (Actually I was trying to use a user defined class pointer in the same way, and it took me a long time to find the problem). I'm giving code snippets below:
#include<iostream>
using namespace std;
// int x = 5; this line works just fine
//whereas the below two lines don't
int x;
x = 5; //red squiggles here, under x. The error reads: this declaration has no storage class or type specifier
int main()
{
//x = 5; this line works too
cout<<x<<endl;
}
I know this might be a very basic question, but I'm still a beginner. Can someone explain to me why declaration in the global scope of a variable does not work? (I'm assuming the variable does not get a memory space until the main function starts, is it so?) I googled the problem, but couldn't find any good answers. Your help would be much appreciated TIA ^-^