17

I have problem creating global variable inside function, this is simple example:

int main{
   int global_variable;  //how to make that
}

This is exactly what I want to do:

int global_variable;
int main{
                   // but I wish to initialize global variable in main function
}
Jordan
  • 1,433
  • 8
  • 14
user3137147
  • 243
  • 1
  • 4
  • 8

4 Answers4

22

You have two problems:

  1. main is not a loop. It's a function.

  2. Your function syntax is wrong. You need to have parentheses after the function name. Either of these are valid syntaxes for main:

     int main() {
     }
    
     int main(int argc, const char* argv[]) {
     }
    

Then, you can declare a local variable inside main like so:

int main() {
  int local_variable = 0;
}

or assign to a global variable like so:

int global_variable;

int main() {
  global_variable = 0;
}
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • 5
    But can I initialize global variable inside main? – user3137147 Dec 30 '13 at 21:04
  • 1
    @user3137147 No. A global variable is by definition declared (and perhaps initialised) in the global namespace. – Joseph Mansfield Dec 30 '13 at 21:04
  • @sftrabbit Perhaps I am getting mixed up, but isn't your last example initializing your `global_variable` inside main? Or is that not considered initialization? – Jordan Dec 30 '13 at 21:06
  • @Jordan Initialisation is when you give an object a value as soon as it is brought into existence. If the `= 0;` had been in the variable declaration, it would have been initialisation. There are other ways to initialise variables too (constructor initialization list, function arguments, etc.). – Joseph Mansfield Dec 30 '13 at 21:10
  • @Jordan `global_variable` *is* being initialized with the value 0 in that example, but not because of the assignment. Objects with static storage duration (like `global_variable`) are automatically zero-initialised. So really, the line in `main` is unnecessary. – Joseph Mansfield Dec 30 '13 at 21:12
  • @sftrabbit I see, I always though that initialization is just when you first give a variable a value, not that it _has_ to happen right when you declare it. – Jordan Dec 30 '13 at 21:12
12

There is no way to declare it the way you want. And that's it.

But:

  • First, if you want you can declare it before the main body but assign a value to it inside main. Look Paul's answer for that
  • Second, actually there is no advantage of declaring variables the way you want. They are global and that means they should be declared in the global scope and no other places.
sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
6
int global_variable;
int main()
{
               global_variable=3; // look you assigned your value.
}
Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
1

well... its indirectly possible by declaring pointers global, and later assigning local variables to them, but sometimes it may lead to situations where pointed variable is unaccessible .

  • 1
    just put memory adress of the object to the pointers in short ;' void *x,*y,*z; int main(){ char localx[500]="hello eorld from local area"; x=&localx; }' ; probably will do the task simuleneusly; – General Chaos Jun 22 '18 at 05:20