How to check for undefined variables.

G

Grand-Master

Consider the following program:

#include <stdio.h>

int main( void )
{
int foo;

return 0;
}

How would I get this program to output an error message stating that the
variable foo was not assigned a value? Is there a special reserved
phrase that I can use in the fashion as EOF or NULL?

Thanks!

-Gordon
 
I

Ian Collins

Grand-Master said:
Consider the following program:

#include <stdio.h>

int main( void )
{
int foo;

return 0;
}

How would I get this program to output an error message stating that the
variable foo was not assigned a value?

Not in standard C, but you compiler probably has an appropriate warning
level. Failing that, lint.
 
N

Nick Keighley

Grand-Master said:
Consider the following program:
#include <stdio.h>

int main( void )
{
int foo;

return 0;
}

How would I get this program to output an error message stating that the
variable foo was not assigned a value? Is there a special reserved
phrase that I can use in the fashion as EOF or NULL?

this is compiler specific. Ask in a ng specific to your compiler. Often

increasing the optimisation level enables this (the compiler has to
analyse the program's flow and as a side effects can tell if a variable
was used without being initialised.
 
G

Greg

Grand-Master said:
Consider the following program:

#include <stdio.h>

int main( void )
{
int foo;

return 0;
}

How would I get this program to output an error message stating that the
variable foo was not assigned a value? Is there a special reserved
phrase that I can use in the fashion as EOF or NULL?

You would have to enable a warning for "unused variables" and then
configure the compiler to treat warnings as errors.

Though that last step in particular might be somewhat draconian. After
all, the uninitialized foo in this program is not causing a problem of
any kind. Were main() to return the uninitialized foo - then that would
be a problem. In that case you would need to enable a different warning
- the one about a value being used before it is initialized.

Greg
 
J

J. J. Farrell

Grand-Master said:
Consider the following program:

#include <stdio.h>

int main( void )
{
int foo;

return 0;
}

How would I get this program to output an error message stating that the
variable foo was not assigned a value? Is there a special reserved
phrase that I can use in the fashion as EOF or NULL?

No; the value is indeterminate, and accessing it results in undefined
behaviour. Some compilers/environments may provide a mechanism to check
this at runtime, or may force some sort of program failure if it is
accessed, but that's implementation specific.

Some compilers have options to check for use of uninitialised variables
at compile time, but they don't always catch all cases, and often give
warnings for situations which are valid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top