Connecting Tech Pros Worldwide Forums | Help | Site Map

Why doesn't this print "Hi Charles"?

Eric Lilja
Guest
 
Posts: n/a
#1: Nov 14 '05
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



Richard Tobin
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


In article <ctp0pd$cvi$1@news.island.liu.se>,
Eric Lilja <ericliljaNoSpam@yahoo.com> wrote:[color=blue]
>As the title, says: Why doesn't the following program print Hi[/color]
[color=blue]
> va_start(args, format);
> printf(format, args);[/color]

Because printf takes several arguments, not a single va_list object.
Try vprintf.

-- Richard
Eric Lilja
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Why doesn't this print "Hi Charles"?



"Richard Tobin" wrote:[color=blue]
> In article <ctp0pd$cvi$1@news.island.liu.se>,
> Eric Lilja <ericliljaNoSpam@yahoo.com> wrote:[color=green]
>>As the title, says: Why doesn't the following program print Hi[/color]
>[color=green]
>> va_start(args, format);
>> printf(format, args);[/color]
>
> Because printf takes several arguments, not a single va_list object.
> Try vprintf.
>
> -- Richard[/color]

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

/ Eric


Eric Sosman
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Why doesn't this print "Hi Charles"?




Eric Lilja wrote:[color=blue]
> 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);[/color]

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
[color=blue]
> va_end(args);
> }
>
> int
> main(void)
> {
> va_arg_example("Hi %s\n", "charles");
>
> return 0;
> }[/color]

--
Eric.Sosman@sun.com

Martin Ambuhl
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


Eric Lilja wrote:[color=blue]
> As the title, says: Why doesn't the following program print Hi
> Charles<newline> when run?[/color]

#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;
}





--
"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
Ben Pfaff
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


"Eric Lilja" <ericliljaNoSpam@yahoo.com> writes:
[color=blue]
> va_list args;
> va_start(args, format);
> printf(format, args);[/color]

printf() does not take a va_list as argument. You may be looking
for vprintf().
--
Go not to Usenet for counsel, for they will say both no and yes.
Peteris Krumins
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


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.

Keith Thompson
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


"Peteris Krumins" <peteris.krumins@gmail.com> writes:[color=blue]
> 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.[/color]

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).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Peteris Krumins
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Why doesn't this print "Hi Charles"?


Keith Thompson wrote:[color=blue]
>
> 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
>[/color]

Thanks, I did not know that. And it was an overkill to do '>' quotation
manually.
[color=blue]
> 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).
>[/color]

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

Closed Thread