473,372 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

re-Write printf()

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
10 5289
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
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

"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
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
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

<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.