Variables in main()

This is a discussion on Variables in main() within the C++ Programming forums, part of the General Programming Boards category; Code: int main() int x = 5; int add(int a, int b) { return a+b+x; } I got an error, ...

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Variables in main()

    Code:
    int main()
    int x = 5;
    int add(int a, int b)
    {
        return a+b+x;
    }
    I got an error, saying that the "x" in the add function is not declared. It is legal in C, but not in C++. May i know why? Also, how could I used the "x" of main() in add()?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    err...Where is main() 's body?
    Do you really think this code is legal in C?

    Also, how could I used the "x" of main() in add()?
    Pass it by reference.
    Last edited by manasij7479; 07-31-2011 at 03:20 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    1337
    Join Date
    Jul 2008
    Posts
    135
    Sorry, i was typing an example without realising that i didnt put the braces for the main().

    Code:
    int main()
    {
        int x = 5;
        int add(int a, int b)
        {
            return a+b+x;
        }
    }
    By the way, can i have a simple example based on this example

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    C is not pascal. One consequence is that functions cannot be implemented inside the bodies of other functions.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    1337
    Join Date
    Jul 2008
    Posts
    135
    Why in C, there is no problem calling a variable from main() in add(), where add() is inside main()? Why is it illegal is C++?

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    The closest you can come to that is..
    Code:
    #include<iostream>
    int main()
    {
        int x = 5;
        int add(int,int,int&);
        std::cout<<add(2,4,x)<<std::endl;
        std::cout<<x; //To show that x really changes
        
        
    }
    int add(int a, int b, int& x)
    {
        x-=4;
        return a+b+x;
    }
    Last edited by manasij7479; 07-31-2011 at 04:44 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by valthyx View Post
    Why in C, there is no problem calling a variable from main() in add(), where add() is inside main()? Why is it illegal is C++?
    This is not legal C, either. A function cannot be encapsulated in another function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    Quote Originally Posted by valthyx View Post
    Why in C, there is no problem calling a variable from main() in add(), where add() is inside main()? Why is it illegal is C++?
    Your code is invalid because add() is implemented in the body of main(). That is illegal in both C and C++.

    If you have something that is apparently working when compiled as C, and not when compiled as C++, then it is different from the code you have posted here.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    If this code is legal for you, then your C compiler is brain-dead. Put it out of our misery and get a new one.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thanks for the reply, I got it.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    GCC allows nested functions.
    Using and Porting the GNU Compiler Collection (GCC): C Extensions

    Why it does is another matter entirely.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Declaring variables in int main()?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 02:21 AM
  3. Replies: 4
    Last Post: 03-16-2009, 08:44 AM
  4. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 06:30 PM
  5. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM

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