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 6 6605 cr***********@hotmail.com (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 1:putchar(a[i&15]);break;}}}
craigbeanhead <cr***********@hotmail.com> 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.
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>... 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
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
On 25 Jul 2003 00:03:48 -0700 cr***********@hotmail.com (craigbeanhead) wrote: "E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>... 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.
On Fri, 25 Jul 2003 08:48:58 -0700, Bertrand Mollinier Toublet
<be***********************@enst-bretagne.fr> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jason Sauer |
last post by:
We use "FOR XML EXPLICIT" and "OPENXML" heavily on our SQL 2000 Advanced
server. Every now and then we get an "Undeclared Tag ID" error (SQL Error
#6806). If I open the stored procedure, modify it...
|
by: craigbeanhead |
last post by:
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...
|
by: ~~~ .NET Ed ~~~ |
last post by:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET
Framework 1.1. In this particular situation I have a custom user control in
a windows form. There is a member variable...
|
by: Stephanie Doherty |
last post by:
Hello World,
I am trying to use a _spawnl function like this (and I
have included the process.h file):
_spawnl(_P_WAIT,iporgfile,iporgfile,NULL);
It compiles with the following errors:
...
|
by: VB Programmer |
last post by:
I wanted to access some properties of a panel control that is on a different
form. So, I went to the (Declarations) section of my main form (the form
with the panel) and changed the panel (called...
|
by: Brandan |
last post by:
Hello,
I am getting the follow error: The variable 'strSQL' is
either undeclared or was never assigned. On the follwing
code:
Dim strSQL As String
Dim strWhatToReplace As String
strSQL =...
|
by: thistleryver |
last post by:
I thought it meant that I hadn't declared a variable but I have.
int DoubleNumbers(int seq, int *digitNum)
{
int i, j;
for (i = 0; i = 9; i++)
for (j = i + 1; j = 9; j++)
if (seq...
|
by: coste068 |
last post by:
I am using visual studio and can't seem to figure out why I keep getting this error when I compile. Any suggestions? Here is the relevant code:
#include <iostream>
int main()
{
..........
|
by: mknoll217 |
last post by:
I am recieving this error from my code:
The multi-part identifier "PAR.UniqueID" could not be bound.
The multi-part identifier "Salary.UniqueID" could not be bound.
The multi-part identifier...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |