My sister needs to know

This is a discussion on My sister needs to know within the C++ Programming forums, part of the General Programming Boards category; Hi, no, she doesnt want a date! She is learning C, ( I do C++ ) anyway, the question she ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    My sister needs to know

    Hi, no, she doesnt want a date! She is learning C, ( I do C++ )

    anyway, the question she asks comes from a text book. The sample code it has I have placed below with the comment from the book included. She wants to know why in C++ you can have the control variable inside a for loop and in C it must be declared outside. i personally think it has somthing to do when they changed the standard in C++, but I could be wrong..

    Here is the code, what is the reason? read the comment in the code for a better understanding!

    Code:
    #include <stdio.h>
    
    int main()
    {
    int i;   /* in C, you MUST  declare the control variable outside the 
                  loop. This is not the case in C++, where control variables
                  can be initalized inside the loop */
    
    for ( i = 0; i <= 10; i++ )
    {
    printf("%d", i);
    }
    
    getchar();
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    In C, all variables must be declared at the top of the bloci in which it is to be used and a for loop does not start the top of the block -- braces '{' do that. In c++ variables can be declared anywhere but always before they are used. And an integer declared in the for loop has scope only within that for loop.

    C++ didn't change the standard. C and C++ each have their own ISO standards.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Perhaps she should join the board and ask her own questions on the C board.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Hi, no, she doesnt want a date! She is learning C, ( I do C++ )

    anyway, the question she asks comes from a text book. The sample code it has I have placed below with the comment from the book included. She wants to know why in C++ you can have the control variable inside a for loop and in C it must be declared outside. i personally think it has somthing to do when they changed the standard in C++, but I could be wrong..

    Here is the code, what is the reason? read the comment in the code for a better understanding!
    In C++, objects are sometimes constructed in the middle function. Their initialization occurs in the constructor. For example, you could have
    Code:
    void f()
    {
            int c = 4 + 4;
    
            for (int i = 0; i < 10; ++i)
                   c++;
    
           Person person(c);
    }
    The construction of the object person must occur after c is processed. If C++ didn't allow construction in the middle, then you'd have to write

    Code:
    void f()
    {
            int c = 4 + 4;
            Person person;
    
            for (int i = 0; i < 10; ++i)
                   c++;
    
           person.init(c);
    }
    This results in a useless constructor with some objects uninitialized. C++ tries to enforce the rule that every object is initialized and no method is called on the object until the object is initialized. This rule reduces errors because calling a method on an unitialized object is almost always an error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 100000 women
    By Snafuist in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 05-27-2009, 07:41 AM
  2. Management Information Systems and My Sister do not mix
    By Necrodeemer in forum Tech Board
    Replies: 0
    Last Post: 01-10-2003, 01:10 PM
  3. Caught red handed
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-27-2002, 08:05 PM
  4. Sister page
    By Gades_GD in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-17-2001, 03:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21