user2469493
user2469493

Reputation: 19

Extremely simple C program won't compile in gcc compiler

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?

Upvotes: 1

Views: 4542

Answers (4)

jxh
jxh

Reputation: 70382

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.

Upvotes: 2

Ingo Leonhardt
Ingo Leonhardt

Reputation: 9884

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())

Upvotes: 4

John Kugelman
John Kugelman

Reputation: 361565

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;
}

Upvotes: 6

Ben Voigt
Ben Voigt

Reputation: 283624

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>

Upvotes: 2

Related Questions