What will happen if I don't include header files

前端 未结 5 2077
误落风尘
误落风尘 2020-12-06 08:08

What will happen if I don\'t include the header files when running a c program? I know that I get warnings, but the programs runs perfectly.

I know that the header f

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 08:37

    For compatibility with old program C compilers can compile code calling functions which have not been declared, assuming the parameters and return value is of type int. What can happen? See for example this question: Troubling converting string to long long in C I think it's a great illustration of the problems you can run into if you don't include necessary headers and so don't declare functions you use. What happened to the guy was he tried to use atoll without including stdlib.h where atoll is declared:

    char s[30] = { "115" };
    long long t = atoll(s);
    printf("Value is: %lld\n", t);
    

    Surprisingly, this printed 0, not 115, as expected! Why? Because the compiler didn't see the declaration of atoll and assumed it's return value is an int, and so picked only part of the value left on stack by the function, in other words the return value got truncated.

    That's why of the reasons it is recommended to compile your code with -Wall (all warnings on).

提交回复
热议问题