Skip to content

Multiple variable declaration/assigment in C switch() statement yield an error in the executable #11639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Harm-Paas opened this issue Jul 28, 2021 · 1 comment
Labels
Component: Compilation Related to compilation of Arduino sketches Type: Bug Type: Duplicate Another item already exists for this topic

Comments

@Harm-Paas
Copy link

Harm-Paas commented Jul 28, 2021

/* --------------------------------------------------------------------------
 * Arduino compiler error :
 *  Multiple inline variable declarations/assigments in a C switch statement
 *  yield an error in the executable.
 *  After multiple variable declarations in the switch() construct
 *  the rest of the switch statement case entries become unreachable.
 * 
 * Compiler version : Arduino IDE 1.8.15
 * O.S. Windows10
 *
 * 2021-07-25 Harm Paas PA0HPG
 */

void pickchoice(char MyChoice) {
   char Cmd ;

   Cmd = MyChoice;
   
   switch(Cmd) {
   case 'A':
     int var1 = 1;        // Signed variables in switch() 
     Serial.println(var1);
     //...stmts...
   break;
   case 'B':
     int var2 = 2;
     Serial.println(var2);
   // ...stmts...
   break;
   case 'C':
     int var3 = 3;
     Serial.println(var3);
     // ...stmts...
   break;
   } // end switch
}
//   -----------------------------------------------------------------------
//   Correct version of the same function:
void correctpickchoice(char MyChoice) {
   char Cmd ;
   int var1 = 10 ; // Do not declare/assign variables in the switch
   int var2 = 20 ;
   int var3 = 30 ; 

   Cmd = MyChoice;
   
   switch(Cmd) {
   case 'A':
     var1 = 10;
     Serial.println(var1);
     //...stmts...
   break;
   case 'B':
     var2 = 20;
     Serial.println(var2);
     // ...stmts...
   break;
   case 'C':
     var3 = 30;
     Serial.println(var3);
   // ...stmts...
   break;
   } // end switch
}
//   -----------------------------------------------------------------------

void setup(){
  Serial.begin(9600);
}

void loop() {

  Serial.println("Executable error with multiple variable declarations in switch()");
  Serial.println("Wrong version:");
  pickchoice('A');
  pickchoice('B');
  pickchoice('C');

  Serial.println("Corrected version of the declaration :");
  
  correctpickchoice('A');
  correctpickchoice('B');
  correctpickchoice('C');

  while(true);
}
@per1234
Copy link
Collaborator

per1234 commented Jul 28, 2021

Hi @Harm-Paas. Thanks for taking the time to submit an issue.

I see we already have a prior issue report about this at #10154

It's best to have only a single issue per subject so we can consolidate all relevant discussion to one place, so I'll go ahead and close this in favor of the other.

If you would like assistance with your code, please post on the Arduino Forum. We can definitely help you out over there:
https://forum.arduino.cc/c/using-arduino/programming-questions/20

@per1234 per1234 closed this as completed Jul 28, 2021
@per1234 per1234 added Component: Compilation Related to compilation of Arduino sketches Type: Bug Type: Duplicate Another item already exists for this topic labels Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Compilation Related to compilation of Arduino sketches Type: Bug Type: Duplicate Another item already exists for this topic
Projects
None yet
Development

No branches or pull requests

2 participants