473,473 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why doesn't this print "Hi Charles"?

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
Nov 14 '05 #1
8 1889
In article <ct**********@news.island.liu.se>,
Eric Lilja <er*************@yahoo.com> wrote:
As the title, says: Why doesn't the following program print Hi va_start(args, format);
printf(format, args);


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

-- Richard
Nov 14 '05 #2

"Richard Tobin" wrote:
In article <ct**********@news.island.liu.se>,
Eric Lilja <er*************@yahoo.com> wrote:
As the title, says: Why doesn't the following program print Hi

va_start(args, format);
printf(format, args);


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
Nov 14 '05 #3


Eric Lilja wrote:
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
va_end(args);
}

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

return 0;
}


--
Er*********@sun.com

Nov 14 '05 #4
Eric Lilja wrote:
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;
}

--
"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
Nov 14 '05 #5
"Eric Lilja" <er*************@yahoo.com> writes:
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().
--
Go not to Usenet for counsel, for they will say both no and yes.
Nov 14 '05 #6
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.

Nov 14 '05 #7
"Peteris Krumins" <pe*************@gmail.com> writes:
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).

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 14 '05 #8
Keith Thompson wrote:

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

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matt | last post by:
I was wondering if someone could explain why I'm receiving this error. It's like a 404 error but I think it might be something else. Basically, I have a page where a user inputs his email...
9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
5
by: BJörn Lindqvist | last post by:
I think it would be cool if you could refer to instance variables without prefixing with "self." I know noone else thinks like me so Python will never be changed, but maybe you can already do it...
0
by: VaMailArmor | last post by:
* * * * * * * * * * * * * * * Vexira ALERT * * * * * * * * * * * * * * * This version of Vexira MailArmor is licensed and full featured. Vexira has detected the following in a mail from your...
2
by: ae | last post by:
what is the signficance of the @?
3
by: George Hester | last post by:
Nothing. The applet does what it is supposed to do but onload never fires. Any suggestions what may be the problem here? Thanks. -- George Hester __________________________________
0
by: cc | last post by:
The message contains Unicode characters and has been sent as a binary attachment.
6
by: Bart van Deenen | last post by:
Hi All I'm happily creating an Ajaxified web-app. I use Prototype for encapsulating xml http requests, with method "post". On the backend, I use PHP, and the replies are eval'd by Javascript. I...
1
by: sidd4one | last post by:
Hello, Hi experts when iam pinging from one of my system in the network. Its show iam "Unable to contact to ip adddress".This is message iam ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.