Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:13:58+00:00 2026-05-27T13:13:58+00:00

I heard (probably from a teacher) that one should declare all variables on top

  • 0

I heard (probably from a teacher) that one should declare all variables on top of the program/function, and that declaring new ones among the statements could cause problems.

But then I was reading K&R and I came across this sentence: “Declarations of variables (including initializations) may follow the left brace that introduces any compound statement, not just the one that begins a function”. He follows with an example:

if (n > 0){
    int i;
    for (i=0;i<n;i++)
    ...
}

I played a bit with the concept, and it works even with arrays. For example:

int main(){
    int x = 0 ;

    while (x<10){
        if (x>5){
            int y[x];
            y[0] = 10;
            printf("%d %d\n",y[0],y[4]);
        }
        x++;
    }
}

So when exactly I am not allowed to declare variables? For example, what if my variable declaration is not right after the opening brace? Like here:

int main(){
    int x = 10;

    x++;
    printf("%d\n",x);

    int z = 6;
    printf("%d\n",z);
}

Could this cause trouble depending on the program/machine?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:13:59+00:00Added an answer on May 27, 2026 at 1:13 pm

    I also often hear that putting variables at the top of the function is the best way to do things, but I strongly disagree. I prefer to confine variables to the smallest scope possible so they have less chance to be misused and so I have less stuff filling up my mental space in each line on the program.

    While all versions of C allow lexical block scope, where you can declare the variables depends of the version of the C standard that you are targeting:

    C99 onwards or C++

    Modern C compilers such as gcc and clang support the C99 and C11 standards, which allow you to declare a variable anywhere a statement could go. The variable’s scope starts from the point of the declaration to the end of the block (next closing brace).

    if( x < 10 ){
       printf("%d", 17);  // z is not in scope in this line
       int z = 42;
       printf("%d", z);   // z is in scope in this line
    }
    

    You can also declare variables inside for loop initializers. The variable will only exist only inside the loop.

    for(int i=0; i<10; i++){
        printf("%d", i);
    }
    

    ANSI C (C90)

    If you are targeting the older ANSI C standard, then you are limited to declaring variables immediately after an opening brace1.

    This doesn’t mean you have to declare all your variables at the top of your functions though. In C you can put a brace-delimited block anywhere a statement could go (not just after things like if or for) and you can use this to introduce new variable scopes. The following is the ANSI C version of the previous C99 examples:

    if( x < 10 ){
       printf("%d", 17);  // z is not in scope in this line
    
       {
           int z = 42;
           printf("%d", z);   // z is in scope in this line
       }
    }
    
    {int i; for(i=0; i<10; i++){
        printf("%d", i);
    }}
    

    1 Note that if you are using gcc you need to pass the --pedantic flag to make it actually enforce the C90 standard and complain that the variables are declared in the wrong place. If you just use -std=c90 it makes gcc accept a superset of C90 which also allows the more flexible C99 variable declarations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I heard that on Windows you can login from a web browser to the
I have a program that requires fast performance. Within one of its inner loops,
That's probably part one of my question. Basically I'm struggling with the actual injection
I heard that if you use port 443 (the port usually used for https)
I heard on a recent podcast (Polymorphic) that it is possible to cache a
I heard that you could right-shift a number by .5 instead of using Math.floor().
I heard that decision tables in relational database have been researched a lot in
I heard somewhere that you can drop down to C++ directly inside C# code.
I heard that Visual Studio came with an Image Library, but I can't find
Since Greasemonkey can't read/write files from a local hard disk, I've heard people suggesting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.