在哪里可以在C99中合法声明变量? [英] Where can I legally declare a variable in C99?

查看:136
本文介绍了在哪里可以在C99中合法声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我初次接触C时,被告知始终在函数顶部声明变量。现在,我已经对语言有了很好的了解,现在我将精力集中在编码风格上,尤其是限制变量的范围。我已经读到了限制范围的好处,并且遇到了一个有趣的例子。显然,C99允许您执行此操作...

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");
}

我曾经认为变量范围受最内层环绕大括号 {} ,但在上面的示例中, int i 的范围似乎受到限制

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.

我尝试通过 fgets()扩展上面的示例。 做我认为类似的事情,但是两者都给了我一个语法错误。

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); *请参见注释**

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

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

那么,在C99中声明变量到底在哪里合法? for循环示例是否是规则的例外?这是否适用于 while do while 循环?

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?

*注意**:即使我可以在此处声明char数组,我也不确定这在语法上是否正确,因为 fgets()正在寻找 char的指针,而不是 char的数组80的指针。这就是为什么我尝试了 malloc()版本的原因。

*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.

推荐答案

在C99,您可以在需要它们的地方声明变量,就像C ++允许您这样做一样。

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...
    }
}



  • 您可以在 for循环的控制部分中声明一个变量:

    • You can declare a variable in the control part of a 'for' loop:

        for (int x = 0; x < 10; x++)    /* New in C99 */
      



    • 您不能在'while'循环或'if'语句的控件部分中声明变量。

    • 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.

      C99标准说:


      6.8.5.3 for语句

      6.8.5.3 The for statement

      语句


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



      的行为如下:表达式expression-2是
      之前求值的控制表达式循环体的每次执行。表达式expression-3是
      ,在每次循环体执行后被评估为void表达式。如果子句1是
      声明,则它声明的任何变量的范围是声明的其余部分,而
      是整个循环(包括其他两个表达式)的范围;在控制表达式的第一次求值之前,它以执行顺序
      到达。如果子句1是一个表达式,则在对控制表达式进行第一次评估之前,将
      评估为void表达式。

      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.

      这篇关于在哪里可以在C99中合法声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