conflicting types error when compiling c program using gcc

前端 未结 3 1629
囚心锁ツ
囚心锁ツ 2020-12-01 08:05

I tried to compile following program with gcc.

0 #include  
1 
2 main ()
3
4 {    
5  char my_string[] = \"hello there\";      
6
7  my_print         


        
相关标签:
3条回答
  • 2020-12-01 08:22

    You have to declare your functions before main()

    (or declare the function prototypes before main())

    As it is, the compiler sees my_print (my_string); in main() as a function declaration.

    Move your functions above main() in the file, or put:

    void my_print (char *);
    void my_print2 (char *);
    

    Above main() in the file.

    0 讨论(0)
  • 2020-12-01 08:43

    To answer a more generic case, this error is noticed when you pick a function name which is already used in some built in library. For e.g., select.

    A simple method to know about it is while compiling the file, the compiler will indicate the previous declaration.

    0 讨论(0)
  • 2020-12-01 08:44

    If you don't declare a function and it only appears after being called, it is automatically assumed to be int, so in your case, you didn't declare

    void my_print (char *);
    void my_print2 (char *);
    

    before you call it in main, so the compiler assume there are functions which their prototypes are int my_print2 (char *); and int my_print2 (char *); and you can't have two functions with the same prototype except of the return type, so you get the error of conflicting types.

    As Brian suggested, declare those two methods before main.

    0 讨论(0)
提交回复
热议问题