Skip to content

Fix compiler error `a label can only be part of a statement and a de…#343

Open
LupusE wants to merge 2 commits intoRfidResearchGroup:mainfrom
LupusE:main
Open

Fix compiler error `a label can only be part of a statement and a de…#343
LupusE wants to merge 2 commits intoRfidResearchGroup:mainfrom
LupusE:main

Conversation

@LupusE
Copy link
Contributor

@LupusE LupusE commented Feb 2, 2026

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

You are welcome to add an entry to the CHANGELOG.md as well

@suut
Copy link
Contributor

suut commented Feb 2, 2026

You can also enclose the contents of the case within { }, it silences the warning but also has the added effect of not defining the variable globally so you don't end up in that case:

switch (2) {
  case 1:
    int x = 2;
    break;
  case 2: ;
    printf("%d\n", x);
    break;
}

Here it would print a random value for x, it's declared globally because it's part of the switch case in the same scope, but it's not defined because case 1 was not executed, so you end up with a random value.

You have the same kind of problem if you declare variables with the same name in two different cases, they would clash together

So instead you can do

switch (2) {
  case 1: {
    int x = 2;
    break;
  }
  case 2: ;
    printf("%d\n", x);
    break;
}

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

Built artifacts for commit 5d2946f

Firmware

Client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants