For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > variable declaration problem in vc









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author variable declaration problem in vc
Mandeep

2006-05-26, 10:05 pm

hi,
I have both VC++ express and VC++6 installed on my system. I am unable
to compile this simple code:
#include <stdlib.h>

void main()
{
int x;
if (x==0)
if (x==1)
x=2;
int y;
y=3;



}


in above listed code i get syntax error when i build.
VC does not allow me to declare variable in procedure after a block
like if, so it says declaration of y is illeagal
C:\c code\ch_6_1\ch.c(9) : error C2143: syntax error : missing ';'
before 'type'
C:\c code\ch_6_1\ch.c(10) : error C2065: 'y' : undeclared identifier

I think the code is valid C code


I added SDL library directories in default project paths, could they
cause this problem?

Igor Tandetnik

2006-05-27, 4:16 am

"Mandeep" <mandeep.singh.bhatia@gmail.com> wrote in message
news:1148701128.563241.280980@j55g2000cwa.googlegroups.com
> I have both VC++ express and VC++6 installed on my system. I am unable
> to compile this simple code:
> #include <stdlib.h>
>
> void main()
> {
> int x;
> if (x==0)
> if (x==1)
> x=2;
> int y;
> y=3;
>
>
>
> }
>
>
> in above listed code i get syntax error when i build.
> VC does not allow me to declare variable in procedure after a block
> like if, so it says declaration of y is illeagal
> C:\c code\ch_6_1\ch.c(9) : error C2143: syntax error : missing ';'
> before 'type'
> C:\c code\ch_6_1\ch.c(10) : error C2065: 'y' : undeclared identifier


Your code is not valid C. In C, you must declare all variables at the
beginning of the block. Rename the source file to have .cpp extension.

> I think the code is valid C code


You think wrong. Besides incorrectly declared variables, main() must
return int. And your program exhibits undefined behavior by examining
variable x before it's been assigned. And of course the code doesn't
make any sense - e.g. x=2 will never be executed.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Mandeep

2006-05-27, 8:07 am

Thanks for letting me know. The code was not supposed to make sense but
just produce the error condition i was getting.
yes the main should be int in c, but the variable declaration after if
statement is valid in dev-c++, so i was why vc did not say the
same.
Thanks

Abdo Haji-Ali

2006-05-27, 7:05 pm

"Mandeep" <mandeep.singh.bhatia@gmail.com> wrote in message
news:1148738042.054450.98490@j55g2000cwa.googlegroups.com...
> Thanks for letting me know. The code was not supposed to make sense but
> just produce the error condition i was getting.
> yes the main should be int in c, but the variable declaration after if
> statement is valid in dev-c++, so i was why vc did not say the
> same.

Because obviously dev-c++ is compiling C++ code... VS does this too, try
changing the extension of your file to .cpp instead of .c and VS would
compile it as C++ code

--
Abdo Haji-Ali
Programmer
In|Framez


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com