Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Position of variable declaration is causing "undeclared identifier" error.

Reply
Thread Tools

Position of variable declaration is causing "undeclared identifier" error.

 
 
craigbeanhead
Guest
Posts: n/a
 
      07-24-2003
Hi,

I'm teaching myself C from K&R2. I've come across something that I
really don't understand. When I try to compile the following code (in
VC++7), I get an "undeclared identifier" error. When I move the second
integer declaration to the beginning of the function, it compiles and
runs correctly. I'm sure I read that you could declare a variable
anywhere in a code block, as long as you don't attempt to use it
*before* the declaration. Can anyone explain this to me?

#include <stdio.h>

int main(void)
{
int a;
/* Works correctly if you move the "int b;" to here */
a = 123;
printf("%d\n", a);

int b;
b = 456;
printf("%d\n", b);

return 0;
}

-- Craig
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-24-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (craigbeanhead) writes:

> I'm teaching myself C from K&R2. I've come across something that I
> really don't understand. When I try to compile the following code (in
> VC++7), I get an "undeclared identifier" error. When I move the second
> integer declaration to the beginning of the function, it compiles and
> runs correctly. I'm sure I read that you could declare a variable
> anywhere in a code block, as long as you don't attempt to use it
> *before* the declaration. Can anyone explain this to me?


This is true in C99, but not in any earlier version. You
probably don't have a C99 compiler. (C++ also supports
declarations mid-block.)
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x1f6},*p=
b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
 
Kevin Easton
Guest
Posts: n/a
 
      07-25-2003
craigbeanhead <(E-Mail Removed)> wrote:
> Hi,
>
> I'm teaching myself C from K&R2. I've come across something that I
> really don't understand. When I try to compile the following code (in
> VC++7), I get an "undeclared identifier" error. When I move the second
> integer declaration to the beginning of the function, it compiles and
> runs correctly. I'm sure I read that you could declare a variable
> anywhere in a code block, as long as you don't attempt to use it
> *before* the declaration. Can anyone explain this to me?


As others have said, you can't do this in the widely-implemented C
standard. However, you can open a block statement anywhere a normal
statement can go:

#include <stdio.h>

int main(void)
{
int a;
a = 123;
printf("%d\n", a);

{ int b;
b = 456;
printf("%d\n", b);

return 0;
} }

- Kevin.

 
Reply With Quote
 
craigbeanhead
Guest
Posts: n/a
 
      07-25-2003
"E. Robert Tisdale" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> craigbeanhead wrote:


> > I'm teaching myself C from K&R2.


> Sigh.


Sigh? Is it a bad idea to learn from K&R2, then? Or is there another
reason for your sighing? I do realise that the book is *very* old, in
computer terms. I also realise that if I'm aiming for up-to-date
standards compliance with my code, that I'll need to supplement the
book with fresher information. (Thinking of buying the ISO standard,
actually). I think th previous C standard is a good place to aim,
actually - If the newest compilers don't support the newest features
yet?

> > When I try to compile the following code (in VC++7),
> > I get an "undeclared identifier" error.


> That's true for C++ and the new C 99 standard
> but *not* for the old C 89 standard.
> Check your compiler options.


I see. This explains my problem. Haven't checked my compiler options,
I'll stick to the traditional method.

> Until more compilers comply with the new C 99 standard,
> it might be better to write:
>
> #include <stdio.h>
>
> int main(int argc, char* argv[]) {
> int a = 123;
> printf("%d\n", a);
> { int b = 456;
> printf("%d\n", b);
> }
> return 0;
> }


Not necessary - I had the beginnings of a space invaders clone that I
wrote a while ago (in pseudo C++, C really), and I wanted to make it
as "C" as possible. Now I know that I'm not allowed to make variable
declarations in the middle of a code block, I'll remember not to do it
again.

Thanks

-- Craig
 
Reply With Quote
 
Bertrand Mollinier Toublet
Guest
Posts: n/a
 
      07-25-2003
jacob navia wrote:
>
> If you compile that with lcc-win32 the output is
> 123
> 456
>
> http://www.cs.virginia.edu/~lcc-win32
>

