Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

437
Vistas
C vs C++ switch statement variable definition vs declaration

I was playing with some syntax and found some strange compiler rules, was wondering what the reasoning is for this

C will not compile this but C++ will:

switch (argc) {
case 0:
    int foo;
    break;
default:
    break;
}

Both C and C++ will compile this:

switch (argc) {
case 0:
    ; int foo;
    break;
default:
    break;
}

C will compile this but not C++:

switch (argc) {
case 0:
    ; int foo = 0;
    break;
default:
    break;
}

gcc -v is gcc version 4.9.3 (MacPorts gcc49 4.9.3_0) if it matters. I realize the solution is to wrap the contents of case 0: with curly brackets, but I am more interested in the reasoning for compilation errors

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

case 0:
    int foo;

In both C and C++ a labeled statement is a label followed by a statement. However in C++ the definition of a statement includes "block declarations" (that is declarations and definitions that may appear in a block) whereas in C it does not (in C a block is a sequence of "block items", which are either block declarations or statements - in C++ it's a sequence of statements, which include block declarations).

case 0:
    ; int foo;

This works because ; is a(n empty) statement in both C and C++, so here we indeed have a label followed by a statement.

case 0:
    ; int foo = 0;

As was already explained in the comments, this does not work in C++ because C++ makes it illegal to jump over an initialization.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda