English 中文(简体)
为什么我能在部族标签之后宣布变数?
原标题:Why can t I declare a variable after a label with clang?

我发现,有些法典编纂成克温堡,但不是部族:

#include<stdio.h>

int main(int argc, char **argv) {
    int test = 0;

    if (test == 0) {
        goto print_five;
    } else {
        return 0;
    }

 print_five:
    int five = 6;

    printf("value of five: %d
", five);
    return 0;
}

if I declare the variable five to the top of the function (under test), then the program compiles under both compilers.

After reading questions [1] and [2], I thought maybe it had to do with potentially skipping a variable declaration, similar to needing to create scope in case statements. The following examples compile in both compilers as well.

print_five:
    printf("value of five: ");
    int five = 6;
    printf("%d
", five);
    
    return 0;
print_five: ;
    int five = 6;     
    printf("value of five: %d
", five);
    return 0;

however it seems that this isn t the case, and has to do with having an expression directly after a label. But isn t variable declaration/initialization considered an expression? Is there something wrong with my first example, or does clang have a bug?

  1. Overkilling "crosses initialization of variable" error in C++?
  2. In C why do you need a statement after a goto label?
问题回答

在C99之前,所有可变声明必须位于范围组的顶端,因为声明不是声明:

int a;
int b;
int c;

puts("hi");
a = 5;
return 0;

自99世纪90年代以来,你可以宣布一项功能中的变量。 这意味着,过去甚至不可能写这样一件事,显然,在声明之前,标签从未改变。 有一些简单的规定:

    // ...
    int five;
SetFive:
    five = 5;
    // ...

    // ...
SetFive:
    {}
    int five = 5;

C23允许这样做。 至于海湾合作委员会为何允许这样做......似乎不符合规定。 建立海湾合作委员会,使用<代码>-std=c99 甚至允许这样做,但使用<代码>-pedantic的旗帜将发出警告。 然而,即使有<代码>st=c2x,Clang也不会将其汇编成册。

GCC is being non-compliant. In the future C revision this will be allowed, but I would recommend you use one of the work-a-rounds above until C23 is officially released.

www.un.org/spanish/ga/president

举例来说,这是有效的:

if (x==0) y=1;

But this is not:

if (x==0) int z;

在贴标签方面,在C17和之前,标签只能附有声明,而不是声明。

In C23 the grammar changes so that a label is implicitly followed by a null statement when it is part of a compound statement (i.e. contained within {}), so in that version of the standard it is allowed to have a label immediately proceeded by a declaration.

In other words, in C23 this:

my_label:
  int i;

Is treated as this:

my_label:
  ;
  int i;

海湾合作委员会显然允许这一新的辛迪加作为延伸,即使不采用C23模式,而部族则没有这种延伸。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签