Variable in main is global?

Asked

Viewed 366 times

12

I read that global variables are those that we declare outside of all functions using #define and this way they could be used by all 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 in main can only be used by other functions if we pass it by parameter.

3 answers

12


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

Global variables are actually declared out of function, but the #define are not even variables. They are only texts that are replaced before compilation, nothing else, disappears completely from the code.

1

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

1

For fixed and constant variables declaration:

COMMANDO:

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

const: Simulates a constant in memory, that is, creates a variable with fixed value, so the compiler allocates a space in memory to the variable.

  • 2

    That’s not what was asked but the biggest problem of the answer is that a value created with #define takes place in memory, like any data. Another problem is calling variable constant.

  • I looked in the Mazano book, actually the compiler understands so. I’ll take another look, but that’s how I learned.

  • Can you quote the passage that says this? Put context. Either you got it wrong or the book is bad.

  • Maybe I misunderstood, but Manzano is not bad not, if he was bad not kkk I have class with him until! " A constant in C language can be simulated by using the const command, which creates a variable in memory and sets a fixed (immutable) value which cannot be modified until the execution of the program is complete."

  • I just tidied up there, but the book was right, I ended up reversing the #define and the const.

  • But the answer is still wrong and still does not answer what was asked. If the book says it, I say it’s bad.

  • I don’t know, I don’t have a class with him, I didn’t read his book, I didn’t see the quote. So I can’t talk. But if I had that information, I could evaluate it since I have experience with it. Who has initial knowledge only, can not assess what is good or not, for that person the initial source she had is always good. It might be the best thing you could have access to or it might be something that’s teaching you all wrong.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.