PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Identifying variables: what process do you follow?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Identifying variables: what process do you follow?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 21h24   #1
Justin Spahr-Summers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

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.

  Réponse avec citation
Vieux 17/10/2007, 06h30   #2
jaysome
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.)


  Réponse avec citation
Vieux 17/10/2007, 06h42   #3
Justin Spahr-Summers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

On Oct 17, 12:30 am, jaysome <jays...@hotmail.com> wrote:
> On Tue, 16 Oct 2007 20:24:26 -0000, Justin Spahr-Summers
> <Justin.SpahrSumm...@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?


I was thinking in a hurry after a long day. It obviously begins where
the declaration is. I'm fairly certain you realize that, but the OP
might not, so thanks for bringing it up.

  Réponse avec citation
Vieux 17/10/2007, 10h33   #4
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

jaysome wrote:
> Justin Spahr-Summers <Justin.SpahrSummers@gmail.com> wrote:
>

.... snip ...
>
>> 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;


It _can_ begin at the brace. It does begin when b is declared.

--
Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)



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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h32.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11173 seconds with 12 queries