type promotions

Discussion in 'C Programming' started by j0mbolar, Feb 29, 2004.

  1. j0mbolar

    j0mbolar Guest

    i know what type promotion is but why is it important?
     
    j0mbolar, Feb 29, 2004
    #1
    1. Advertising

  2. j0mbolar

    Malcolm Guest

    "j0mbolar" <> wrote in message
    >
    > 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.
     
    Malcolm, Feb 29, 2004
    #2
    1. Advertising

  3. j0mbolar

    Leor Zolman Guest

    On 29 Feb 2004 12:13:13 -0800, (j0mbolar) wrote:

    >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

    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
     
    Leor Zolman, Feb 29, 2004
    #3
  4. j0mbolar

    Dan Pop Guest

    In <> (j0mbolar) writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Mar 1, 2004
    #4
  5. "Dan Pop" <> wrote in message
    news:c1vs2v$i8r$...
    [...]
    > 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!
     
    Peter Pichler, Mar 1, 2004
    #5
  6. "Peter Pichler" <> writes:
    > "Dan Pop" <> wrote in message
    > news:c1vs2v$i8r$...
    > [...]
    > > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Mar 1, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Able
    Replies:
    4
    Views:
    441
    Attila Feher
    Jan 28, 2004
  2. jimjim

    integer promotions

    jimjim, Sep 13, 2005, in forum: C Programming
    Replies:
    14
    Views:
    581
    Jack Klein
    Sep 15, 2005
  3. Question about integer promotions

    , Aug 3, 2006, in forum: C Programming
    Replies:
    4
    Views:
    331
    CBFalconer
    Aug 4, 2006
  4. Thurston Manson

    Bit-fields vs integral promotions

    Thurston Manson, Aug 23, 2007, in forum: C Programming
    Replies:
    15
    Views:
    555
    Jun Woong
    Aug 29, 2007
  5. Fred

    Integer Promotions

    Fred, Sep 28, 2007, in forum: C Programming
    Replies:
    11
    Views:
    651
    Jack Klein
    Sep 29, 2007
Loading...

Share This Page