How to declare global variable inside function?
57,635
Solution 1
You have two problems:
main
is not a loop. It's a function.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;
}
Solution 2
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 insidemain
. 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.
Solution 3
int global_variable;
int main()
{
global_variable=3; // look you assigned your value.
}
Solution 4
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 .
Related videos on Youtube
Author by
user3137147
Updated on January 28, 2022Comments
-
user3137147 over 2 years
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 }
-
Joseph Mansfield over 10 yearsLoop? I think you mean function. These are veeeery different things.
-
-
Joseph Mansfield over 10 years-1. For not having the parentheses and calling an assignment initialisation.
-
user3137147 over 10 yearsBut can I initialize global variable inside main?
-
Joseph Mansfield over 10 years@user3137147 No. A global variable is by definition declared (and perhaps initialised) in the global namespace.
-
Jordan over 10 years@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? -
Joseph Mansfield over 10 years@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 over 10 years@Jordan
global_variable
is being initialized with the value 0 in that example, but not because of the assignment. Objects with static storage duration (likeglobal_variable
) are automatically zero-initialised. So really, the line inmain
is unnecessary. -
Jordan over 10 years@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.
-
Boldijar Paul over 8 years@JosephMansfield Oops, fixed. After 3 years haha.
-
General Chaos about 6 yearsjust 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;