Why is a 'conflicting type' error being thrown when I execute this program?

前端 未结 3 1172
心在旅途
心在旅途 2021-01-19 15:48

In K&R Chapter 1.9, I\'ve been experimenting with the program provided below. Particularly, what would happen if I removed certain decelerations of functions.

So

3条回答
  •  佛祖请我去吃肉
    2021-01-19 16:10

    As per the latest C standard C11, every function has to be (at least) declared before it has been used. In that way, compiler will have the knowledge of the function signature.

    In the code, while calling a function, if the declaration (at least) is not visible to the compiler, (due to some legacy reason), the compiler (used to) assume

    • The function returns an int
    • accepts any number of paramater.

    Later, when you define the function to have a return type other than int, it will create the conflict.

    That is why,

    • removing the forward declaration of getline() produces no error.
    • removing the forward declaration of copy() produces the error, mismatch in the return type.

    Along the same line, main() is no longer required to be supported, as implicit int has also been removed. You should write int main(void), at least to be standard conforming.

提交回复
热议问题