Thread: Variables in main()

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 07: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