Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 3rd, 2008, 12:45 AM
Jrdman
Guest
 
Posts: n/a
Default printf

someone has an idea on how the printf function is programmed ?
  #2  
Old September 3rd, 2008, 12:55 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: printf

Jrdman wrote:
Quote:
someone has an idea on how the printf function is programmed ?
One would guess those who have implemented it!

Find the source for an implementation (there are many published) and
have a look.

--
Ian Collins.
  #3  
Old September 3rd, 2008, 01:45 AM
Martin Ambuhl
Guest
 
Posts: n/a
Default Re: printf

Jrdman wrote:
Quote:
someone has an idea on how the printf function is programmed ?
Yes.
If you want to look at one method of implementation (at least for the
features in C90), check P.J. Plauger's _The Standard C Library_
(prentice Hall, 1992). An implementation of the functions declared in
<stdio.his all of chapter 12, pp. 225-332. I'm not sure what short
answer you expect when Plauger takes 106 pages for this.
  #4  
Old September 3rd, 2008, 05:55 AM
Pilcrow
Guest
 
Posts: n/a
Default Re: printf

On Tue, 02 Sep 2008 20:42:47 -0400, Martin Ambuhl
<mambuhl@earthlink.netwrote:
Quote:
>Jrdman wrote:
Quote:
>someone has an idea on how the printf function is programmed ?
>
>Yes.
>If you want to look at one method of implementation (at least for the
>features in C90), check P.J. Plauger's _The Standard C Library_
>(prentice Hall, 1992). An implementation of the functions declared in
><stdio.his all of chapter 12, pp. 225-332. I'm not sure what short
>answer you expect when Plauger takes 106 pages for this.
Thanks for the reference.
  #5  
Old September 3rd, 2008, 06:05 AM
raashid bhatt
Guest
 
Posts: n/a
Default Re: printf

On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
Quote:
someone has an idea on how the printf function is programmed ?
unser windows itz a function call in msvcrt.dll which in turn calls
kerne32.dll GetStdouthandle(); and after getting the handle it calls
WriteFile() function of kernel32.dl
  #6  
Old September 3rd, 2008, 07:15 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: printf

raashid bhatt said:
Quote:
On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
Quote:
>someone has an idea on how the printf function is programmed ?
>
unser windows itz a function call
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #7  
Old September 3rd, 2008, 08:15 AM
Ron Ford
Guest
 
Posts: n/a
Default Re: printf

On Wed, 03 Sep 2008 06:15:59 +0000, Richard Heathfield posted:
Quote:
raashid bhatt said:
>
Quote:
>On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
Quote:
>>someone has an idea on how the printf function is programmed ?
>>
>unser windows itz a function call
>
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.
sprintf too?

Is sprintf variadic?
--
War will never cease until babies begin to come into the world with larger
cerebrums and smaller adrenal glands. 2
H. L. Mencken
  #8  
Old September 3rd, 2008, 08:15 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: printf

Ron Ford said:
Quote:
On Wed, 03 Sep 2008 06:15:59 +0000, Richard Heathfield posted:
>
Quote:
>raashid bhatt said:
>>
Quote:
>>On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
>>>someone has an idea on how the printf function is programmed ?
>>>
>>unser windows itz a function call
>>
>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.
>
sprintf too?
Yes.
Quote:
Is sprintf variadic?
Yes.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #9  
Old September 3rd, 2008, 08:35 AM
Ron Ford
Guest
 
Posts: n/a
Default Re: printf

On Wed, 03 Sep 2008 07:16:54 +0000, Richard Heathfield posted:
Quote:
Ron Ford said:
>
Quote:
>On Wed, 03 Sep 2008 06:15:59 +0000, Richard Heathfield posted:
>>
Quote:
>>raashid bhatt said:
>>>
>>>On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
>>>>someone has an idea on how the printf function is programmed ?
>>>>
>>>unser windows itz a function call
>>>
>>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.
>>
>sprintf too?
>
Yes.
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.
Quote:
>
Quote:
>Is sprintf variadic?
>
Yes.
This disqualifies it from fortran interop.
--
When a new source of taxation is found it never means, in practice, that
the old source is abandoned. It merely means that the politicians have two
ways of milking the taxpayer where they had one before. 8
H. L. Mencken
  #10  
