Why doesn't this print "Hi Charles"?

E

Eric Lilja

As the title, says: Why doesn't the following program print Hi
Charles<newline> when run?
#include <stdarg.h>
#include <stdio.h>

static void va_arg_example(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(format, args);

va_end(args);
}

int
main(void)
{
va_arg_example("Hi %s\n", "charles");

return 0;
}


The output is simply Hi<newline>

I tried running it under my debugger: Here's the contents of the variables
format and args when the printf-statement is reached:
(gdb) print args
$1 = 0x22ef64 ""
(gdb) print format
$2 = 0x403008 "Hi %s\n"

How do I fix it?

I encountered this problem in a much larger program involving a third-party
gui library but I hope I have boiled the problem to a short program using
only standard C constructs.

Thanks for reading and replying.

/ Eric
 
E

Eric Lilja

Richard Tobin said:
Because printf takes several arguments, not a single va_list object.
Try vprintf.

-- Richard

Thank you Richard for that prompt reply. This solves my problem with the
real program as well. Cheers!

/ Eric
 
E

Eric Sosman

Eric said:
As the title, says: Why doesn't the following program print Hi
Charles<newline> when run?
#include <stdarg.h>
#include <stdio.h>

static void va_arg_example(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(format, args);

Bzzzt! You want vprintf() here, not printf(). See
Question 15.5 in the comp.lang.c Frequently Asked Questions
(FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
B

Ben Pfaff

Eric Lilja said:
va_list args;
va_start(args, format);
printf(format, args);

printf() does not take a va_list as argument. You may be looking
for vprintf().
 
M

Martin Ambuhl

Eric said:
As the title, says: Why doesn't the following program print Hi
Charles<newline> when run?

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

static void va_arg_example(const char *format, ...)
{
va_list args;
va_start(args, format);
printf(format, va_arg(args, char *)); /* NOTE change */

va_end(args);
}

int main(void)
{
va_arg_example("Hi %s\n", "charles");
return 0;
}
 
P

Peteris Krumins

No matter how correct your va_arg_example() function is the
program will always print "Hi charles" with a lowecase 'c' unless of
course
your va_arg_example() function does explicity capitalize the first
letter.
 
K

Keith Thompson

Peteris Krumins said:
No matter how correct your va_arg_example() function is the
program will always print "Hi charles" with a lowecase 'c' unless of
course
your va_arg_example() function does explicity capitalize the first
letter.

Please provide some context. To quote CBFalconer's signature:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

You're right about the upper case vs. lower case 'c', but that's not
the point (presumably it was just a typo). The point (which was
mentioned in a followup several days ago) is that printf() doesn't
take a va_list argument (vprintf() does).
 
P

Peteris Krumins

Keith said:
Please provide some context. To quote CBFalconer's signature:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Thanks, I did not know that. And it was an overkill to do '>' quotation
manually.
You're right about the upper case vs. lower case 'c', but that's not
the point (presumably it was just a typo). The point (which was
mentioned in a followup several days ago) is that printf() doesn't
take a va_list argument (vprintf() does).

I noticed that but since it was already mentioned I didnt write it my
reply.
I also assumed it was not a typo.


P.Krumins
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top