By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

re-Write printf()

P: n/a
This is an interview question from: http://www.freshersworld.com/intervi...nterview_C.htm

Can You write a function similar to printf() ?

I can only think using putchar() and casting for such a thing.
Jan 13 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
riva wrote:
This is an interview question from: http://www.freshersworld.com/intervi...nterview_C.htm

Can You write a function similar to printf() ?
Yes.
I can only think using putchar() and casting for such a thing.
I think you're thinking along the wrong lines. Suggested
reading: "The Standard C Library" by P.J. Plauger.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 13 '08 #2

P: n/a
On Jan 13, 2:26*pm, riva <ra.ravi....@gmail.comwrote:
This is an interview question from:http://www.freshersworld.com/intervi...nterview_C.htm

Can You write a function similar to printf() ?

I can only think using putchar() and casting for such a thing.
you need to handle variable number of arguments (the ... in printf())

you need to parse the format thingies (%d, %s etc.)

then take the appropriate action
--
Nick Keighley

Jan 13 '08 #3

P: n/a

"riva" <ra*********@gmail.comwrote in message
This is an interview question from:
http://www.freshersworld.com/intervi...nterview_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.

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

Jan 13 '08 #4

P: n/a
On Jan 13, 5:24 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"riva" <ra.ravi....@gmail.comwrote in message
This is an interview question from:
http://www.freshersworld.com/intervi...nterview_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().
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'.
Jan 13 '08 #5

P: n/a
On Jan 13, 4:26 pm, riva <ra.ravi....@gmail.comwrote:
This is an interview question from:http://www.freshersworld.com/intervi...nterview_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.
Jan 13 '08 #6

P: n/a

<vi******@gmail.comwrote in message
On Jan 13, 5:24 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
>"riva" <ra.ravi....@gmail.comwrote in message
This is an interview question from:
http://www.freshersworld.com/intervi...nterview_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().

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.

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

Jan 13 '08 #7

P: n/a
riva wrote:
This is an interview question from:
http://www.freshersworld.com/intervi...nterview_C.htm

Can You write a function similar to printf() ?
I can and I have -- for suitable values of "similar to".
I can only think using putchar() and casting for such a thing.
It's very like that you'll need both of those, but they won't
suffice on their lonesome ownsome.

--
Playing Oberon Hedgehog
Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/

Jan 13 '08 #8

P: n/a
riva wrote:
>
This is an interview question from:
http://www.freshersworld.com/intervi...nterview_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 */

--
pete
Jan 14 '08 #9

P: n/a
riva wrote:
This is an interview question from: http://www.freshersworld.com/intervi...nterview_C.htm

Can You write a function similar to printf() ?
I like interview questions which require one-word answers ...
Jan 14 '08 #10

P: n/a
vi******@gmail.com wrote:
>
On Jan 13, 4:26 pm, riva <ra.ravi....@gmail.comwrote:
This is an interview question from:http://www.freshersworld.com/intervi...nterview_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.
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/

--
pete
Jan 15 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.