J
Jrdman
someone has an idea on how the printf function is programmed ?
Jrdman said:someone has an idea on how the printf function is programmed ?
Jrdman said:someone has an idea on how the printf function is programmed ?
someone has an idea on how the printf function is programmed ?
raashid bhatt said:
It's a function call in *any* environment. And the rest of your answer is
unnecessarily platform-specific - the printf function can be written
portably.
Ron Ford said:
Yes.
Yes.
[sprintf ]I'd be curious to see a useful snippet.
sprintf() can be written portably. printf() cannot, though it can be written
on top of putchar(), which has to be platform-specific.
printf can also be written portably. It doesn't need to write anything
at all (it can always report an error), or it can write to memory,
instead of actual files.
Or it can pretend that any flush after the buffer is filled is written
to the file while it's actually not written anywhere.
P.S. Please don't reply to ron ford, he is a troll.
[...]printf can also be written portably. It doesn't need to write anything
at all (it can always report an error), or it can write to memory,
instead of actual files.
Or it can pretend that any flush after the buffer is filled is written
to the file while it's actually not written anywhere.
printf can also be written portably. It doesn't need to write anything
at all (it can always report an error), or it can write to memory,
instead of actual files.
Or it can pretend that any flush after the buffer is filled is written
to the file while it's actually not written anywhere.
[...]
If it doesn't write anything at all, or writes to memory rather than
to a file, or pretends to write to a file while failing to do so, then
it's not an implementation of printf.
Input and output, whether to or from physical devices such as terminals and tape drives,
or whether to or from ï¬les supported on structured storage devices, are mapped into
logical data streams, whose properties are more uniform than their various inputs and
outputs. Two forms of mapping are supported, for text streams and for binary
streams.
But the bulk of the processing done by printf *can* be done portably.
In fact, printf is typically a simple wrapper around fprintf.
And there's nothing non-portable about performing output by calling
putchar.
If you look at all the functions declared in <stdio.h> that perform
output, *at least one* of them has to be implemented non-portably --
and typically, I suspect, more than one of them will be.
I see the syntax here in §7.2.
What I think a person might call this is an internal write. In fortran, we
go so far as to speak of "internal files," when what we really mean is the
character variable that we write to. I/O need not apply.
I'd be curious to see a useful snippet
In fact, printf is typically a simple wrapper around fprintf.
James Kuyper said:Keith Thompson wrote:
...
That might be the case if it's written as a macro; however, the
function version, if written as a C function, would have to be a
wrapper around vfprintf(), rather than fprintf(). That's an example of
why vfprintf() was invented.
Um, yeah...
This, for example, is a valid implementation of printf:
int printf(const char *format, ...) { __set_errno(); return -1; }
On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
Another one to add the the Official CLC Conforming Standard Library...
void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
}
Bart said:This one is not conforming.
The result of calling malloc(0) is well-defined: either NULL or a
non-NULL value. Which you get has to be documented.
This one is not conforming.
The result of calling malloc(0) is well-defined: either NULL or a non-
NULL value. Which you get has to be documented.
So, a pedantically conforming implementation would be
void *malloc(size_t size)
{
return NULL;
}
That's not conforming either. If malloc returns NULL, errno must be
set.
From the C99 C Standard (C90 has identical wording):
<quote>
7.20.3.3 The malloc function
Synopsis
1 #include <stdlib.h>
void *malloc(size_t size);
Description
2 The malloc function allocates space for an object whose size is
specified by size and whose value is indeterminate.
Returns
3 The malloc function returns either a null pointer or a pointer to
the allocated space.
</quote>
Nowhere does it say that errno must be set if malloc returns NULL.
Where did you get that idea?
7.5 Errors said:The value of errno is zero at program startup, but is never set to zero by any library
function.176) The value of errno may be set to nonzero by a library function call
whether or not there is an error, provided the use of errno is not documented in the
description of the function in this International Standard.
Thus, a program that uses errno for error checking should set it to zero before a library function call,
then inspect it before a subsequent library function call. Of course, a library function can save the
value of errno on entry and then set it to zero, as long as the original value is restored if errno’s
value is still zero just before the return.
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.