473,321 Members | 1,669 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,321 software developers and data experts.

do/while loop and scope of automatic vars

Hi all,

Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.

Questions:

- Is this correct according to the ISO/ANSI C standards?
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?

TIA,
Erik
--
-----------------------------------------------------------------
Erik de Castro Lopo
-----------------------------------------------------------------
Of the four project development variables - scope, cost, time and
quality - quality isn't really a free variable. The only possible
values are "excellent" and "insanely excellent", depending on
whether lives are at stake." -- Kent Beck, XP Explained
Aug 21 '07 #1
8 4512
Erik de Castro Lopo <er***@mega-nerd.comwrites:
Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.

Questions:

- Is this correct according to the ISO/ANSI C standards?
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?
Did you mean to write that it *doesn't" realize that the scope extends
to the end of the conditional?

When I wrap your code in a main program and compile it with gcc, I get:

c.c: In function `main':
c.c:7: error: `r' undeclared (first use in this function)
c.c:7: error: (Each undeclared identifier is reported only once
c.c:7: error: for each function it appears in.)

gcc is correct. C99 6.2.1p4 says:

If the declarator or type specifier that declares the identifier
appears inside a block or within the list of parameter
declarations in a function definition, the identifier has _block
scope_, which terminates at the end of the associated block.

The block extends from the opening '{' to the closing '}'.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 21 '07 #2
On Aug 20, 11:01 pm, Erik de Castro Lopo <er...@mega-nerd.comwrote:
Hi all,

Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.
Because it doesn't.
>
Questions:

- Is this correct according to the ISO/ANSI C standards?
No.
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?
§6.2.1p4:
"...If the declarator or type specifier that declares the identifier
appears inside a block or within the list of parameter declarations in
a function definition, the identifier has block scope, which
terminates at the end of the associated block."

§6.8.5p4-5
"An iteration statement causes a statement called the loop body to be
executed repeatedly
until the controlling expression compares equal to 0.

An iteration statement is a block whose scope is a strict subset of
the scope of its
enclosing block. The loop body is also a block whose scope is a strict
subset of the scope
of the iteration statement."

The loop body is a block, the variable r is declared in this block.
The scope of the variable extends to the end of the associated block
of which the controlling expression is not a part.

Robert Gamble

Aug 21 '07 #3
Erik de Castro Lopo wrote:
Hi all,

Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.
The scope of the `r' inside the { } ends at the closing }.
The `r' in the while clause is some other `r' from an enclosing
scope.
Questions:

- Is this correct according to the ISO/ANSI C standards?
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?
6.8.5p5: "An iteration statement is a block whose scope is
a strict subset of the scope of its enclosing block. The loop
body is also a block whose scope is a strict subset of the scope
of the iteration statement."

See also 6.2.1p4 for the definition of "block scope."

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 21 '07 #4
Erik de Castro Lopo wrote:
>
Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.

Questions:

- Is this correct according to the ISO/ANSI C standards?
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?
Consider that rand may never return the value 0, in which case the
loop will run forever. The only possible use of this is to advance
the rand generator to the point where it has just returned zero.
The value(s) returned are not available anywhere. And that is IF
the scope of r extends through the conditional, which may not be so
for another compiler. Whether or not it should is another matter
entirely.

Simpler code with the same faults and no scope problem is:

do {
/* nada */
} while (rand());

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 21 '07 #5
CBFalconer wrote:
>
Erik de Castro Lopo wrote:

Consider the following code snippet:

do
{
int r = rand () ;
} while (r != 0) ;

It seems the compiler I'm using (GCC) does realise that the
scope of variable r extends the end of the while's conditional.

Questions:

- Is this correct according to the ISO/ANSI C standards?
- If so, can someone point me to the section of the ISO
C99 stanard whic defines this?
My C90 compiler doesn't allow this. As others have pointed out,
"r" is not in scope outside of the loop, and should not exist for
the "while" statement.
Consider that rand may never return the value 0, in which case the
loop will run forever. The only possible use of this is to advance
the rand generator to the point where it has just returned zero.
I took this as simply a sample snippet to demonstrate the "should
'r' be in scope for the 'while' statement" question, not as a
real-life code snippet. We often post "useless" snippets of code
here, simply to boil it down to the simplest wasy of demonstrating
what we are asking.

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Aug 21 '07 #6
On Tue, 21 Aug 2007 15:04:57 -0400, Kenneth Brody wrote:
I took this as simply a sample snippet to demonstrate the "should
'r' be in scope for the 'while' statement" question, not as a
real-life code snippet. We often post "useless" snippets of code
here, simply to boil it down to the simplest wasy of demonstrating
what we are asking.
<ot>
In the comp.lang.c++ FAQ there is a snippet of code which, to show
a situation in which different functions could be called without
being able to know which one at compile time, used to use
srand(time(0)); switch(rand() % 3). But recently it was changed to
switch ((rand() >8) % 3) because "the ">8" (typically)
improves the period of the lowest 2 bits".
(And of course n % 3 depends on *all* of the bits of n, and the
shift does nothing but worsen the bias due to RAND_MAX + 1 not
being a multiple of 3.)
</ot>
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 22 '07 #7
Army1987 wrote:
>
On Tue, 21 Aug 2007 15:04:57 -0400, Kenneth Brody wrote:
I took this as simply a sample snippet to demonstrate the "should
'r' be in scope for the 'while' statement" question, not as a
real-life code snippet. We often post "useless" snippets of code
here, simply to boil it down to the simplest wasy of demonstrating
what we are asking.

<ot>
In the comp.lang.c++ FAQ there is a snippet of code which, to show
a situation in which different functions could be called without
being able to know which one at compile time, used to use
srand(time(0)); switch(rand() % 3). But recently it was changed to
switch ((rand() >8) % 3) because "the ">8" (typically)
improves the period of the lowest 2 bits".
(And of course n % 3 depends on *all* of the bits of n, and the
shift does nothing but worsen the bias due to RAND_MAX + 1 not
being a multiple of 3.)
</ot>
They were probably thinking of (n % 4) and got confused.

--
pete
Aug 22 '07 #8
pete wrote:
>
Army1987 wrote:
[...]
<ot>
In the comp.lang.c++ FAQ there is a snippet of code which, to show
a situation in which different functions could be called without
being able to know which one at compile time, used to use
srand(time(0)); switch(rand() % 3). But recently it was changed to
switch ((rand() >8) % 3) because "the ">8" (typically)
improves the period of the lowest 2 bits".
(And of course n % 3 depends on *all* of the bits of n, and the
shift does nothing but worsen the bias due to RAND_MAX + 1 not
being a multiple of 3.)
</ot>

They were probably thinking of (n % 4) and got confused.
Or (n & 3).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Aug 22 '07 #9

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

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
0
by: Ori Berger | last post by:
I *think* I saw a post some time ago enabling "spreadsheet" like computations, that allows something along the lines of: >>> vars.a = 10 >>> vars.b = dependency("vars.a * 20") >>> print vars.b...
0
by: Uwe Mayer | last post by:
Hi, I've got a class that receives a function in the constructor and uses the __call__ method to execute the aforementioned function when the instance object is called: class foo(object):...
39
by: vineoff | last post by:
If I'm having nested loops like: for (...) for (..) for (...) { /* exit here */ } and I need to exit from there ^ . Is it better to use exceptions or goto or some other method?
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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: 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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.