Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda