problem incrementing my variable

P

Phil Carmody

Bartc said:
Keith Thompson said:
CBFalconer said:
Ian Collins wrote:
CBFalconer wrote:

Note that printf expects to receive a double. You should so cast
the argument. This is one of the few places that a C cast is
needed.

Isn't that taken care of by default argument promotion?

No, because printf doesn't know anything about the actual argument
presented. Its only identification is in the format string, which
doesn't have to be fixed. You can construct that format string
just before calling printf, for example. printf assumes arguments
have been promoted, but nothing does any promotion.
[...]

You are mistaken. This:

float f = 42.0;
printf("f = %f\n", f);

is perfectly correct code. The value of is is promoted, by the
default argument promotions, from float to double, which matches the
"%f" format. The default argument promotions include both the integer
promotions and promotion from float to double. See C99 6.5.2.2p6-7.

CBF might have a point, at least when int/double conversions are
involved. This:

printf(s,i);

where i is an integer, and s *might* contain %d or %f, could cause a
problem I think.

What do possible characters in a string have to do with default
argument promotions? Either arguments of type X are promoted to
be of type Y or not when passed as parameters (and for X=float,
Y=double, they are), independently of any characters of any
string anywhere.

Phil
 
P

Phil Carmody

Keith Thompson said:
Certainly. "l" and "i" are passed as long and int, respectively, so
both printf calls are incorrect (invoke undefine behavior). Though in
both cases, a better solution would be to fix the format string:

long l = 42;
printf("%ld\n", l);

int i = 42;
printf("%d\n", i);

Unless the format string is computed at run time and imposed by
something that's outside the programmer's control.

Or the type of the value being printed is defined a long way away,
and may vary. Personally, I find casts on *printf parameters to
be the safest option for any code which I think might need to change.
But given this:

int i = 42;
printf(fmt, i);

fmt *must* consume one argument of type int. If it doesn't, the
behavior is undefined. If you happen to know that the format string
consumes one argument of type long, I suppose you could write:

int i = 42;
printf(fmt, (long)i);

but I can't think if any plausible scenario where that would actually
happen.

Casts are commonly required for arguments to printf, but usually when
there's no built-in format for the type you're passing. The classic
pre-C99 example is:

printf("sizeof x = %lu\n", (unsigned long)sizeof x);

though of course C99 has "%zu".

Don't you find that most of the types you encounter are typedefs?
Where I work, I certainly do.

Phil
 
B

Bart

Phil Carmody said:
What do possible characters in a string have to do with default
argument promotions? Either arguments of type X are promoted to
be of type Y or not when passed as parameters (and for X=float,
Y=double, they are), independently of any characters of any
string anywhere.

You're right; I was under that impression that automatic int<=>double
conversion occured when literal format strings were available. In fact
literal formats only make possible some error detection.
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top