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 625983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:22:09+00:00 2026-05-13T19:22:09+00:00

When I was first introduced to C I was told to always declare my

  • 0

When I was first introduced to C I was told to always declare my variables at the top of the function. Now that I have a strong grasp of the language I am focusing my efforts on coding style, particularly limiting the scope of my variables. I have read about the benefits to limiting the scope and I came across an interesting example. Apparently, C99 allows you to do this…

for (int i = 0; i < 10; i++)
{
   puts("hello");
}

I had thought that a variables scope was limited by the inner-most surrounding curly braces { }, but in the above example int i appears to be limited in scope by the curly braces of the for-loop even though it is declared outside of them.

I tried to extend the above example with fgets() to do what I thought was something similar but both of these gave me a syntax error.

fgets(char fpath[80], 80, stdin); *See Note**

fgets(char* fpath = malloc(80), 80, stdin);

So, just where exactly is it legal to declare variables in C99? Was the for-loop example an exception to the rule? Does this apply to while and do while loops as well?

*Note**: I’m not even sure this would be syntactically correct even if I could declare the char array there since fgets() is looking for pointer to char not pointer to array 80 of char. This is why I tried the malloc() version.

  • 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-13T19:22:10+00:00Added an answer on May 13, 2026 at 7:22 pm

    In C99, you can declare your variables where you need them, just like C++ allows you to do that.

    void somefunc(char *arg)
    {
        char *ptr = "xyz";
        if (strcmp(arg, ptr) == 0)
        {
            int abc = 0;    /* Always could declare variables at a block start */
    
            somefunc(arg, &ptr, &abc);
    
            int def = another_func(abc, arg);   /* New in C99 */
            ...other code using def, presumably...
        }
    }
    
    • You can declare a variable in the control part of a ‘for’ loop:

        for (int x = 0; x < 10; x++)    /* New in C99 */
      
    • You cannot declare a variable in the control part of a ‘while’ loop or an ‘if’ statement.

    • You cannot declare a variable in a function call.

    • Obviously, you can (and always could) declare variables in the block after any loop or an ‘if’ statement.

    The C99 standard says:

    6.8.5.3 The for statement

    The statement

    for ( clause-1 ; expression-2 ; expression-3 ) statement
    

    behaves as follows: The expression expression-2 is the controlling expression that is
    evaluated before each execution of the loop body. The expression expression-3 is
    evaluated as a void expression after each execution of the loop body. If clause-1 is a
    declaration, the scope of any variables it declares is the remainder of the declaration and
    the entire loop, including the other two expressions; it is reached in the order of execution
    before the first evaluation of the controlling expression. If clause-1 is an expression, it is
    evaluated as a void expression before the first evaluation of the controlling expression.

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

Sidebar

Related Questions

In the language I was first introduced to, there was a function repeat() ,
I have a couple of design/architectural questions that always come up in our shop.
When I first became interested in programming, I took a class that introduced me
I have a user class in EF Code First that contains a lot of
When was the strip_tags() function first introduced to PHP? Which version?
I am to decide wether to use a comfortable function that was introduced with
I have been using WinForms since the first framework introduced and invested a lot
In which SQL standard was RANK() first introduced? List of SQL standards: SQL-86 SQL-89
First post, but I've been lurking around this site for a long while now,
First of all, i would like to explain my condition right now. I'm using

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.