Declaring variables in int main()?

This is a discussion on Declaring variables in int main()? within the C++ Programming forums, part of the General Programming Boards category; Is it good practice to declares variables (and/or functions) within the int main() function? Don't they have to be initialized ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Declaring variables in int main()?

    Is it good practice to declares variables (and/or functions) within the int main() function? Don't they have to be initialized before they're called, and the initializing takes places outside (and before) the int main() function?
    I'm asking because I've seen a few tutorials on this site, that have the code declaring the variables within the int main() function.

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    You should keep your initialising in a function (any function, not just main), or a class, or namespace, etc. Having it outside these means that it is global. It will work having it both inside and out, but it is best to keep it in something so there aren't unexpected name clashes.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'd be itnerested in where you have been learning the language, you've obviously learned some strange things --

    It is perfectly conventional to define/declare variables that are local to the a function

    Code:
    int main() 
    {
        int a; int b; // variables declared in main
    
        a = b = 0; // initialize them!
    }
    even if it is main. You can not define functions within the main function (however you can declare them)

    Code:
    int main()
    {
        // error C2601: 'sub' : local function definitions are illegal
        int sub()
        {
            return bar; 
        }
    
        // declares that a function 'foo' exists and shall be resolved by the linker    
        int foo();
    
    }
    You can also define and declare variables outside of main (in functions other than main or global variables)

    Code:
    #include <stdio.h>
    int a = 0, b = 0; // global variables
    
    void bar(); // regular function declaration
    
    int main()
    {
    	void foo(); // this is weird to declare function inside of main
    	foo();
    	bar();
    }
    
    void foo()
    {
    	a = b = 1;
    }
    
    void bar()
    {
    	printf("%d %d", a, b);
    }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Thanks. Any particular reason why you used C in the last example, instead of C++ (the language I'm writing my program in)?
    Anyways, I just have one more short question...
    What about arrays?
    Would you declare (and define) that inside the function its relevant for, too, or does it matter?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C++ (where variables can be declare almost anywhere, the rule is to declare the variable as close to it's first use as possible - if possible, initialize it at the same place too. In C (pre-C99 standard, that is) you can only declare variables between the starting brace and the first actual code, so you can't quite follow that pattern.

    But as a general rule, it's the same principles for both C and C++ - the variable should have as small scope as possible - that helps the compiler optimize the code better, and it is easier to see what he variable is if it is close to where it is used.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. My Game isn't working...
    By kimset in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2007, 01:42 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM

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