printf doubt

D

dutche

Hi, there is some kind of difference in these two statements?

float num = 154.87;
printf("There is : $0.0f",num);


and


float num = 154.87;
printf("There is : $f",num);
 
R

Richard Heathfield

dutche said:
Hi, there is some kind of difference in these two statements?

float num = 154.87;
printf("There is : $0.0f",num);


and


float num = 154.87;
printf("There is : $f",num);

Yes. (We will assume that the '$' character is in the execution character
set.)

The first of your examples will print:

There is : $0.0f

The second will print:

There is : $f

These are different outputs.
 
D

dutche

Richard Heathfield escreveu:
dutche said:


Yes. (We will assume that the '$' character is in the execution character
set.)

The first of your examples will print:

There is : $0.0f

The second will print:

There is : $f

These are different outputs.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



Sorry I made a mistake :)

float num = 154.87;
printf("There is : $%0.0f",num);

and

float num = 154.87;
printf("There is : $%f",num);
 
A

Andrew Poelstra

Hi, there is some kind of difference in these two statements?

float num = 154.87;
printf("There is : $0.0f",num);


and


float num = 154.87;
printf("There is : $f",num);

Run them, and you'll note that one will print "There is : $0.0f" and
other other will print "There is : $f", neither of which will have a
newline*

* Actually, on a lot of systems it won't print at all, because you're
missing a newline.

You need to read a good beginner's C book.
 
R

Richard Heathfield

dutche said:

float num = 154.87;
printf("There is : $%0.0f",num);

This will print the value in at least 0 (first 0!) columns but possibly
more, and with a precision of 0 (second 0) decimal places. The number is
"rounded to the appropriate number of digits", as the Standard puts it. So
in this case it'll display 155 (because that's closer than 154).
and

float num = 154.87;
printf("There is : $%f",num);

This will just print the value, to default precision. On my implementation
it gives 154.869995 as the result.
 
A

Andrew Poelstra

You need to read a good beginner's C book.

Never mind; I thought you confused `$' and `%', but according to your
other post, you just forgot to type `%', which is an understandable
mistake (because the characters are right beside each other on a US
keyboard, and you'd already typed one).
 
F

Fred Kleinschmidt

dutche said:
Hi, there is some kind of difference in these two statements?

float num = 154.87;
printf("There is : $0.0f",num);


and


float num = 154.87;
printf("There is : $f",num);
Yes. The first one will print:
There is : $0.0f
and the second will print:
There is : $f

And without a newline, it is possible that neither will show up.
 
R

Robert Gamble

Richard said:
dutche said:



This will print the value in at least 0 (first 0!) columns but possibly
more, and with a precision of 0 (second 0) decimal places. The number is
"rounded to the appropriate number of digits", as the Standard puts it. So
in this case it'll display 155 (because that's closer than 154).


This will just print the value, to default precision.

More precisely, it will print the value as if a precision of 6 had been
specified (ignoring the fact that no newline is present).

Robert Gamble
 
B

Barry Schwarz

dutche said:


Yes. (We will assume that the '$' character is in the execution character
set.)

The first of your examples will print:

There is : $0.0f

The second will print:

There is : $f

These are different outputs.

Does not the presence of a superfluous argument lead to undefined
behavior?


Remove del for email
 
B

Ben Pfaff

Barry Schwarz said:
Does not the presence of a superfluous argument lead to undefined
behavior?

No. From C99 7.19.6.1 "The fprintf function":

If the format is exhausted while arguments remain, the
excess arguments are evaluated (as always) but are otherwise
ignored.
 
R

Richard Heathfield

Barry Schwarz said:

Does not the presence of a superfluous argument lead to undefined
behavior?

No. The relevant C89 draft wording is:

"If the format is exhausted while arguments remain, the excess
arguments are evaluated (as always) but are otherwise ignored."
 
C

Chris Torek

dutche said:
This will print the value in at least 0 (first 0!) columns ...

Aha, a rare Richard Heathfield error! :)

In a printf conversion, the "0" character is a *flag*, not a field
width, unless:

- you are already working on a field width, or
- you are already working on a precision

(and possibly if you use various extensions as well). For instance,

printf("%040d\n", 12);

prints "0000000000000000000000000000000000000012\n", while:

printf ("%40d\n", 12);

prints " 12\n".

A zero flag is allowed with %f conversions, with the obvious meaning
(so printing 0.5 with %012.6f produces "00000.500000"; note that the
field width includes the decimal-point character).
but possibly more, and with a precision of 0 (second 0) decimal
places.

Note that, for %f format, if no precision is specified, a default
value of 6 is assumed. So:

printf("%f\n", val);

and:

printf("%.6f\n", val);

have identical meanings.
 
F

Flash Gordon

Does not the presence of a superfluous argument lead to undefined
behavior?

No, you are explicitly allowed to pass extra arguments to printf. The
standard states "If the format is exhausted while arguments remain, the
excess arguments are evaluated (as always) but are otherwise ignored."
 
K

Keith Thompson

Andrew Poelstra said:
Run them, and you'll note that one will print "There is : $0.0f" and
other other will print "There is : $f", neither of which will have a
newline*

* Actually, on a lot of systems it won't print at all, because you're
missing a newline.

Actually, we don't know that he's missing a newline. He's only showed
us a couple of small code fragments, not an entire program. If
putchar('\n') or equivalent is executed later on, he's fine (at least
as far as trailing newlines are concerned).
 
B

Barry Schwarz

No. From C99 7.19.6.1 "The fprintf function":

If the format is exhausted while arguments remain, the
excess arguments are evaluated (as always) but are otherwise
ignored.

I wonder how systems which push return addresses and arguments on the
same stack, like the old 6502, manage this. Obviously, fprintf will
not pop the superfluous arguments off the stack since it doesn't even
know about them.


Remove del for email
 
B

Ben Pfaff

Barry Schwarz said:
I wonder how systems which push return addresses and arguments on the
same stack, like the old 6502, manage this.

What systems use separate stacks for return addresses and
arguments? I am not aware of any in the modern world, so it
would be educational to hear about them. Such a system might be
more resistant to "stack smashing" buffer overflow attacks, for
one thing.
Obviously, fprintf will not pop the superfluous arguments off
the stack since it doesn't even know about them.

A common convention is for the callee to pop off the return
address and leave the arguments on the stack. The caller then
pops the arguments.
 
O

Old Wolf

Barry said:
I wonder how systems which push return addresses and arguments on the
same stack, like the old 6502,

and x86, and sparc, ...
manage this. Obviously, fprintf will not pop the superfluous arguments
off the stack since it doesn't even know about them.

The caller cleans up the stack. For example, the caller might
push the arguments and return address, then call the function,
then rewind the stack pointer.
 
M

Morris Dovey

Barry Schwarz (in (e-mail address removed)) said:

| I wonder how systems which push return addresses and arguments on
| the same stack, like the old 6502, manage this. Obviously, fprintf
| will not pop the superfluous arguments off the stack since it
| doesn't even know about them.

From out of the mists of time I heard a raspy voice whisper: "Dope
vectors..." In some environments they were used exclusively to
describe arrays; and on others to describe and point to call
parameters.
 
K

Keith Thompson

Barry Schwarz said:
I wonder how systems which push return addresses and arguments on the
same stack, like the old 6502, manage this. Obviously, fprintf will
not pop the superfluous arguments off the stack since it doesn't even
know about them.

Presumably the caller has to pop the arguments (or pass an extra
implicit argument so the callee knows what to pop).
 

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

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top