Discussion:
printing % dont work
(too old to reply)
fir
2017-04-01 18:45:31 UTC
Permalink
Raw Message
sorry im to tired at this moment to do full test
suite, but what i can say from memory

i use some kind of vararg function (in fact i may paste it)

void mlog(char *format, ...)
{
va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);

mlog_top++;
}

this is just used to do some on-screen (hud) logging
of messages, (mlog[][] is just an ram where i store communicates) the function is correct but i encountered
weird problem

it seem to show for all string characters except the %
which sign is not printed (i also used %% also no effect)
it just shows nothing so mlog("aaa%%b") just result in aaab

also mlog("aaa%cb", 0x25); when 0x25 is an ascii for % also
do not work

after a 20 minutes of confusion at least i found
mlog("aaa%sb", "%%") do work, but still its confusing
to me

is that expected behaviour? why is that so
(sorry i write partially form memory but to tired to do fulltestcase now)
Lew Pitcher
2017-04-01 19:03:35 UTC
Permalink
Raw Message
Post by fir
sorry im to tired at this moment to do full test
suite, but what i can say from memory
i use some kind of vararg function (in fact i may paste it)
void mlog(char *format, ...)
{
va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);
mlog_top++;
}
this is just used to do some on-screen (hud) logging
of messages, (mlog[][] is just an ram where i store communicates) the
function is correct but i encountered weird problem
it seem to show for all string characters except the %
which sign is not printed (i also used %% also no effect)
it just shows nothing so mlog("aaa%%b") just result in aaab
also mlog("aaa%cb", 0x25); when 0x25 is an ascii for % also
do not work
after a 20 minutes of confusion at least i found
mlog("aaa%sb", "%%") do work, but still its confusing
to me
is that expected behaviour?
Essentially, yes. This is expected behaviour
Post by fir
why is that so
(sorry i write partially form memory but to tired to do fulltestcase now)
Your code passes the percent symbol in what becomes the vsprintf() /format/
string. If you reread the requirements for the vsprintf format string, you
will find that, like in all the formatted output ("printf') functions, the
percent ('%') character is reserved as an escape character to introduce a
conversion specification. If, /in the format string/, you want your output
to contain a single percent sign, then you have to use the "%%" conversion
specification.

7.21.6.13 The vsprintf function
...
2 The vsprintf function is equivalent to sprintf, with the variable
argument list replaced by arg, which shall have been initialized by the
va_start macro (and possibly subsequent va_arg calls).

7.21.6.6 The sprintf function
..
2 The sprintf function is equivalent to fprintf, except that the output is
written into an array (specified by the argument s) rather than to a
stream.

7.21.6.1 The fprintf function
...
4 Each conversion specification is introduced by the character %. After
the %, the following appear in sequence:
...
% A % character is written. No argument is converted. The complete
conversion specification shall be %%.
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
fir
2017-04-01 19:20:33 UTC
Permalink
Raw Message
Post by Lew Pitcher
Post by fir
sorry im to tired at this moment to do full test
suite, but what i can say from memory
i use some kind of vararg function (in fact i may paste it)
void mlog(char *format, ...)
{
va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);
mlog_top++;
}
this is just used to do some on-screen (hud) logging
of messages, (mlog[][] is just an ram where i store communicates) the
function is correct but i encountered weird problem
it seem to show for all string characters except the %
which sign is not printed (i also used %% also no effect)
it just shows nothing so mlog("aaa%%b") just result in aaab
also mlog("aaa%cb", 0x25); when 0x25 is an ascii for % also
do not work
after a 20 minutes of confusion at least i found
mlog("aaa%sb", "%%") do work, but still its confusing
to me
is that expected behaviour?
Essentially, yes. This is expected behaviour
Post by fir
why is that so
(sorry i write partially form memory but to tired to do fulltestcase now)
Your code passes the percent symbol in what becomes the vsprintf() /format/
string. If you reread the requirements for the vsprintf format string, you
will find that, like in all the formatted output ("printf') functions, the
percent ('%') character is reserved as an escape character to introduce a
conversion specification. If, /in the format string/, you want your output
to contain a single percent sign, then you have to use the "%%" conversion
specification.
7.21.6.13 The vsprintf function
...
2 The vsprintf function is equivalent to sprintf, with the variable
argument list replaced by arg, which shall have been initialized by the
va_start macro (and possibly subsequent va_arg calls).
7.21.6.6 The sprintf function
..
2 The sprintf function is equivalent to fprintf, except that the output is
written into an array (specified by the argument s) rather than to a
stream.
7.21.6.1 The fprintf function
...
4 Each conversion specification is introduced by the character %. After
...
% A % character is written. No argument is converted. The complete
conversion specification shall be %%.
ok but i dont fully understand it, from this above should
mlog("x%%"); just print x% or it is expected to not work?
(and if so how to pront the %?) (as i said in my tests that not worked)
fir
2017-04-01 19:31:50 UTC
Permalink
Raw Message
Post by fir
Post by Lew Pitcher
Post by fir
sorry im to tired at this moment to do full test
suite, but what i can say from memory
i use some kind of vararg function (in fact i may paste it)
void mlog(char *format, ...)
{
va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);
mlog_top++;
}
this is just used to do some on-screen (hud) logging
of messages, (mlog[][] is just an ram where i store communicates) the
function is correct but i encountered weird problem
it seem to show for all string characters except the %
which sign is not printed (i also used %% also no effect)
it just shows nothing so mlog("aaa%%b") just result in aaab
also mlog("aaa%cb", 0x25); when 0x25 is an ascii for % also
do not work
after a 20 minutes of confusion at least i found
mlog("aaa%sb", "%%") do work, but still its confusing
to me
is that expected behaviour?
Essentially, yes. This is expected behaviour
Post by fir
why is that so
(sorry i write partially form memory but to tired to do fulltestcase now)
Your code passes the percent symbol in what becomes the vsprintf() /format/
string. If you reread the requirements for the vsprintf format string, you
will find that, like in all the formatted output ("printf') functions, the
percent ('%') character is reserved as an escape character to introduce a
conversion specification. If, /in the format string/, you want your output
to contain a single percent sign, then you have to use the "%%" conversion
specification.
7.21.6.13 The vsprintf function
...
2 The vsprintf function is equivalent to sprintf, with the variable
argument list replaced by arg, which shall have been initialized by the
va_start macro (and possibly subsequent va_arg calls).
7.21.6.6 The sprintf function
..
2 The sprintf function is equivalent to fprintf, except that the output is
written into an array (specified by the argument s) rather than to a
stream.
7.21.6.1 The fprintf function
...
4 Each conversion specification is introduced by the character %. After
...
% A % character is written. No argument is converted. The complete
conversion specification shall be %%.
ok but i dont fully understand it, from this above should
mlog("x%%"); just print x% or it is expected to not work?
(and if so how to pront the %?) (as i said in my tests that not worked)
what funny it seems that %%%% works if one need to print one % (but how can i be sure it is right?) seven % prints still one % then eight %%%%%%%% prints %%

i must say i dont fullt understand varargs (always got no time for it), is there simple explanation what thise

va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);

really is? (especially what is args?)
Adam Wysocki
2017-04-02 15:39:05 UTC
Permalink
Raw Message
Post by fir
what funny it seems that %%%% works if one need to print one % (but how can i be sure it is right?) seven % prints still one % then eight %%%%%%%% prints %%
Probably later in the code you're inspecting it with:

printf(text);

While you should with:

printf("%s", text);
Post by fir
i must say i dont fullt understand varargs (always got no time for it), is there simple explanation what thise
va_list args;
va_start(args, format);
vsprintf(&mlog_[mlog_top][0], format, args);
va_end(args);
really is? (especially what is args?)
va_args is a structure holding arguments. va_start initializes it and
va_arg (used internally in vsprintf) gets the next argument (it needs to
know the argument type).
--
[ Adam Wysocki :: Warsaw, Poland ]
[ Email: ***@b a=grp b=chmurka.net ]
[ Web: http://www.chmurka.net/ ]
Rosario19
2017-04-02 09:01:53 UTC
Permalink
Raw Message
Post by fir
also mlog("aaa%cb", 0x25); when 0x25 is an ascii for % also
do not work
did you try mlog("aaa%%b") ?
Loading...