473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to check for undefined variables.

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
Oct 18 '06 #1
4 5017
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.
Oct 18 '06 #2
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

Oct 18 '06 #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?
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

Oct 18 '06 #4

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.

Oct 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Nuno Paquete | last post by:
Hi group. I'm using this code to see if is there any parameter for variable "menu": if($_GET == "downloads") .... But this code log errors if there is no parameter passed (this heappens at...
5
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
14
by: Mark | last post by:
Hi, I would like to check if my object has been deleted or not, but my program keeps crashing. Here's the simplified code (in infinite loop) delete tetromino; //if(tetromino==NULL)...
39
by: emre esirik(hacettepe computer science and enginee | last post by:
int n_mines; printf("How many mines do you want in the minefield?"); scanf("%d", &n_mines); while(n_mines>100 || n_mines<0) { printf("Please only enter between 1 and 100"); scanf("%d",...
3
by: Yansky | last post by:
If I have the following code: var abc; if(!abc){ alert('test'); }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.