473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

what's the scope of i ?

int main() {
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
}

when compiling it in Visual C++, i got an error.
but using g++, it is no problem.

Could anybody what's wrong?
Jul 22 '05 #1
8 1667
* Zeng Dinghao:
int main() {
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
}

when compiling it in Visual C++, i got an error.
but using g++, it is no problem.
That's because MSVC has (pre-standard) backwards compatibility
as default: you need to turn a bunch of compiler options to make
that compiler standard-conforming in various respects.

Could anybody what's wrong?


1) No '#include <iostream>'.
2) No 'using namespace std;'.
3) Stylewise, using 'i++' instead of '++i'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Zeng Dinghao wrote:
int main() {
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
}

when compiling it in Visual C++, i got an error.
but using g++, it is no problem.

Could anybody what's wrong?

Yes, the compiler is very old. Upgrade to VC++ 2003.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1098002279.86140@athnrd02...
Zeng Dinghao wrote:
int main() {
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
for (int i=0; i<10; i++) {
cout << "dfdfd" << endl;
}
}

when compiling it in Visual C++, i got an error.
but using g++, it is no problem.

Could anybody what's wrong?

Yes, the compiler is very old. Upgrade to VC++ 2003.


Which is available free (the compiler anyway, not the GUI), at
http://msdn.microsoft.com/visualc/vctoolkit2003/

Anyway, you'd still need to zet either /Za or /Zc:forScope to get
standards-compliant behaviour.

--
Unforgiven
Jul 22 '05 #4
Unforgiven wrote:
Which is available free (the compiler anyway, not the GUI), at
http://msdn.microsoft.com/visualc/vctoolkit2003/

Anyway, you'd still need to zet either /Za or /Zc:forScope to get
standards-compliant behaviour.

int main()
{
for(int i=0; i<10; ++i)
;

for(int i=0; i<10; ++i)
;
}
C:\c>cl temp.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1098008738.444962@athnrd02...
Unforgiven wrote:
Which is available free (the compiler anyway, not the GUI), at
http://msdn.microsoft.com/visualc/vctoolkit2003/

Anyway, you'd still need to zet either /Za or /Zc:forScope to get
standards-compliant behaviour.


int main()
{
for(int i=0; i<10; ++i)
;

i = 4; // Should cause error, i not in scope

for(int i=0; i<10; ++i)
;
}

G:\My Documents>cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

G:\My Documents>cl test.cpp /Zc:forScope
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
test.cpp(6) : error C2065: 'i' : undeclared identifier

G:\My Documents>cl test.cpp /Za
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
test.cpp(6) : error C2065: 'i' : undeclared identifier

--
Unforgiven
Jul 22 '05 #6
Unforgiven wrote:
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1098008738.444962@athnrd02...
Unforgiven wrote:

Which is available free (the compiler anyway, not the GUI), at
http://msdn.microsoft.com/visualc/vctoolkit2003/

Anyway, you'd still need to zet either /Za or /Zc:forScope to get
standards-compliant behaviour.

int main()
{
for(int i=0; i<10; ++i)
;

i = 4; // Should cause error, i not in scope

for(int i=0; i<10; ++i)
;
}

G:\My Documents>cl test.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj

G:\My Documents>cl test.cpp /Zc:forScope
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
test.cpp(6) : error C2065: 'i' : undeclared identifier

G:\My Documents>cl test.cpp /Za
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

test.cpp
test.cpp(6) : error C2065: 'i' : undeclared identifier


Yes, the key is this:
/Ze enable extensions (default)

/Za disable extensions (implies /Op)


C:\c>cl /Za temp.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

temp.cpp
temp.cpp(6) : error C2065: 'i' : undeclared identifier

C:\c>
I guess in the default mode, the compiler auto-detects the old style
code and accepts it. For example the following does not produce any errors:

int main()
{
for(int i=0; i<10; ++i)
;

int i = 4;

for(int i=0; i<10; ++i)
;
}
However in the OP compiler it does, as even his simpler code with two
loops refused to compile.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:1098012253.879248@athnrd02...
<snip>
However in the OP compiler it does, as even his simpler code with two
loops refused to compile.


I know all that, but my point was that you need /Za or /Zc:forScope to get
standards-compliant behaviour in for-loop scope. Even if VC7.1 compiles the
OP's code without those options, it's still not standards-compliant
behaviour because 'i' is still visible outside of the loops.

The OP is obviously using VC6 or older, because those are the only versions
of VC that fail for his code. Nevertheless, even though VC7+ don't fail on
that code, their behaviour is still not fully standards-compliant *unless*
you specify one of those options. That is what I was trying to say. I wasn't
trying to refute the fact that VC7+ would fix the OPs situation.

VC8 is still the same btw. IMO it should be the reverse though,
standards-compliant behaviour by default and non-standards compliant
behaviour with an option. Maybe I'll file a suggestion on that.

--
Unforgiven
Jul 22 '05 #8
Unforgiven wrote:
I know all that, but my point was that you need /Za or /Zc:forScope to get
standards-compliant behaviour in for-loop scope. Even if VC7.1 compiles the
OP's code without those options, it's still not standards-compliant
behaviour because 'i' is still visible outside of the loops.

The OP is obviously using VC6 or older, because those are the only versions
of VC that fail for his code. Nevertheless, even though VC7+ don't fail on
that code, their behaviour is still not fully standards-compliant *unless*
you specify one of those options. That is what I was trying to say. I wasn't
trying to refute the fact that VC7+ would fix the OPs situation.

VC8 is still the same btw. IMO it should be the reverse though,
standards-compliant behaviour by default and non-standards compliant
behaviour with an option. Maybe I'll file a suggestion on that.


Well if you use /Za then you get strict standard compliant behaviour.
GCC also is about the same, the default enables various extensions and
-std=c++98 -pedantic-errors provides strict standard compliant behaviour.

Essentially all compiles enable extensions in their default mode so as
system-dependent code to be able to compile. Like __fastcall etc
keywords in Win32, etc.
The important thing with an ISO C++ compliant is to *compile* ISO C++
code in default mode, and VC++ 2003, GCC, BC++ etc do that.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
1
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
6
by: Eric Moors | last post by:
If have a question with respect to the code below: ################################################################# #include <stdio.h> char *ret_ok(void) { static char str = "explicit...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
39
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
4
by: FlaPnthrsPunk | last post by:
Hi all, this is my first time ever using PHP, and I am still very new to programming in general. I'm doing an assignment for my Media E-Commerce class where the code for a survey is all provided. ...
4
MMcCarthy
by: MMcCarthy | last post by:
http://bytes.com/images/howtos/projectscope_blocks.jpgAs a freelance IT consultant for over 10 years, I’ve come to appreciate well defined project scopes. A project scope is a common understanding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.