1

I have the following program

main()
{
   char a,b;
   printf("will i get the job:");
   scanf("%c",&a);
   printf("%c",a);
   printf("We did it");
}

I saved the file as Hope.c. When I try to compile the code above with the gcc compiler, I will get the following error:

Hope.c:In function 'main':
Hope.c:4:2:warning:incompatible implicit declaration of built-in function 'printf' [enabled by default]          
Hope.c:5:2:warning:incompatible implicit declaration of built-in function scanf[enabled by default]

The compiler gives this error when I use printf() or scanf(), even in a simple "Hello world" program.

Is there something wrong with my code, or is there a problem with the compiler?

6
  • 6
    Add #include <stdio.h> at the top. Also, remove the c++ tag. Commented Aug 22, 2013 at 15:52
  • You need #include <stdio.h> Commented Aug 22, 2013 at 15:53
  • 1
    One short-cut almost in all cases whenever you get a warning like warning:incompatible implicit declaration of built-in function 'any function' [enabled by default] , look for the proper header files Commented Aug 22, 2013 at 15:59
  • 2
    "is there a problem with the compiler?" I would need 100-to-1 odds before betting against the compiler. Compilers are far more often correct than my initial coding - though not infallible. Recommend assuming the compiler is rarely wrong. (Unless you are in the embedded world.) Commented Aug 22, 2013 at 16:30
  • It's not an error, it's a warning ! You still have it compiled. Commented Aug 22, 2013 at 16:31

4 Answers 4

6

You're missing #include <stdio.h> at the top. Note that these were warnings, though, not errors. Your program should still have compiled as is.

Also, for good measure, you should write out the return type and parameters to main() and return a value at the end.

#include <stdio.h>

int main(void)
{
   char a,b;
   printf("will i get the job:");
   scanf("%c",&a);
   printf("%c",a);
   printf("We did it");

   return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

although your absolutely right, I want to add that these warnings may result in a runtime crash, e.g. I have seen printf( "%s", func( ) ) throwing seg fault, although func( ) returned a valid string, only because there was no prototype that declared the return type as char *
As of the 1999 standard, the return 0; is unnecessary because it's implicit in reaching the closing } -- though IMHO it's still not a bad idea to have it. Also as of the 1999 standard, calling any function without a visible declaration is a constraint violation, requiring a compile-time diagnostic. (The declaration still isn't required to be a prototype, but all function declarations specify the return type.)
4

When you call functions that you're not familiar with, look at the man page and include the headers that are mentioned there. It's #include <stdio.h> in your case.

That's really extermely important, e.g. I have experienced printf( "%s", func( ) ) causing a segmentation fault although func() returned a valid null terminated string, but there was no prototype that declared the return type of func() as char * (doing a little bit research, we found that only the last four bytes of the 64 bit pointer have been passed to printf())

Comments

2

Yes, there's something wrong with the code. You're using the functions printf and scanf without declaring them.

The usual thing to do is use the declarations shipped with the compiler (because they're known to be correct) with

#include <stdio.h>

Comments

2

C.89/C.90 permits implicit declarations of functions. Your warning messages are informing you that you have not provided explicit declarations for scanf and printf. As has been mentioned, you can correct this by adding #include <stdio.h> to the beginning of your program. By not doing so, the behavior of your program is undefined.

Because scanf() and printf() have implicit declarations, they are treated as if their prototypes were given as:

extern int scanf ();
extern int printf ();

These declarations state that scanf() and printf() take an as of yet unknown number of arguments and return and int. However, this kind of declaration still assumes that these functions will take a fixed number of arguments. This is incompatible with their true prototypes, in which they take a variable number of arguments:

extern int scanf (const char *, ...);
extern int printf (const char *, ...);

Your C compiler apparently knows about the true prototypes of these functions because it treats those functions as "built-ins", meaning it can generate special case code when compiling to source code that calls those functions. Since the implicit declaration does not match its built-in knowledge of their prototypes, it generated the warning.

A compiler that did not have this "built-in knowledge" probably would not have generated the warning. It would then have generated code to call scanf() and printf() as if they took a fixed number of arguments. The error then may occur at runtime, since the calling convention for a function that takes a variable number of arguments may differ from the calling convention of a function that takes a fixed number of arguments.

This is all described in C.89 §3.3.2.2.

If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration
extern int identifier();
appeared.
...
If the expression that denotes the called function has a type that does not include a prototype, the integral promotions are performed on each argument and arguments that have type float are promoted to double. ... If the function is defined with a type that includes a prototype, and the types of the arguments after promotion are not compatible with the types of the parameters, or if the prototype ends with an ellipsis ( ", ..." ), the behavior is undefined.

Note that C.99 removes the allowance for implicit function declarations.

2 Comments

Do we really need three paragraphs citations to explain that the OP forgot to add a header?
@Jim: It is to explain the three things going on: (1) what is an implicit declaration, (2) why there is a warning message if implicit declarations are allowed, and (3) why using the implicit declaration of a function that takes a variable number of arguments leads to undefined behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.