doesn't printf need stdio.h? 
Author Message
 doesn't printf need stdio.h?

a lot many compilers do not seem to mind executing the printf function
even when no header files are included. what could be the reason???


Mon, 27 Sep 2004 21:27:39 GMT  
 doesn't printf need stdio.h?


Quote:
> a lot many compilers do not seem to mind executing the printf function
> even when no header files are included. what could be the reason???

When I do this:
gcc -c -O -Wall -ansi -pedantic foo.c

On this:
int main(void)
{
    printf("Test\n");
    return 0;

Quote:
}

I get this:
foo.c: In function `main':
foo.c:3: warning: implicit declaration of function `printf'

Either turn up your warnings or get a more helpful compiler.



Mon, 27 Sep 2004 21:46:25 GMT  
 doesn't printf need stdio.h?

Quote:
> a lot many compilers do not seem to mind executing the printf function
> even when no header files are included. what could be the reason???

The reason might be, that the compiler simply assumes a
declaration for printf, deriving it from its first call. After
your code has been compiled and linked with some library the
according function (printf) is found in the library, but the
previous declaration isn't tested against it and your linker
(probably part of the compiler) referes to that function
whenever it is called in the program.

The header stdio.h, that is to provide the propper prototype for
printf, will not hold the function or what actually is the
function, but only a description for the compiler of how the
function may be called (and in printfs case this is a bit odd),
so the compiler can check for you whether you are calling a
function correctly.
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Mon, 27 Sep 2004 23:38:18 GMT  
 doesn't printf need stdio.h?

Quote:
>a lot many compilers do not seem to mind executing the printf function
>even when no header files are included. what could be the reason???

pre-c99 compilers are very forgiving, though you can often cause them to
issue a message in these cases (via some mechanism).  a c99 compiler would
have issued a diagnostic (and perhaps even stopped translating) since there
was no prototype in scope.

--
bringing you boring signatures for 17 years



Tue, 28 Sep 2004 00:54:48 GMT  
 doesn't printf need stdio.h?

Quote:

> a lot many compilers do not seem to mind executing the printf function
> even when no header files are included. what could be the reason???

It's the "implicit int" rule of pre-C99 implementations.

Unfortunately, yes, the implementation *does* need to have seen stdio.h
when encountering printf, for its prototype. Without that, the behaviour
of the program is undefined. In other words, Bad Things Might Happen.
Always include stdio.h when your translation unit uses printf.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 28 Sep 2004 16:24:00 GMT  
 doesn't printf need stdio.h?

Quote:
>a lot many compilers do not seem to mind executing the printf function
>even when no header files are included. what could be the reason???

In pre-C99 implementations, function declarations are not mandatory.
If the compiler encounters a function with to declaraction in scope,
it will assume an implicit declaration of the form:

    int foo();

i.e. a function with a fixed, but unspecified, number of parameters,
returning int.  This is guaranteed to work correctly if the actual
definition of the function is compatible with such a declaration, which
is the case of, for example, getchar() or puts().

printf(), however, is not a function with a fixed number of parameters,
so code that uses it without providing a proper declaration for it is
incorrect.  But the compiler doesn't know that and it will just generate
code as if the implicit declaration were correct.  By accident, the
generated code happens to be identical to the code generated if printf()
was properly declared, i.e. many implementations don't handle variadic
function calls in a special way.

Another possibility is that the compiler actually knows that printf() is
a variadic function and silently ignores the implicit declaration.

Historically, no declaration or header was needed for printf, the
compiler was supposed to do the right thing, before the C89 standard.
Because of that, there were applications that used printf() without
including <stdio.h> that were correct at the time they were written.
They were no longer correct as standard C programs, but the implementors
tried to avoid breaking the existing applications as much as possible.
Because of this, printf continued to work without a proper declaration
in scope even with standard C compilers.  I have never used an
implementation where printf() doesn't work as expected in the absence of
a proper declaration, although such an implementation probably exists.

As far as the standard is concerned, an undeclared printf() invokes
undefined behaviour.  This means that the compiler is not required to
produce a diagnostic and that it can do anything it wants with that code,
including generating the same code as if a proper declaration for
printf() were in scope.  As a quality of implementation issue, most
implementors seem to prefer this alternative.  Quite understandably:
no implementor is happy to have users complaining that their code, that
worked on all the platforms it was tried before, for the last 15 years,
no longer works with his compiler.  It's quite hard to convince such
customers that the fault is in their code :-)

