TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: MATHIEU BOUCHARD
from: DARIN MCBRIDE
date: 1997-09-07 20:54:00
subject: Why the braces?

 NH>>> switch (foo) {
 NH>>> case 1 :
 NH>>> int bar = 2;
 NH>>> break;
 NH>>> case 2 :
 NH>>> int bar = 3;
 NH>>> break;
 NH>>> }
Well, close.  Let's try this:
z.cc:
==========
int main(int argc, char**)
{
    
    switch(argc)
    {
    case 1:
        int foo = 1;
        break;
    case 2:
        int foo = 2;
        break;
    }
    return argc;
}
==========
[0] g:\src\z>gcc z.cc -c
z.cc: In function `int main(int, char **)':
z.cc:9: jump to case label
z.cc:7:   crosses initialization of `int foo'
z.cc:10: redeclaration of `int foo'
z.cc:7: `int foo' previously declared here
z.cc:10: warning: unused variable `int foo'
==========
Jump to case label (case 2) crosses initialization of `int foo' (inside case 
1).  That's error #1, which your compiler didn't catch.  The one you caught 
was the next error - the redeclaration of `int foo'.  The warning we can all 
understand.  :-)
 MB> Using TC++ 3.00,
 MB> Turbo C++ Version 3.00 Copyright (c) 1992 Borland International
 MB> z.cpp:
 MB> Error z.cpp 7: Multiple declaration for 'patate' in function main(int)
 MB> Warning z.cpp 9: 'patate' is assigned a value that is never used in
 MB> function mai
 MB> n(int)
 MB> *** 1 errors in Compile ***
 MB> that's all.
 MB> (the warning is irrelevant to our problem here)
 MB> only 1 error. i'll try it in GCC in a few minutes..
Your results with GCC?
Here:
  [0] g:\src\z>gcc -v
  gcc version 2.7.2.1
[EMX 0.9c, fixpak 3]
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.