Function "strcmp()" working without adding "string. h"

Asked

Viewed 203 times

4

I wrote an algorithm in C that uses the function strcmp(). Even forgetting to add the string.h the algorithm worked.

I would like to understand how it worked since I only found this function in the library string.h.

#include<stdio.h>
#include<stdlib.h>


typedef struct Produto{
    char nome[30];
    float preco;
}Produto;

int compara_nome(const void * A, const void* B){
Produto * pointerA=(Produto *) A;
Produto * pointerB=(Produto *) B;

return strcmp (pointerA->nome,pointerB->nome);/* como isto está funcionando sem o string.h ?*/


}
int compara_preco(const void * A, const void * B){
    Produto * pointerA=(Produto *) A;
    Produto * pointerB=(Produto *) B;
return (pointerA->preco-pointerB->preco);

}


int main(){
 static Produto Estoque[10]={{"Leite",5.50},{"Donuts",23.6},{"Detergente",4.15},{"Acucar",7.84},
 {"Brigadeiro",12.30},{"Limão",3.48},{"Morango",6.21},{"Tomate",4.12},{"Feijao",3.10},{"Skol",7.89}};

 qsort(Estoque,10,sizeof(Produto),compara_nome);

 int i;
 for(i=0;i<10;i++){
    printf("Nome: %-10s\t  Preco:%5.2f \t\n",Estoque[i].nome,Estoque[i].preco);
 }
qsort(Estoque,10,sizeof(Produto),compara_preco);    
printf("-----------------------------------------------");
printf("\n");
 for(i=0;i<10;i++){
    printf("Nome: %-10s\t  Preco:%5.2f \t\n",Estoque[i].nome,Estoque[i].preco);
 }  

    return 0;
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

5


What’s in the header file is just the signing function, not its implementation. If the implementation is available, the linkediting will work. In this case the compiler recognizes that it is possible to call it without further checks and trusts that it will work. The ideal is not to do so because calling yourself wrong will give error at runtime, an error that could be detected in the compilation if had the signature evaluated.

C has a lot of things that work, but there’s no guarantee it’s right. Remember that C is a portable assembly and not a high-level language with all possible checks. That’s why I always say:

Fiat 147 todo detonado andando pelas ruas

Especially in C people need to get used to the philosophy of understanding what is doing and happening and not rely on the outcome. In modern C this is considered a mistake. It is possible to connect an analysis in the compiler not to let happen, I advise to do it, otherwise it will deal with something like "works on my machine" which is always a programmer problem.

Signing is just a contract to be followed, it is not something that executes a code. Execution will be inserted normally and you hope it works.

  • Interesting!! I had the idea that the compiler or linkeditor only added functions cited in the prototypes (or signature?) of the headers. Apparently that’s not the case. When you talk about " modern C", are you talking about ISO’s? I’ve read some things about them, but I don’t know if I should focus on them now at the beginning. I’m sorry for the lack of knowledge of the terms of the area.

  • Each one does what he thinks is best for himself, and some people prefer to stay in the old way because it is supposedly more portable (even if not using signatures is less portable), I prefer to always work with C99 at least or C18 where it works. Compilers who do not accept, these novelties are usually platforms that you have to do code differently anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.