How to check for undefined variables.

Discussion in 'C Programming' started by Grand-Master, Oct 18, 2006.

  1. Grand-Master

    Grand-Master Guest

    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
     
    Grand-Master, Oct 18, 2006
    #1
    1. Advertising

  2. Grand-Master

    Ian Collins Guest

    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.
     
    Ian Collins, Oct 18, 2006
    #2
    1. Advertising

  3. 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
     
    Nick Keighley, Oct 18, 2006
    #3
  4. Grand-Master

    Greg Guest

    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
     
    Greg, Oct 18, 2006
    #4
  5. 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.
     
    J. J. Farrell, Oct 19, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,844
    Dan Pop
    Feb 17, 2004
  2. Replies:
    9
    Views:
    1,008
  3. Tammo Tjarks
    Replies:
    2
    Views:
    303
    Tammo Tjarks
    Sep 13, 2007
  4. VK
    Replies:
    45
    Views:
    666
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    390
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page