If I declare a variable and not use it in the program body, will it compile?

0

I have a program and I left a declared variable, but I did not use it in the program body.

    
asked by anonymous 19.12.2018 / 02:36

3 answers

3

Depends on the compiler configuration. You can enable or disable this. It has a very simple way of figuring out the standard of your compiler, compile under these conditions and see if it compiles. I advise to keep it connected and not leave a variable declared and unused, it ends up forgetting it there and it will disorganize the code, if it did not use it, erase it, if it needs more forward create again.

Note that the variable will not be created in the application. At least in C variables are only created if they have use, the question is just organization of the code. In fact even used variables can be removed after compiled, variable is a concept only exists in code. Whether it will take up space in memory or not is a secondary issue to this. In many cases even used variables disappear completely and everything ends up being done in the register, not consuming memory.

For those of you who have never seen compiled for this reason, it's just see ideone a simple code that does not compile .

    
19.12.2018 / 02:39
1

I've never seen a situation where the program does not compile for that reason, but that's not enough to say it will always work.

If you compile and the compiler uses an optimization technique called DCE (Dead Code Elimination ), the variable will not be part of the final compilation result. More details, for example: link

    
19.12.2018 / 03:04
0

If you want to keep just to remember you can mark it as momentary, so there is no risk of occupying unnecessary memory, although as already quoted by the above colleagues, depending on the configuration the compiler will remove.

    
19.12.2018 / 06:56