Thread: strange declaration error

  1. #1
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Well, it's strange, in C++ you can declare that type of variables almost any where in the code...

    I have one question:
    If we have a block inside the function can we declare a variable inside that block...

    int main()
    {
    {
    int x;
    }
    return 0;
    }

    Is that OK!
    none...

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    strange declaration error

    hi
    something very strange is happening, or im simply blind.
    Code:
    #include<stdio.h>
    
    int main ()
    {
    	printf("whats wrong?");
    	int i;	
    	return 0;
    }
    my compiler doesnt compile it but says that the "Declaration is not allowed here in function main"

    im using Borland C++ 5.5.1 for win32

    whats wrong with this small code? why cant i declare variables in
    main?
    whaaa

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    In C, declarations have to be at the top of functions.

    I think C99 allows declarations anywhere -- as long as you declare before you use, but I'm not sure; I don't usually program in C. If that's the case, then if you get an updated C compiler you might be able to what you were trying to do.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    You're declaring a variable

    after you started the program.
    You can declare a variable outside a function (global variable) or at the start of the function, look:

    Code:
    #include <stdio.h>
    
    /* global var */
    int iGlobal; 
    
    int main(void)
    {
        /* local var */
        int iLocal;
       
        return 0;
        
    }

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    darn!
    thanks i was going blind because of python.

  6. #6
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I dont think this was mentioned, if so I didnt read everything right. If you have a function, all the variables must be declared at the top of that function

    Code:
    void print()
    {
         int i;
         printf("something");
    }
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>all the variables must be declared at the top of that function
    No, as said by Salem, local variables must be declared at the top of the code block, not the function.

    So, a very simplistic example would be this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i;
        
        for (i = 0; i < 10; i++)
        {
            int j;
            j = i * 10;
            printf ("%d * 10 is %d\n", i, j);
        }
    
        return 0;
    }
    Here, j is declared within the for loop, and therefore it's scope is limited to that code block.

    Is this good programming style? Well, that's another conversation that I'm sure someone will argue to death in a 50 post long thread, but imho, if it suits your needs, use it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    #include<stdio.h>

    int main ()
    {
    printf("whats wrong?");
    int i;
    return 0;
    }
    This compiles fine with me, using GCC 3.1. According to C99, the latest C standard, this is valid C. C99, like C++, does not require that all declarations appear at the start of a block.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. My first game
    By homeyg in forum Game Programming
    Replies: 20
    Last Post: 12-22-2004, 05:25 PM