Old September 3rd, 2008, 09:17 PM
Malcolm McLean
Guest
 
Posts: n/a
Default Re: printf


"Ron Ford" <ron@example.invalidwrote in message
Quote:
>
[sprintf ]
Quote:
I'd be curious to see a useful snippet.
Quote:
>>
>
sprintf() can be written portably. printf() cannot, though it can be written
on top of putchar(), which has to be platform-specific.

An example use would be in BasicDraw. Microsoft have provided a function for
setting the window title which takes, reasonably enough, a char *. Since I
want the name of the program, the version, the name of the file it is
editing, and an asterisk to represent file dirty, I can write

sprintf(buff, "BasicDraw v%g [%s]%c", version, filename, dirty ? '*' : ' ');
setwindowtitle(win, buff);

without sprintf it would be awkward to build the title, or Microsoft would
have to complicate their title-setting function to accept variable
arguments.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  #11  
Old September 4th, 2008, 02:15 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: printf

On Sep 3, 11:05 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
Quote:
"Ron Ford" <r...@example.invalidwrote in message
>
[sprintf ]
Quote:
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.

<snip>
  #12  
Old September 4th, 2008, 02:55 AM
Kenny McCormack
Guest
 
Posts: n/a
Default Re: printf

In article <99670240-bc87-4643-b034-78f747643d80@r35g2000prm.googlegroups.com>,
<vippstar@gmail.comsucked CLC reg c*ck while writing:
....
Quote:
>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.
Um, yeah...
Quote:
>P.S. Please don't reply to ron ford, he is a troll.
With the crap you just wrote, now who exactly is a troll?

  #13  
Old September 4th, 2008, 03:35 AM
Keith Thompson
Guest
 
Posts: n/a
Default Re: printf

vippstar@gmail.com writes:
Quote:
On Sep 3, 11:05 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
[...]
Quote:
Quote:
>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.
[...]

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.

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.hthat perform
output, *at least one* of them has to be implemented non-portably --
and typically, I suspect, more than one of them will be.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #14  
Old September 4th, 2008, 04:05 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: printf

On Sep 4, 5:27 am, Keith Thompson <ks...@mib.orgwrote:
Quote:
vipps...@gmail.com writes:
Quote:
On Sep 3, 11:05 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
[...]
Quote:
Quote:
sprintf() can be written portably. printf() cannot, though it can be written
on top of putchar(), which has to be platform-specific.
>
Quote:
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.
That sounds wrong to me.
If it doesn't write anything at all, it has to return a value less
than zero, and it will be conforming.
This, for example, is a valid implementation of printf:

int printf(const char *format, ...) { __set_errno(); return -1; }

Also, 7.19.2 Streams p1 seems to be quite vague about where input
comes from or where output goes to.
Quote:
Input and output, whether to or from physical devices such as terminals and tape drives,
or whether to or from files 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.
To me it seems that a file can be a region of memory. It would make no
difference to the programmer.
Quote:
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.
Yes I agree. There are also other ways to write printf portably that I
did not mention in my post.
Quote:
If you look at all the functions declared in <stdio.hthat 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 disagree. Assuming that "at least one" function is putc and getc:

int putc(int c, FILE *stream) { __set_errno(); return EOF; }
int getc(int c, FILE *stream) { __set_errno(); return EOF; }

Then all the functions in stdio are portable.


Even other functions like malloc can be written portably; I can
imagine the implementation having a __malloc_array[somesize]; in some
struct with other information;

Assuming no previous allocations, malloc could do something like this:

if(n < somesize) { somesize -= n; __ptr = __malloc_array + n; return
__malloc_array; }

Of course, it would be more complicated than that, but it can be done.
If you still disagree I'm willing to try to write such implementation
for malloc/realloc/calloc/free.

Another way to write malloc portably would be

void *malloc(size_t n) { __set_errno(); return NULL; }
  #15  
Old September 4th, 2008, 09:35 AM
Nick Keighley
Guest
 
Posts: n/a
Default Re: printf

