Variable in main is global?

12

I have read that global variables are those that we declare out of all functions using #define and that way they could be used by all the functions of a program. However, I was informed that the variables declared in main are global. I'm pretty sure this is false, since the variables we declare on main can only be used by other functions if we pass them by parameter.

    
asked by anonymous 28.08.2015 / 03:18

3 answers

12

No, main() is a function like any other. The variables declared within it are only seen within it. The maximum that can be done is to indirectly access the addresses where these variables were allocated. Since everything declared in main() will exist for the lifetime of the application, this is possible. But it is something abnormal and as I said before, access to content is indirect and not by the variables themselves that can not be seen.

The global variables are actually declared out of functions, but the #define are not even variable. They are just texts that are replaced before compilation. Nothing more than this. It completely disappears from the code.

    
28.08.2015 / 03:22
1

Main is the main function of the program. And variables declared outside of any function (scope) are global. The ones within the functions are local.  Globals can be used in the entire program and sites can only be scoped.

    
29.08.2015 / 08:19
1

Regarding the declaration fixed and constant variables:

COMMAND:

#define: Creates a label for any value and does not take up memory space.

const: Simulate a constant in memory, that is, create a variable with fixed value, so the compiler allocates a space in memory for the variable.

    
31.08.2015 / 01:01