switch case seems not to work with assigning new variable inside case.

#define button1 5
#define button2 6
int state=2;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  digitalWrite(button1, HIGH);
  digitalWrite(button2, HIGH);
  Serial.print("setup");
}

void loop() {
  // put your main code here, to run repeatedly:
  
 switch(state) {
 case 1:
  delay(800);
  Serial.print("state1");
  int b1 = digitalRead(button1);  // if I remove these 2 lines readint buttons , machine states are updated correctly
  int b2 = digitalRead(button2);   // otherwise 
  state=2;
  break;

 case 2:
  Serial.print("state2");
  delay(1200);
  state=1;
  break; 
 }
}

Hello .
in this simple 2 state machine, if I put those 2 lines reading pin it stops working.
is this common knowledge Im missing ? you cant declare inside a case ?
Ive seen that declaring b1 and b2 as global it works.
thanks for the support in advance.

simple: don't declare variables within switch case.

What happens if you enclose the following in a block as in :

 case 1:
{
  delay(800);
  Serial.print("state1");
  int b1 = digitalRead(button1);  // if I remove these 2 lines readint buttons , machine states are updated correctly
  int b2 = digitalRead(button2);   // otherwise
  state=2;
  break;
}

Incidentally, did you get any compiler warnings ?

Turn your compiler warnings on.

noiasca:
simple: don't declare variables within switch case.

You can do, but just make sure you define the scope of those variables.

Ok i will turn on compiler warnings. Thanks for the tip.
Ive been enclosing in brakets as well with no results.
The declaration inside case: reduced memory use thats why I went there...

Ive been enclosing in brakets as well with no results.

Please post the sketch where you tried enclosing the code for cases in { and }

oswe:
The declaration inside case: reduced memory use

No, it just moved it somewhere else.