type promotions

M

Malcolm

j0mbolar said:
i know what type promotion is but why is it important?
It's not something you really need to understand to be an effective C
programmer. However if you use C a lot, if you play hack tricks, or if you
want to know what the compiler is doing under the bonnet, then you need to
know about it.

Some types have more precision than others. For instance a double can
express any float, but a float cannot express every double.

Now say we're printing out a floating point value.

float x = 1.0;

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

In early C compilers, arguments were usually passed as bytes on the stack.
How does printf() know whether you have passed it a float or a double? One
solution would be to use %f for floats and %lf or something for doubles,
however this would be a nuisance. The solution adopted was to convert all
floats into doubles when passed to variadic functions.
What this means is that if you are implementing a variadic function then all
the floating-point data will come at you as doubles, even if the caller
passes floats.

Another thing you need to be aware of is conversion of char to int in many
of the standard library functions (eg the isspace() family of functions).
There are several reasons for this. One is that int is the natural type to
use on the machine, so there may be efficiency gains. Another reason is that
error codes can be embedded in the integer. Another reason is that
expressions such as isspace(ch + 10) won't wrap round on overflow and give
misleading results. Another reason is that the constant expression 'c' has
integer type, and it simplifies the compiler warning system.

So type promotion is an important part of the C type system, which itself is
driven by the need to represent numbers in a machine-efficient way.
 
L

Leor Zolman

i know what type promotion is but why is it important?

It simplifies things, for the most part. Some examples:

Many math functions are written to accept and return doubles, but they'll
work fine when used with floats or ints (because the values are implicitly
promoted to double before the calls).

You can have mixed-mode arithmetic without having to cast all over the
place:

double x;
float f;
int i;

x = f + i;
x = (double) (f + (float) i); // pick one...

HTH,
-leor




Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
D

Dan Pop

In said:
i know what type promotion is but why is it important?

First, there is no such thing as "type promotion".

There are the "integral promotions", the "default argument promotions" and
the "usual arithmetic conversions".

There is no way to write correct non-trivial programs in C without
a very good understanding of all these three categories. But, these
being basic language features, they're off topic here, unless you have
a specific question about a specific aspect you don't understand.

Dan
 
P

Peter Pichler

[...]
But, these being basic language features, they're off topic here
[...]

Eh? I thought the topic here was *exactly* basic language features.
Badly asked question, yes. But off-topic, no!
 
K

Keith Thompson

Peter Pichler said:
[...]
But, these being basic language features, they're off topic here
[...]

Eh? I thought the topic here was *exactly* basic language features.
Badly asked question, yes. But off-topic, no!

I can see Dan's point. I'm not sure I'd refer to very basic questions
as off-topic (nor am I sure that I wouldn't), but there are certainly
some questions that are better answered by reading a textbook.

Without commenting directly on the question that started this thread,
if a beginner came here and asked "How do I declare an integer
variable?", it would be perfectly appropriate to tell the poster to
read a book (and if the answer isn't in the first few chapters, to
find a different book). Such a question isn't even a FAQ. This
newsgroup isn't the place to learn the language from scratch; it's the
place to discuss the language, particular aspects of it that might not
be clear after you've studied it.

The only possible answer to such a basic question that would be useful
to the poster would be to post a C textbook or tutorial to the
newsgroup. Since there are plenty of textbooks and tutorials out
there, there's not much point in doing that.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top