definition of stdio.h

L

lak

if i view stdio.h there are only symbolic constants.
where is the definition of printf and scanf is available?
i want to see the definition of printf and scanf and where it is
stored?
 
I

Ian Collins

lak said:
if i view stdio.h there are only symbolic constants.
where is the definition of printf and scanf is available?
i want to see the definition of printf and scanf and where it is
stored?
Either in <stdio.h> or a header included by <stdio.h>
 
R

Richard Heathfield

Ian Collins said:
Either in <stdio.h> or a header included by <stdio.h>

Deeply unlikely. Only an idiot would define printf and scanf in a header.
Since you yourself are not an idiot, I think you must have misread his
requirement. He is after the function definitions, not the declarations.
 
I

Ian Collins

Richard said:
Ian Collins said:


Deeply unlikely. Only an idiot would define printf and scanf in a header.
Since you yourself are not an idiot, I think you must have misread his
requirement. He is after the function definitions, not the declarations.
:)

A bit odd that the OP's <stdio.h> only has symbolic constants.
 
R

Richard Tobin

lak said:
if i view stdio.h there are only symbolic constants.
where is the definition of printf and scanf is available?
i want to see the definition of printf and scanf and where it is
stored?

You want to look at the source code for the library, which may well
not be available on you system. If you're using Linux, BSD, or some
other free operating system you will be able to get hold of it,
otherwise you're probably out of luck.

You can find the FreeBSD implementation at

http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/stdio/

(click on the red version numbers to see the code).

-- Richard
 
R

Richard Tobin

If you examine this output
you'll find the declarations for printf and scanf somewhere.

I suspect he really wants the definitions, not the declarations.

-- Richard
 
S

santosh

lak said:
if i view stdio.h there are only symbolic constants.
where is the definition of printf and scanf is available?
i want to see the definition of printf and scanf and where it is
stored?

It should be in <stdio.h>. However it might use so many compiler
extensions as to unrecognisable to a beginner or someone who only knows
ISO C. Also as Ian notes, it might be in another file included by
<stdio.h>.

Write a simple program using both printf and scanf and direct the
compiler to produce preprocessed output. If you examine this output
you'll find the declarations for printf and scanf somewhere.
 
S

santosh

Richard said:
Ian Collins said:


Deeply unlikely. Only an idiot would define printf and scanf in a
header. Since you yourself are not an idiot, I think you must have
misread his requirement. He is after the function definitions, not the
declarations.

Oops. I made the same mistake! Oh well.
 
K

Keith Thompson

lak said:
if i view stdio.h there are only symbolic constants.
where is the definition of printf and scanf is available?
i want to see the definition of printf and scanf and where it is
stored?

Do you want the definitions or the declarations?

The declaration of printf is something like this:

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

whereas the definition is something like this:

int printf(const char * restrict format, ...)
{
/* lots and lots of code here */
}

The definition won't be in any header file, but the declaration
probably will be.

But the standard headers are written for the compiler to use, not
necessarily for programmers to read, and they're free to use ugly
compiler-specific extensions that you can't, or shouldn't, use in your
own code.

If you want to know about a standard function, you're much better off
reading a reference book or your system's documentation. Reading
headers can tell you things about your specific implementation, but
depending on that information can make your own code non-portable.
 
K

Keith Thompson

Richard Heathfield said:
Ian Collins said:

Deeply unlikely. Only an idiot would define printf and scanf in a header.

printf and scanf could be defined as macros. (I *think* that C99's
variadic macros are up to the task.)
Since you yourself are not an idiot, I think you must have misread his
requirement. He is after the function definitions, not the declarations.

He asked for definitions, but I'm not convinced that that's really
what he's after (which is why I asked him for clarification
elsethread).
 
R

Richard Tobin

Keith Thompson said:
whereas the definition is something like this:

int printf(const char * restrict format, ...)
{
/* lots and lots of code here */
}

Probably not "lots and lots", in this case. I suspect that most
implementations will look just like this one from FreeBSD:

int printf(char const * restrict fmt, ...)
{
int ret;
va_list ap;

va_start(ap, fmt);
ret = vfprintf(stdout, fmt, ap);
va_end(ap);
return (ret);
}

-- Richard
 
A

Army1987

printf and scanf could be defined as macros. (I *think* that C99's
variadic macros are up to the task.)

There must be also a real declaration, in case I use
(printf)("foo");
or
#undef printf
printf("foo");
And that one is very unlikely to be also a definition. There still
have to be a (probably precompiled) printf function somewhere.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.
 
K

Keith Thompson

Army1987 said:
There must be also a real declaration, in case I use
(printf)("foo");
or
#undef printf
printf("foo");
And that one is very unlikely to be also a definition. There still
have to be a (probably precompiled) printf function somewhere.

Sure. My (somewhat silly) point was simply that a non-idiot might
define printf and scanf in a header (though it must also be fully
defined elsewhwere).
 
R

Richard Heathfield

Keith Thompson said:
[...] Only an idiot would define printf and scanf in a
header.

[...] My (somewhat silly) point was simply that a non-idiot might
define printf and scanf in a header (though it must also be fully
defined elsewhwere).

Yes, you have made your point well. I must re-phrase my claim to take your
objection into account.

Only an idiot or a completely macro-crazed bozo would define printf and
scanf in a header.

:)
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
[...] Only an idiot would define printf and scanf in a
header.

[...] My (somewhat silly) point was simply that a non-idiot might
define printf and scanf in a header (though it must also be fully
defined elsewhwere).

Yes, you have made your point well. I must re-phrase my claim to take your
objection into account.

Only an idiot or a completely macro-crazed bozo would define printf and
scanf in a header.

:)

printf() is simply fprintf() with the first argument implicitly set to
stdin. Likewise, scanf() is simply fscanf() with the first argument
implicitly set to stdout.

Assuming a C99-compliant compiler, and assuming that C99's variadic
macros are up to the task (I suppose I should confirm that at some
point), I see no reason not to define printf(...) as a macro that
invokes fprintf(stdout, ...) (in addition, of course, to the mandatory
definitions (outside the header) as functions).
 
R

Richard Heathfield

Keith Thompson said:

Assuming a C99-compliant compiler, and assuming that C99's variadic
macros are up to the task (I suppose I should confirm that at some
point), I see no reason not to define printf(...) as a macro that
invokes fprintf(stdout, ...) (in addition, of course, to the mandatory
definitions (outside the header) as functions).

Ah, a very straight bat indeed. Well played, sir. :)
 
C

CBFalconer

Keith said:
.... snip ...

printf() is simply fprintf() with the first argument implicitly
set to stdin. Likewise, scanf() is simply fscanf() with the first
argument implicitly set to stdout.

I suspect you have performed some unusual operations on the i/o
system before using these macros. :)
 
K

Keith Thompson

CBFalconer said:
Keith Thompson wrote:
... snip ...

I suspect you have performed some unusual operations on the i/o
system before using these macros. :)

Only in my head. (Swap stdin and stdout, of course.)
 
A

Army1987

Assuming a C99-compliant compiler, and assuming that C99's variadic
macros are up to the task (I suppose I should confirm that at some
point), I see no reason not to define printf(...) as a macro that
invokes fprintf(stdout, ...) (in addition, of course, to the mandatory
definitions (outside the header) as functions).
Considering that it is very likely that the only differences
between the (real) definitions of printf and fprintf are
+ int printf(const char * restrict format, ...)
- int fprintf(FILE * restrict stream, const char * restrict format, ...)
and
+ ret = vfprintf(stdout, format, ap)
- ret = vfprintf(stream, format, ap)
I can't see any advantage in using the macro...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top