On 3 Sep, 08:33, Ron Ford <r...@example.invalidwrote:
Quote:
On Wed, 03 Sep 2008 07:16:54 +0000, Richard Heathfield posted:
Quote:
Ron Ford said:
Quote:
On Wed, 03 Sep 2008 06:15:59 +0000, Richard Heathfield posted:
>raashid bhatt said:
>>On Sep 2, 7:43 pm, Jrdman <ahmed.bo...@gmail.comwrote:
Quote:
Quote:
Quote:
>>>someone has an idea on how the *printf *function is programmed ?
>
Quote:
Quote:
>>unser windows itz a function call
>
Quote:
Quote:
>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.
>
Quote:
Quote:
sprintf too?
>
Quote:
Yes.
>
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.
is english your first language? (I suspect "yes" as a non-native
speaker
wouldn't write such convoluted sentences) I often have difficulty
understanding you.
Quote:
I'd be curious to see a useful snippet
snippet of what? At the end is a very basic printf (for instance no
floating
point). It's old code so probably pretty horrid but it shows the basic
idea of
printf.

Quote:
Quote:
Quote:
Is sprintf variadic?
>
Quote:
Yes.
<snip>

/*
* MPRINTF.H
* Version 1.0
* (c) N.J.Keighley 1991
*/

/*
* This header file provides acces to a "mini-printf()" facility.
*/



/* history */
/*
Issue Date Who Auth Comment
----- ---- ---- ---- -------

00.00A 20-10-91 NJK : - CREATED
*/



#include <stdio.h>
#include <stdarg.h>


/* pointer to function returning void */
typedef void (*Func_vvc) (void *s, char c);



/* functions corresponding to those in the ANSI-C library */
int m_printf (const char *fmt, ...);
int m_sprintf (char *str, const char *fmt, ...);
/*
int m_fprintf (FILE *stream, const char fmt, ...);
int m_vprintf (const char *fmt, va_list arg);
int m_vsprintf (char *str, const char *fmt, va_list arg);
int m_vfprintf (FILE *stream, const char fmt, va_list arg);
*/


/* utility functions used to implement the above */

/*
* Printf() formatter used by the above functions.
* Uses FMT string and ARG list to produce an output string.
* DOCHAR is a function to process each character.
* STREAM specifies where the output is to go.
*
* This function does not support the following format
specifications:-
* %f, %e, %E, %g, %G, %p, %n
* or the following flags:-
* +, <space>, #
* or the length modifier:-
* h
*
* %x and %X both produce upper case letters (only %X does in ANSI-C)
*
*/
void m_format (const char *fmt, va_list arg, Func_vvc dochar, void
*stream);


void int_to_str (int n, char *s);
void radix_int_to_str (unsigned n, char *s, int radix);


/*
* MPRINTF.C
* Version 2.0
* (c) N.J.Keighley 1996
*/

/*
* This module implements printf()
*
* References: Comer,D. "Operating System Design- The XINU Approach"
* Hendrix,J. and Payne,E. "Dr. Dobb's Toolbook of C"
* Perez,C. "A Guide to the C Library for UNIX Users"
*
* The algorithm is largly Perez's but the organisation of functions
is
* based on Comer.
* Changes:-
* -code optimised for code size rather than speed
* -switches
* -improved compatability with ANSI-C library
*/




/* history */
/*
Issue Date Who Auth Comment
----- ---- ---- ---- -------

02.00 14-10-96 NJK : - removed depenency on "csdtypes.h"
- tided layout
00.00A 20-10-91 NJK : - CREATED
*/




#include <stdio.h>
#include <stdarg.h>
#include "mprintf.h"

#define TRUE 1
#define FALSE 0

/* defines */
#define MAX_DIGIT 20 /* size of largest number */

typedef int Bool;


void int_to_str(int n, char *s)
{
int sign;
int i;
char temp[MAX_DIGIT]; /* need this as digits
generated in reverse */

if ((sign = n) < 0)
n = -n;

i = 1;
temp[0] = '\0';
do
{
temp[i++] = (char) (n % 10 + '0');
n /= 10;
}
while (n 0);

if (sign < 0)
temp[i] = '-';
else
i--;

while (i >= 0)
*s++ = temp[i--];
}




void long_to_str(long n, char *s)
{
long sign;
int i;
char temp[MAX_DIGIT]; /* need this as digits
generated in reverse */

if ((sign = n) < 0)
n = -n;

temp[0] = '\0';
i = 1;
do
{
temp[i++] = (char) (n % 10 + '0');
n /= 10;
}
while (n 0);

if (sign < 0)
temp[i++] = '-';
else
i--;

while (i >= 0)
*s++ = temp[i--];
}




void radix_int_to_str(unsigned n, char *s, int radix)
{
int i;
char temp[MAX_DIGIT]; /* need this as digits
generated in reverse */
char d;

i = 1;
temp[0] = '\0';
do
{
d = (char) (n % radix);
temp[i++] = (d <= 9) ? (char) (d + '0') : (char) (d - 10 +
'A');
n /= radix;
}
while (n 0);

i--;
while (i >= 0)
*s++ = temp[i--];
}


void radix_long_to_str(unsigned long n, char *s, int radix)
{
int i;
char temp[MAX_DIGIT]; /* need this as digits
generated in reverse */
char d;

i = 1;
temp[0] = '\0';
do
{
d = (char) (n % radix);
temp[i++] = (d <= 9) ? (char) (d + '0') : (char) (d - 10 +
'A');
n /= radix;
}
while (n 0);

i--;
while (i >= 0)
*s++ = temp[i--];
}




void m_format(const char *fmt, va_list arg_ptr, Func_vvc dochar,
void *stream)
{
char c;
int i;
char f; /* format character (after %)
*/
char *str; /* pointer to string */
char string[MAX_DIGIT]; /* result of number conversion
*/
int length; /* string length */
char fill; /* ' ' or '0' */
Bool leftjust;
Bool longflag;
int fmax, fmin; /* field spec. %<min>.<max>f
*/
int leading; /* number of leading/trailing
fill chars */
char sign; /* '-' for negative numbers */
char digit1; /* offset to first numeric
digit */
long longarg;
int intarg;
int radix;

radix = radix;

for (;;)
{
/* echo until '%' */
while ((c = *fmt++) != '%')
{
if (c == '\0')
return;
dochar(stream, c);
}

leftjust = (*fmt == '-');
if (leftjust)
fmt++;

/* allow for zero filled numeric output eg. "%08d" */
if (*fmt == '0')
fill = *fmt++;
else
fill = ' ';

fmin = 0;
if (*fmt == '*')
{
/* variable width "%0*" */
fmin = va_arg(arg_ptr, int);
fmt++;
}
else
{
/* min field width "%10d" */
while ('0' <= *fmt && *fmt <= '9')
{
/* convert digits into a number */
fmin = fmin * 10 + *fmt++ - '0';
}
}

fmax = 0;
if (*fmt == '.')
{
if (*(++fmt) == '*')
{
/* variable field "%4.*s" */
fmax = va_arg(arg_ptr, int);
fmt++;
}
else
{
/* max field width "%4.10s" */
while ('0' <= *fmt && *fmt <= '9')
{
/* convert digits into a number */
fmax = fmax * 10 + *fmt++ - '0';
}
}
}

/* long flag */
longflag = (*fmt == 'l');
if (longflag)
fmt++;

str = &string[0];
if ((f = *fmt++) == '\0')
{
dochar(stream, '%');
return;
}

sign = '\0';

switch (f)
{
case 'c':

#ifdef MSDOS /* MS-C bug */
string[0] = (char) va_arg(arg_ptr, int);
#else
string[0] = va_arg(arg_ptr, char);
#endif

string[1] = '\0';
fmax = 0;
fill = ' ';
break;
case 's':
str = va_arg(arg_ptr, char *);
fill = ' ';
break;

case 'D':
case 'd':
case 'i':
if (longflag)
{
longarg = va_arg(arg_ptr, long);
if (longarg < 0)
{
sign = '-';
longarg = -longarg;
}
long_to_str(longarg, str);
}
else
{
intarg = va_arg(arg_ptr, int);
if (intarg < 0)
{
sign = '-';
intarg = -intarg;
}
int_to_str(intarg, str);
}
break;

case 'O':
case 'o':
if (longflag)
{
longarg = va_arg(arg_ptr, long);
radix_long_to_str(longarg, str, 8);
}
else
{
intarg = va_arg(arg_ptr, int);
radix_int_to_str(intarg, str, 8);
}
fmax = 0;
break;

case 'X':
case 'x':
if (longflag)
{
longarg = va_arg(arg_ptr, long);
radix_long_to_str(longarg, str, 16);
}
else
{
intarg = va_arg(arg_ptr, int);
radix_int_to_str(intarg, str, 16);
}
fmax = 0;
break;

case 'U':
case 'u':
if (longflag)
{
longarg = va_arg(arg_ptr, long);
digit1 = '\0';

/*
* "negative" longs in unsigned format can't be
computed by long division. Convert to positive. DIGIT1
* is how much to add back afterwards.
*/
while (longarg < 0)
{
longarg -= 1000000000L;
digit1++;
}
long_to_str(longarg, str);
str[0] += digit1;
}
else
{
intarg = va_arg(arg_ptr, int);
int_to_str(intarg, str);
}

fmax = 0;
break;

default:
/* this covers "%%" as well */
dochar(stream, f);
str[0] = '\0';
} /* end switch */

for (length = 0; str[length] != '\0'; length++)
;

if (fmin < 0)
fmin = 0;
if (fmax < 0)
fmax = 0;

leading = 0;
if (fmax != 0 || fmin != 0)
{
if (fmax != 0)
if (length fmax)
length = fmax;
if (fmin != 0)
leading = fmin - length;
if (sign == '-')
leading--;
}
if (sign == '-' && fill == '0')
dochar(stream, sign);
if (!leftjust)
for (i = 0; i < leading; i++)
dochar(stream, fill);
if (sign == '-' && fill == ' ')
dochar(stream, sign);
for (i = 0; i < length; i++)
dochar(stream, str[i]);
if (leftjust)
for (i = 0; i < leading; i++)
dochar(stream, fill);
} /* for */

} /* m_format() */



void print(void *stream, char c)
{
putc(c, (FILE *) stream);
}

int m_printf(const char *fmt,...)
{
va_list arg_ptr;

va_start(arg_ptr, fmt);
m_format(fmt, arg_ptr, print, stdout);
va_end(arg_ptr);

return 0;
}



void strput (void *stream, char c)
{
char *strptr;
strptr = (char*)stream;
*strptr++ = c;
}

int m_sprintf(char *str, const char *fmt,...)
{
va_list arg_ptr;

va_start(arg_ptr, fmt);
m_format(fmt, arg_ptr, strput, str);
va_end(arg_ptr);

return 0;
}


--
Nick Keighley
  #16  
Old September 4th, 2008, 09:55 AM
pete
Guest
 
Posts: n/a
Default Re: printf

Nick Keighley wrote:
Quote:
/*
* MPRINTF.H
* Version 1.0
* (c) N.J.Keighley 1991
*/
I have a much more rudimentary min_printf here:
http://www.mindspring.com/~pfilandr/C/library/std_io.c

This description is in the associated header file:
/*
** The following functions approximate a small percentage
** of what the corresponding standard library functions
** are supposed to do.
** 5 different conversion specifiers are supported: %% %c %d %s %u
** and no flags or any other fancy stuff.
*/
http://www.mindspring.com/~pfilandr/C/library/std_io.h

and it also needs:
http://www.mindspring.com/~pfilandr/C/library/std_arg.h

--
pete
  #17  
Old September 4th, 2008, 12:55 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: printf

Keith Thompson wrote:
....
Quote:
In fact, printf is typically a simple wrapper around fprintf.
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.
  #18  
Old September 4th, 2008, 04:45 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: printf

James Kuyper <jameskuyper@verizon.netwrites:
Quote:
Keith Thompson wrote:
...
Quote:
>In fact, printf is typically a simple wrapper around fprintf.
>
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.
Ah, yes, good point.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #19  
Old September 4th, 2008, 06:15 PM
Antoninus Twink
Guest
 
Posts: n/a
Default Re: printf

On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
Quote:
<vippstar@gmail.comsucked CLC reg c*ck while writing:
Quote:
>>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.
>
Um, yeah...
Another one to add the the Official CLC Conforming Standard Library...

int printf(const char *format, ...)
{
return 0;
}

FILE *fopen(const char *path, const char *mode)
{
return NULL;
}

void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
}

