2

Possible Duplicate:
c - warning: implicit declaration of function ‘printf’

Just learning C. I'm getting warnings in a hello world program:

main()
{
    printf("Hi\n");
}

here's the warning:

x.c: In function 'main':
x.c:2: warning: incompatible implicit declaration of built-in function 'printf'

How can I eliminate it?

Community
  • 1
  • 1
user1936469
  • 39
  • 1
  • 3
  • 8
    You forgot `#include `. – cnicutar Dec 29 '12 at 15:42
  • 1
    That's not an error, it's a warning. –  Dec 29 '12 at 15:43
  • 2
    Also, `main()` should be at least `int main()` but most preferrably `int main(void)` or `int main(int argc, char *argv[])`. –  Dec 29 '12 at 15:46
  • But it SHOULD be an error: use `-Wall -Werror` when compiling. Even the Linux kernel does that to a large degree these days. – Mats Petersson Dec 29 '12 at 21:01
  • This is, in fact a duplicate of what @birryree linked. It covers exactly the same ground. I'm re-opening this so that the most appropriate close reason is applied. – Tim Post Jan 04 '13 at 08:12

6 Answers6

8

You haven't included header file stdio.h you can include this by #include<stdio.h>

A header file contains forward declarations of functions and variables. Every external function will be mentioned in a header file, including libraries that are pre-compiled into object code, and source files required to build the C program

what you need to so is thanks to the #include line, which tells it where the definition of printf can be found.

#include<stdio.h>
main()
{
    printf("Hi\n");
}

check The Header File

and printf=> void printf (const char *format, ...);


and now explanation of above program

in line first

#=> it is Processors the preprocessor is a translation phase that is applied to your source code before the compiler proper gets its hands on it

recomended reading C - Pre-Processors

main() function => the main function is where a program starts execution. It is responsible for the high-level organization of the program's functionality, and typically has access to the command arguments given to the program when it was executed.

printf() is nearly full implementation of standard ANSI C printf function, which sends the formatted output to the screen in terminal (TTY) mode. this is belongs to stdio.h header file

good read wikipedia article and Where are C/C++ main function's parameters?

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
6

You are not including the library, add this before you start your code

#include <stdio.h>

because you are using printf() and it's an output function, belongs to stdio.h library...

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
4

you are missing the #include that declares the libraries.

add :

#include <stdio.h>

main(){

  ...etc

you need to declare functions before they can be used.

kdubs
  • 1,596
  • 1
  • 21
  • 36
1

Include the following header file to access the definition of printf.

#include <stdio.h>
Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

incompatible implicit declaration... This sentence tells something is to be there before printf. That something is #include "Every inbuilt function in C should refer Predefined procedures present in lib's"

0

Include header file in your program..

#include<stdio.h>

Implicit declaration warning occurs when the compiler is expecting a function declaration or function prototype and printf() function prototype is declared in #include<stdio.h>

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42