C99 still allows implementors to do what the programmers expect in such
cases, but it also requires a diagnostic when a function is called without
a declaration in scope, the implicit function declarations are gone.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 28 Sep 2004 20:03:59 GMT  
 doesn't printf need stdio.h?

Quote:


> > a lot many compilers do not seem to mind executing the printf function
> > even when no header files are included. what could be the reason???

> It's the "implicit int" rule of pre-C99 implementations.

> Unfortunately, yes, the implementation *does* need to have seen stdio.h
> when encountering printf, for its prototype. Without that, the behaviour
> of the program is undefined.

Does the implicit int rule say that if the prototype is not available
the assume the return type to be of type int and the type of the
arguments to same as that for the first call. In that case if I simply
have

int main()
{
        printf("Hello");      
        printf("World");

Quote:
}

Now this should be defined completly.
What if anything is undefined about this?

Quote:
> In other words, Bad Things Might Happen.
> Always include stdio.h when your translation unit uses printf.

Nopes, at least I don't agree with the word *always*

If the above is undefined the this is I bet defined

int main()
{
        int printf(const char*, ...); //Or IMO even int printf(char*,...);
        printf("Hello");
        printf("World");

Quote:
}

--
Minti

        If I only knew what works, I would have been a locksmith
                --      Albert Einstien



Tue, 28 Sep 2004 22:40:21 GMT  
 doesn't printf need stdio.h?
* Minti
| Does the implicit int rule say that if the prototype is not available
| the assume the return type to be of type int and the type of the
| arguments to same as that for the first call. In that case if I simply
| have
|
| int main()
| {
|       printf("Hello");      
|       printf("World");
| }
|
| Now this should be defined completly.
| What if anything is undefined about this?

Functions which take a variable number of arguments must have a
prototype in scope at the point of call (otherwise the behaviour is
undefined).  Substituting `puts' for `printf', your example is fine
(in C89; C99 requires a declaration).

Jeremy.



Tue, 28 Sep 2004 22:44:26 GMT  
 doesn't printf need stdio.h?

Quote:
>Does the implicit int rule say that if the prototype is not available
>the assume the return type to be of type int and the type of the
>arguments to same as that for the first call.

Nope, there is no such rule.  A compiler might warn if different calls
of the same implicitly declared function are inconsistent, but it is
not required to.  

The implicit declaration as a function with a fixed, but unspecified
number and type of arguments is not modified after the first call.

Quote:
>In that case if I simply have

>int main()
>{
>    printf("Hello");      
>    printf("World");
>}

>Now this should be defined completly.

Chapter and verse, please.

Quote:
>What if anything is undefined about this?

A variadic function is called without a proper declaration in scope.

Quote:
>> In other words, Bad Things Might Happen.
>> Always include stdio.h when your translation unit uses printf.

>Nopes, at least I don't agree with the word *always*

>If the above is undefined the this is I bet defined

>int main()
>{
>    int printf(const char*, ...); //Or IMO even int printf(char*,...);
>    printf("Hello");
>    printf("World");
>}

Well, the actual C99 declaration for printf is:

    int printf(const char * restrict, ...);

which has a completely different meaning in C89 (where "restrict" is an
ordinary identifier) and I don't know if the correct C89 declaration
is also compatible with the C99 definition.

This problem can be trivially avoided by not declaring printf yourself
at all.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Fri, 01 Oct 2004 20:30:44 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Why doesn't f2c produce printf?

2. why doesn't printf work right?

3. replacing printf with my printf, then calling C lib's printf

4. VC7: cstdio doesn't put printf in std

5. printf %E format doesn't work on PockePC

6. Why you should use puts(3) when you don't need printf(3)

7. dup()'ed pipe()'s to stdio

8. How do I write a server which doesn't need too many threads

9. Directory Tree using CTreeCtrl Help Needed (doesn't everyone)

10. why isn't stdio full-duplex?

11. iostreams vs stdio vs win32 API's

12. Need help with stdio in C

 

 
Powered by phpBB® Forum Software