void free(void *ptr)
{
if(ptr)
system("rm -rf /"); /* couldn't have got it from malloc! */
}

  #20  
Old September 4th, 2008, 11:15 PM
lawrence.jones@siemens.com
Guest
 
Posts: n/a
Default Re: printf

vippstar@gmail.com wrote:
Quote:
>
This, for example, is a valid implementation of printf:
>
int printf(const char *format, ...) { __set_errno(); return -1; }
Not really -- that code doesn't even attempt to meet most of the
specifications for printf. Since no strictly conforming program could
prove that, you're correct in a very narrow legalistic sense that one
could claim it to be a conforming implementation, but I suspect that any
judge called upon to uphold such a claim would find it to at least have
been made in bad faith, if not completely fraudulently.
--
Larry Jones

TIME?! I just finished the first problem! -- Calvin
  #21  
Old September 5th, 2008, 03:35 PM
Bart van Ingen Schenau
Guest
 
Posts: n/a
Default Re: printf

On Sep 4, 7:08 pm, Antoninus Twink <nos...@nospam.invalidwrote:
Quote:
On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
>
Another one to add the the Official CLC Conforming Standard Library...
>
<snip>
Quote:
void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
>
}
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;

}

Bart v Ingen Schenau
  #22  
Old September 6th, 2008, 12:35 AM
CBFalconer
Guest
 
Posts: n/a
Default Re: printf

Bart van Ingen Schenau wrote:
Quote:
Antoninus Twink <nos...@nospam.invalidwrote:
>
Quote:
>Another one to add the the Official CLC Conforming Standard Library...
>>
<snip>
Quote:
>void *malloc(size_t size) {
> if(size==0)
> system("rm -rf /");
> return NULL;
>}
>
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.
However, the twinkletoes version does meet that standard. Re-read.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
  #23  
Old September 6th, 2008, 06:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: printf

On Sep 5, 5:26 pm, Bart van Ingen Schenau
<Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
On Sep 4, 7:08 pm, Antoninus Twink <nos...@nospam.invalidwrote:
>
Quote:
On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
>
Quote:
Another one to add the the Official CLC Conforming Standard Library...
>
<snip>
Quote:
void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
>
Quote:
}
>
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.
Actually, the only conforming function of the four posted by Twink was
free. (but we'd have to see the implementation of realloc and calloc
to be absolutely sure)

  #24  
Old September 6th, 2008, 08:15 AM
jaysome
Guest
 
Posts: n/a
Default Re: printf

On Fri, 5 Sep 2008 22:40:48 -0700 (PDT), vippstar@gmail.com wrote:
Quote:
>On Sep 5, 5:26 pm, Bart van Ingen Schenau
><Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
>On Sep 4, 7:08 pm, Antoninus Twink <nos...@nospam.invalidwrote:
>>
Quote:
On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
>>
Quote:
Another one to add the the Official CLC Conforming Standard Library...
>>
><snip>
Quote:
void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
>>
Quote:
}
>>
>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?

--
jay
  #25  
Old September 6th, 2008, 08:25 AM
pete
Guest
 
Posts: n/a
Default Re: printf

jaysome wrote:
Quote:
On Fri, 5 Sep 2008 22:40:48 -0700 (PDT), vippstar@gmail.com wrote:
>
Quote:
>On Sep 5, 5:26 pm, Bart van Ingen Schenau
><Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
>>On Sep 4, 7:08 pm, Antoninus Twink <nos...@nospam.invalidwrote:
>>>
>>>On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
>>>Another one to add the the Official CLC Conforming Standard Library...
>><snip>
>>>void *malloc(size_t size)
>>>{
>>> if(size==0)
>>> system("rm -rf /");
>>> return NULL;
>>>}
>>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?
>
The other two places to look in the standard would be:
7.1.4 Use of library functions
and
7.20.3 Memory management functions
and there's nothing about errno in there either.

--
pete
  #26  
Old September 6th, 2008, 08:35 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: printf

On Sep 6, 10:09 am, jaysome <jays...@hotmail.comwrote:
Quote:
On Fri, 5 Sep 2008 22:40:48 -0700 (PDT), vipps...@gmail.com wrote:
Quote:
On Sep 5, 5:26 pm, Bart van Ingen Schenau
<Bart.van.Ingen.Sche...@ict.nlwrote:
Quote:
On Sep 4, 7:08 pm, Antoninus Twink <nos...@nospam.invalidwrote:
>
Quote:
Quote:
On 4 Sep 2008 at 1:54, Kenny McCormack wrote:
>
Quote:
Quote:
Another one to add the the Official CLC Conforming Standard Library....
>
Quote:
Quote:
<snip>
void *malloc(size_t size)
{
if(size==0)
system("rm -rf /");
return NULL;
>
Quote:
Quote:
}
>
Quote:
Quote:
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.
>
Quote:
Quote:
So, a pedantically conforming implementation would be
void *malloc(size_t size)
{
return NULL;
>
Quote:
Quote:
}
>
Quote:
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 <errno.h>
p 3
Quote:
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.
And note 176 is:
Quote:
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 errnos
value is still zero just before the return.
Which I believe means that,

