re-Write printf()

M

Malcolm McLean

riva said:
This is an interview question from:
http://www.freshersworld.com/interview/technical_interview_C.htm

Can You write a function similar to printf() ?

I can only think using putchar() and casting for such a thing.
Obviously you need some way of outputting characters to the screen. This
could be an 8x8 bitmap to a memory-mapped device, or it could be built on
top of a function like putchar().

The rules for formatting are quite intricate and certainly non-trivial to
implement. You can implement a good enough printf() replacement that accepts
%d %c and %s modifiers easily enough. Converting floating point to
human-readable characters is a bit more difficult, and realistically you
have to support both width and precision for it to be of any real use, since
people don't like to read long strings of non-significant characters.
Extending your "good enough" printf() to a fully conforming one is quite a
job, but bread and butter type work.
 
V

vippstar

Obviously you need some way of outputting characters to the screen. This
could be an 8x8 bitmap to a memory-mapped device, or it could be built on
top of a function like putchar().
Then it would not be a printf().
printf() writes to the file stream 'stdout'
printf() does not guarantee that it will output *anything at all* to
the 'screen'.
 
V

vippstar

This is an interview question from:http://www.freshersworld.com/interview/technical_interview_C.htm

Can You write a function similar to printf() ?

I can only think using putchar() and casting for such a thing.
printf is usually written as a macro or a warper function to vfprintf
-- snip.c --
#include <stdarg.h>

int printf(const char * restrict fmt, ...) {
va_list list;
int i;
va_start(list, fmt);
i = vfprintf(stdout, fmt, list);
va_end(list);
return i;
}
-- snip.c --
That shall do it.
 
M

Malcolm McLean

Then it would not be a printf().
printf() writes to the file stream 'stdout'
printf() does not guarantee that it will output *anything at all* to
the 'screen'.
You're asking whether it is possible to write a wholly generic, portable
printf(). The answer is yes, on top of a function that writes a single
character to stdout.
However the questioner may or may not be looking for some understnading
about how to implement the stdout stream, or something similar.
 
P

pete

riva said:
This is an interview question from:
http://www.freshersworld.com/interview/technical_interview_C.htm

Can You write a function similar to printf() ?

I can only think using putchar() and casting for such a thing.

/* BEGIN min_printf.c */
/*
** min_printf uses the following 6 features from stdio.h:
** 1 EOF
** 2 FILE
** 3 stdout
** 4 putc
** 5 feof
** 6 ferror
**
** min_printf uses the following 4 features from stdarg.h:
** 1 va_list
** 2 va_start
** 3 va_end
** 4 va_arg
*/
#include <stdio.h>
#include <stdarg.h>

#define put_c(c, stream) putc((c), (stream))
#define put_char(c) put_c((c), stdout)

#define sput_c(c, stream) \
(put_c((c), (stream)) == EOF \
&& (feof(stream) || ferror(stream)) ? EOF : 1)
#define sfput_char(c) (sput_c((c), stdout))

int (put_c)(int c, FILE *stream);
int (put_char)(int c);
/*
** 5 different conversion specifiers
** are supported by min_printf: %% %c %d %s %u
** and no fancy stuff.
*/
int min_printf(const char *format, ...);

static int sfput_d(int d);
static int sfput_s(const char *s);
static int sfput_u(unsigned u);
static int sfput_u_plus_1(unsigned u);

int (put_c)(int c, FILE *stream)
{
return put_c(c, stream);
}

int (put_char)(int c)
{
return put_char(c);
}

int min_printf(const char *format, ...)
{
int count, increment;
va_list ap;

va_start(ap, format);
for (count = 0; *format != '\0'; ++format) {
if (*format == '%') {
switch (*++format) {
case '%':
increment = sfput_char('%');
break;
case 'c':
increment = sfput_char(va_arg(ap, int));
break;
case 'd':
increment = sfput_d(va_arg(ap, int));
break;
case 's':
increment = sfput_s(va_arg(ap, char *));
break;
case 'u':
increment = sfput_u(va_arg(ap, unsigned));
break;
default:
increment = sfput_char(*format);
break;
}
} else {
increment = sfput_char(*format);
}
if (increment != EOF) {
count += increment;
} else {
count = -42;
break;
}
}
va_end(ap);
return count;
}

static int sfput_s(const char *s)
{
int count;

for (count = 0; *s != '\0'; ++s) {
if (sfput_char(*s) == EOF) {
count = EOF;
break;
}
++count;
}
return count;
}

static int sfput_d(int d)
{
int count;

if (0 > d) {
count = put_char('-');
if (count != EOF) {
count = sfput_u_plus_1(-(d + 1));
if (count != EOF) {
++count;
}
}
} else {
count = sfput_u(d);
}
return count;
}

static int sfput_u(unsigned u)
{
int count;
unsigned digit, tenth;

tenth = u / 10;
digit = u - 10 * tenth + '0';
count = tenth != 0 ? sfput_u(tenth) : 0;
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

static int sfput_u_plus_1(unsigned u)
{
int count;
unsigned digit, tenth;

tenth = u / 10;
digit = u - 10 * tenth + '0';
if (digit == '9') {
if (tenth != 0) {
count = sfput_u_plus_1(tenth);
} else {
count = put_char('1') == EOF ? EOF : 1;
}
digit = '0';
} else {
count = tenth != 0 ? sfput_u(tenth) : 0;
++digit;
}
return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
}

/* END min_printf.c */
 
P

pete

printf is usually written as a macro or a warper function to vfprintf
-- snip.c --
#include <stdarg.h>

int printf(const char * restrict fmt, ...) {
va_list list;
int i;
va_start(list, fmt);
i = vfprintf(stdout, fmt, list);
va_end(list);
return i;
}
-- snip.c --
That shall do it.

That's an interesting idea.
So, I just rewrote min_printf that way in my toy library:

http://www.mindspring.com/~pfilandr/C/library/std_io.h
http://www.mindspring.com/~pfilandr/C/library/std_io.c
http://www.mindspring.com/~pfilandr/C/library/
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top