Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to check for undefined variables.

Reply
Thread Tools

How to check for undefined variables.

 
 
Grand-Master
Guest
Posts: n/a
 
      10-18-2006
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-18-2006
Grand-Master wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      10-18-2006
Grand-Master wrote:
> 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.

--
Nick Keighley

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-18-2006

Grand-Master wrote:
> 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

 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      10-18-2006

Grand-Master wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
.Net Pro include Visual Source Safe or other Code Check in Check out software? Davisro ASP .Net 1 06-14-2004 03:23 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments