Thread: extra braces inside main

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Question extra braces inside main

    I ran across this code and was wondering what the extra braces are there for?

    Code:
    int main()
     {
      const int sz = 100; // Buffer size;
      char buf[sz];
      
    
    
    {
    
    
        ifstream in("Strfile.cpp"); // Read
        assure(in, "Strfile.cpp"); // Verify open
        ofstream out("Strfile.out"); // Write
        assure(out, "Strfile.out");
        int i = 1; // Line counter
    
        // A less-convenient approach for line input:
        while(in.get(buf, sz)) 
         { 
          in.get(); // Throw away next character (\n)
          cout << buf << endl; // Must add \n
          out << i++ << ": " << buf << endl;
        }
     
    
    
     } // Destructors close in & out
    
      
    } ///:~

    It seems very odd to me but compiled fine when i removed the assure function calls. I also removed the ofstream stuff, but i think it would compile with that in there also
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They have no purpose in the posted code, other than to potentially prevent all variables declared in there from being used in the code after the last red }.

    They just define another scope block. As a result, all variables declared inside that block, exist only until that block ends, then they're gone.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 01-01-2008, 02:34 AM
  2. Main Declaration error
    By starkhorn in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 09:04 AM
  3. Defining main function!
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 02:00 PM
  4. why int main instead of void main?
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-26-2001, 10:49 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM