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

221
Vistas
program compiled when function parameter does not match the const parameter declaration in c

I have a piece of code like this:

#include <stdio.h>

int add(const int x, const int y);

int main()
{
    printf("%d", add(9, 8));

    return 0;
}

int add(int x, int y)
{
    return x + y;
}

I forward declared the function "add" with const parameters after that i defined it without the const parameter, and when i compile it the compiler gives no complain.
The out put of the program is: 17. Why does this happen ?

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

0

Well, the function declaration and function definition have to be compatible, we all know that. So the same name and same return type and type of parameters. So we know from C11 6.7.6.3p15:

For two function types to be compatible, [...] corresponding parameters shall have compatible types. [...]

But, there is an explicit backdoor, later in that text:

(In the determination of type compatibility [...] each parameter declared with qualified type is taken as having the unqualified version of its declared type.)

The type-qualifier is for example const. And it is ignored. You can put any type-qualifier and it is just ignored when checking if the function declarations are the same with each other.

int func(int n);
int func(volatile int n);
int func(const int n);
int func(const volatile int n);
int func(int n) {
    return n;
}
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