I am pretty disappointed to see that instead of being technically
helpful on this newsgroup (and, as the developer of a compiler, you
would be expected to know enough to be very helpful), you repeatedly
advertise your own product in the lamest and most unhelpful way.

I shall not want to read from you again.
--
Bertrand Mollinier Toublet
"No sea vivo, Buendia" -- El presidente del tribunal,
in Cien anos de soledad, de Gabriel Garcia Marquez

 
Reply With Quote
 
Mark Gordon
Guest
Posts: n/a
 
      07-25-2003
On 25 Jul 2003 00:03:48 -0700
(E-Mail Removed) (craigbeanhead) wrote:

> "E. Robert Tisdale" <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>...
> > craigbeanhead wrote:

>
> > > I'm teaching myself C from K&R2.

>
> > Sigh.

>
> Sigh? Is it a bad idea to learn from K&R2, then? Or is there another
> reason for your sighing? I do realise that the book is *very* old, in
> computer terms.


It may be old but it is still the book to use IMHO.

> I also realise that if I'm aiming for up-to-date
> standards compliance with my code, that I'll need to supplement the
> book with fresher information. (Thinking of buying the ISO standard,
> actually). I think th previous C standard is a good place to aim,
> actually - If the newest compilers don't support the newest features
> yet?


Correct. Most compilers do not support C99 so C90 is still better for
portability.

> > > When I try to compile the following code (in VC++7),
> > > I get an "undeclared identifier" error.

>
> > That's true for C++ and the new C 99 standard
> > but *not* for the old C 89 standard.
> > Check your compiler options.

>
> I see. This explains my problem. Haven't checked my compiler options,
> I'll stick to the traditional method.


It is worth turning up the warnings. It allows the compiler to tell you
about *some* things that won't work as expected.

> > Until more compilers comply with the new C 99 standard,
> > it might be better to write:
> >
> > #include <stdio.h>
> >
> > int main(int argc, char* argv[]) {
> > int a = 123;
> > printf("%d\n", a);
> > { int b = 456;
> > printf("%d\n", b);
> > }
> > return 0;
> > }


Introducing a new block just to add a variable is IMHO pointless.

> Not necessary - I had the beginnings of a space invaders clone that I
> wrote a while ago (in pseudo C++, C really), and I wanted to make it
> as "C" as possible. Now I know that I'm not allowed to make variable
> declarations in the middle of a code block, I'll remember not to do it
> again.


Since you are prepared to change your habits so that you produce
conforming code you should find most people here to be helpful.

Many of the people that know the C standards far better think that ERT
should be ignored. From what I've seen I agree.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-27-2003
On Fri, 25 Jul 2003 08:48:58 -0700, Bertrand Mollinier Toublet
<(E-Mail Removed)> wrote in comp.lang.c:

> jacob navia wrote:
> >
> > If you compile that with lcc-win32 the output is
> > 123
> > 456
> >
> > http://www.cs.virginia.edu/~lcc-win32
> >

> I am pretty disappointed to see that instead of being technically
> helpful on this newsgroup (and, as the developer of a compiler, you
> would be expected to know enough to be very helpful), you repeatedly
> advertise your own product in the lamest and most unhelpful way.
>
> I shall not want to read from you again.


I would dispute the word "advertise" here, since lcc-win32 can be
downloaded for free. And that is really free, no registration or
personal information required, just point a browser at that page and
click on the download links. So Jacob isn't selling anything at all.

There are other free compilers available for the Windows platform that
provide some effort at C99 conformance, but none is as small a
download or as easy to use as Jacob's lcc-win32.

Personally I use lcc-win32 frequently and recommend it to anyone
looking for a free compiler to begin learning with. Especially anyone
still limited to dial-up Internet access, since it is a vastly smaller
download than other alternatives.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
variable definiton / variable declaration baumann@pan C Programming 3 05-11-2005 01:01 PM
`our' declaration causing problems with `strict' pragma across files Dave Bakhash Perl Misc 14 07-23-2004 07:29 PM
XML-Stylesheet declaration in DTD causing issues Sarah Haskins XML 3 01-15-2004 09:37 AM
Position of variable declaration is causing "undeclared identifier" error. craigbeanhead C Programming 1 07-24-2003 10:19 PM



Advertisments