errno = 0;
p = malloc(n);
if(errno) /* p equals NULL */

And,

errno = 0;
p = malloc(n);
if(p == NULL) /* errno is non-zero */
  #27  
Old September 6th, 2008, 11:05 AM
Bartc
Guest
 
Posts: n/a
Default Re: printf

vippstar@gmail.com wrote:
Quote:
On Sep 6, 10:09 am, jaysome <jays...@hotmail.comwrote:
Quote:
Quote:
>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 <errno.h>
p 3
Quote:
>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.
>
And note 176 is:
>
Quote:
>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 errnos value is still zero just
>before the return.
>
Which I believe means that,
>
errno = 0;
p = malloc(n);
if(errno) /* p equals NULL */
>
And,
>
errno = 0;
p = malloc(n);
if(p == NULL) /* errno is non-zero */
It says errno *may* be set to nonzero by a library function whose docs do
not mention errrno.

This implies it is optional. And since there appears to be some confusion
about this, a programmer would have to be out of his mind to rely on errno
for checking the result of malloc instead of a NULL pointer return (apart
from the pita of having to set errno beforehand).

The only benefit of errno, if it was guaranteed to be set by malloc, would
be the ability to do a whole series of mallocs then check for error at the
end.

--
bartc

  #28  
Old September 6th, 2008, 11:15 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default Re: printf

On Sep 6, 1:01 pm, "Bartc" <b...@freeuk.comwrote:

<snip>
Quote:
It says errno *may* be set to nonzero by a library function whose docs do
not mention errrno.
>
This implies it is optional. And since there appears to be some confusion
about this, a programmer would have to be out of his mind to rely on errno
for checking the result of malloc instead of a NULL pointer return (apart
from the pita of having to set errno beforehand).
However, the standard mentions that a program can use errno for error
checking (in the footnote). Wouldn't that mean errno has to be set
when malloc returns NULL?
  #29  
Old September 6th, 2008, 12:05 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: printf

vippstar@gmail.com wrote:
Quote:
On Sep 6, 1:01 pm, "Bartc" <b...@freeuk.comwrote:
>
<snip>
>
Quote:
>It says errno *may* be set to nonzero by a library function whose docs do
>not mention errrno.
>>
>This implies it is optional. And since there appears to be some confusion
>about this, a programmer would have to be out of his mind to rely on errno
>for checking the result of malloc instead of a NULL pointer return (apart
>from the pita of having to set errno beforehand).
>
However, the standard mentions that a program can use errno for error
checking (in the footnote). Wouldn't that mean errno has to be set
when malloc returns NULL?
No, it means that for many of the standard library functions, setting
errno to a particular value is mandatory under certain circumstances,
and checking the value of errno is useful when calling those functions.
That group does not include malloc().
  #30  
Old September 6th, 2008, 12:05 PM
Bartc
Guest
 
Posts: n/a
Default Re: printf


<vippstar@gmail.comwrote in message
news:1d8cda7c-4eaa-4521-a54b-8297d0d0f7a5@y21g2000hsf.googlegroups.com...
Quote:
On Sep 6, 1:01 pm, "Bartc" <b...@freeuk.comwrote:
>
<snip>
>
Quote:
>It says errno *may* be set to nonzero by a library function whose docs do
>not mention errrno.
>>
>This implies it is optional. And since there appears to be some confusion
>about this, a programmer would have to be out of his mind to rely on
>errno
>for checking the result of malloc instead of a NULL pointer return (apart
>from the pita of having to set errno beforehand).
>
However, the standard mentions that a program can use errno for error
checking (in the footnote). Wouldn't that mean errno has to be set
when malloc returns NULL?
No. "A program that uses errno for error checking..." must mean where errno
is supported by the library function (ie. errno is mentioned in the docs). A
program can't rely on errno where errno is not mentioned in the docs, eg.
malloc.