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

298
Vistas
Why does sizeof(x)++ compile?

I have run into the following code snippet:

int a = 3;
printf("%d", sizeof(a)++);

Apparently this will compile with GCC 9.3.0 and -std=c99. While this does not compile:

printf("%d", sizeof(3)++);

GCC prints an error

error: lvalue required as increment operand

Before I have compiled the first snippet I would have expected such an error.

The operand of the postfix ++ operator shall be an lvalue as of C99 standard

The operand of the postfix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue.

About the return value of the sizeof Operator (as expected):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

The value of the result is implementation-defined, and its type (an unsigned integer type) is size_t, defined in (and other headers).

How is it possible the sizeof(a)++ compiles? Is this undefined behavior or am I missing something?

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

0

The sizeof is an operator, not a function, and as any operator it has a precedence, which is in C lower than of the ++ operator. Therefore the construct sizeof(a)++ is equivalent to sizeof a++ which is in turn equivalent to sizeof (a++). Here we have postincrement on a which is an lvalue so it is perfectly legal. If instead of a you have 3 which is not lvalue, the compilation will fail.

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