Thread: Declaring variables after statements.

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Declaring variables after statements.

    I have seen this done in some code samples and seen Salem saying that its not allowed in standard C. What I was wondering is when is memory allocated and what the lifespan of the variable is when it is declared after statements.

    example:
    Code:
    int foo (void)
    {
    int blah, blah2;
    blah = something;
    switch (blah)
              {
              case 1:
                      blah2+=1;
                      break;
             case 2:
                     int blah3;
                     blah3=blah * blah;
                     break;
             case 3:
                     blah2=ok;
                     break;
            }
    return 0;
    }
    Could blah3 be used in case 3? case 1?

    I personally dont like it but I'd like to know how it works none the less.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    In C you can't define variables after any statements. You can only do that in C++. You will get a compiler error in compiling that code.
    Depending on the version of the C++ compiler, earlier versions of compilers will have the scope of blah3 to be until the end of the function. Later versions, i think would have the scope end at the end of the switch statement.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Declaring variables after statements.

    In other words, your code is illegal because blah3 is not defined at a block beginning. This will work, though:

    Code:
    int foo (void)
    {
    int blah, blah2;
    blah = something;
    switch (blah)
              {
              case 1:
                      blah2+=1;
                      break;
             case 2:
                  {
                     int blah3;
                     blah3=blah * blah;
                     break;
                  }
             case 3:
                     blah2=ok;
                     break;
            }
    return 0;
    }
    In this case, blah3 is only available within the block defined.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Sorry to necro this post, but another thread brought this topic to my attention...

    I was under the impression that you cannot declare variables after any statements in a function. However this code compiles and executes...
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int input;
    	int x,y,z,zZ;
    
    	scanf("%d", &input);
    
    	x=y=input;
    	
    	int table[x][y];
    	int testVar = 0;
    
    	for (z=0; z < input ; z++)
    	{
    		for (zZ = 0; zZ < input ; zZ++ )
    		{
    			table[z][zZ] = zZ;
    		}
    	}
    	for (z=0; z < input ; z++)
    	{
    		for (zZ = 0; zZ < input ; zZ++ )
    		{
    			printf("%d",table[z][zZ] = zZ);
    		}
    		printf("\n");
    	}
    	printf ("%d\n",testVar);
    
    	return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Scribbler
    Sorry to necro this post, but another thread brought this topic to my attention...

    I was under the impression that you cannot declare variables after any statements in a function. However this code compiles and executes...
    (Holy OLD THREAD, Batman!)

    That's because you can declare variables whenever you feel like it...

    ...in C99. Linky.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM