Unexpected result.

Bengt Richter bokr at oz.net
Thu Sep 23 23:41:03 CEST 2004


On 23 Sep 2004 21:09:26 GMT, bokr at oz.net (Bengt Richter) wrote:

>On Thu, 23 Sep 2004 14:18:01 -0500, "Larry Bates" <lbates at swamisoft.com> wrote:
>
>>Actually the result is exactly as expected.
>>Programming 101 teaches us not to reuse
>>loop variables in nested loops.
>>
>
>[14:07] C:\pywk\clp>type p101.cpp
>#include <cstdio>
>void main(){
>    char abc[]="abc";
>    for(int i=0;i<3;++i){
>        printf("%c ", abc[i]);
>        for(int i=0;i<3;++i) printf("%d ", i);
>    }
>}
>
>
>[14:07] C:\pywk\clp>cl p101.cpp
>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
>Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
>
>p101.cpp
>Microsoft (R) Incremental Linker Version 6.00.8168
>Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
>
>/out:p101.exe
>p101.obj
>
>[14:07] C:\pywk\clp>p101
>a 0 1 2 b 0 1 2 c 0 1 2
>
>;-)
>
>Regards,
>Bengt Richter

Oops, I gave too much credit to M$ ;-/
Reordering the above as below, it gives

[14:24] C:\pywk\clp>cl/nologo p101.cpp
p101.cpp

[14:38] C:\pywk\clp>p101
0 1 2   0 1 2   0 1 2

vs g++ from mingw using msys shell:

[14:35] /c/pywk/clp>cat -n p101.cpp
     1  #include <cstdio>
     2  int main(){
     3      char abc[]="abc";
     4      for(int i=0;i<3;++i){
     5          for(int i=0;i<3;++i) printf("%d ", i);
     6          printf("%c ", abc[i]);
     7      }
     8      return 0;
     9  }
    10  
[14:35] /c/pywk/clp>g++  p101.cpp -o p101a.exe
p101.cpp: In function `int main()':
p101.cpp:6: warning: name lookup of `i' changed
p101.cpp:4: warning:   matches this `i' under ISO standard rules
p101.cpp:5: warning:   matches this `i' under old rules
[14:35] /c/pywk/clp>p101a
0 1 2 a 0 1 2 b 0 1 2 c [14:36] /c/pywk/clp>
[14:36] /c/pywk/clp>

Regards,
Bengt Richter



More information about the Python-list mailing list