Re: Identifying variables: what process do you follow?
On Tue, 16 Oct 2007 20:24:26 -0000, Justin Spahr-Summers
<Justin.SpahrSummers@gmail.com> wrote:
>On Oct 16, 9:23 am, Kevin Walzer <k...@codebykevin.com> wrote:
>> With C, such an approach doesn't seem practical. You need to identify
>> some variables ahead of time just to have a program that will compile!
>
>C99 (which is the latest standard) allows mixed declarations and code,
>but is not widely implemented in full; however, in C90, you can also
>do something like the following:
>
>int myfunction (void) {
> int a = 5;
> /* do stuff with 'a' here */
> {
> int b = 6;
> /* do stuff with 'a' and 'b' here */
> }
>
> /* do more stuff with 'a' here */
> return a;
>}
>
>The scope of the variable 'b' begins at the inner opening brace and
>ends at the inner closing brace, and as long as you keep its
>declaration at the very beginning of that scope, it's legal in both
>C90 and C99. Any amount of nested braces is allowed as well.
If the scope of the variable 'b' begins at the inner opening brace,
shouldn't I be allowed to do something like this?
int myfunction (void) {
int a = 5;
/* do stuff with 'a' here */
{
int b0 = b;
int b = 6;
/* do stuff with 'a' and 'b' here */
}
/* do more stuff with 'a' here */
return a;
}
When I do this, I get a compiler error:
error C2065: 'b' : undeclared identifier
What gives?
Thanks in advance
--
jaysome
"No product of human intellect comes out right the first time. We
rewrite sentences, rip out knitting stitches, replant gardens, remodel
houses, and repair bridges. Why should software be any different?"
(RTCA/DO-248A, Second Annual Report for Clarification of DO-178B
"Software Considerations in Airborne Systems and Equipment
Certification". RTCA, Inc., Washington, D. C., September 13, 2000.)
|