[c] conflicting types for 'outchar'

I am a newbie to C programming. I'm learning by reading the chapters and doing the examples from the book "Teach Yourself C" by Herbert Schildt. I'm trying to run this program in Dev C:

#include <stdio.h> #include <stdlib.h>  main() {     outchar('A');     outchar('B');     outchar('C'); }  outchar(char ch) {     printf("%c", ch); } 

but I get this error when I compile it:

20  1   C:\Dev-Cpp\main.c   [Error] conflicting types for 'outchar'  21  1   C:\Dev-Cpp\main.c   [Note] an argument type that has a default  promotion can't match an empty parameter name list declaration  15  2   C:\Dev-Cpp\main.c   [Note] previous implicit declaration of 'outchar' was here 

Please help me with this!

This question is related to c

The answer is


It's because you haven't declared outchar before you use it. That means that the compiler will assume it's a function returning an int and taking an undefined number of undefined arguments.

You need to add a prototype pf the function before you use it:

void outchar(char);  /* Prototype (declaration) of a function to be called */  int main(void) {     ... }  void outchar(char ch) {     ... } 

Note the declaration of the main function differs from your code as well. It's actually a part of the official C specification, it must return an int and must take either a void argument or an int and a char** argument.


In C, the order that you define things often matters. Either move the definition of outchar to the top, or provide a prototype at the top, like this:

#include <stdio.h> #include <stdlib.h>  void outchar(char ch);  int main() {     outchar('A');     outchar('B');     outchar('C');     return 0; }  void outchar(char ch) {     printf("%c", ch); } 

Also, you should be specifying the return type of every function. I added that for you.