integral promotions

This is a discussion on integral promotions within the C Programming forums, part of the General Programming Boards category; hi all Code: #include<stdio.h> #include<stdlib.h> main() { char x; char f; int z; x='f'; f='d'; z=x+f; printf("\n &#37;d , %d\n",x,f); ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    integral promotions

    hi all
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    main()
    {
            char x;
            char f;
            int z;
    
            x='f';
            f='d';
            z=x+f;
    
            printf("\n &#37;d , %d\n",x,f);
            printf("%d\n",z);
    
    exit(EXIT_SUCCESS);
    
    }
    as my topic says i am asking about integral promotions
    when integral promotion applied shouldn'y the value be an int in case it cane hold it and unsigned int in case int can not hold it
    so
    look at this code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    main()
    {
            char x;
            char f;
            char z;
    
            x='f';
            f='d';
            z=x+f;
    
            printf("\n %d , %d\n",x,f);
            printf("%d\n",z);
    
    exit(EXIT_SUCCESS);
    
    }
    it gives me the output -54 !!!!!!!! how this could be ?
    shouldn'y it gives me error or somtehing that it cannot assgine value to this kind or variables ???
    or i am getting the hall thing wrong ?
    so my question is
    how and where integral pormotions occures ?
    thanks in advance

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    if the new type can hold all of the values of the old type, then the value remains unchanged.
    i did a samle search and i found this
    that's mean it's occure in the expressions when it's required ?
    and it's value assgined to the vraiable
    if it can hold it the value is not changed but if it can't the precision is lost ?
    thanks

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Integral promotion occurs when you convert a smaller variable into a larger variable, for example a char into an int, or a long into a double.

    As for why your last program outputs -54: you're adding 'f' and 'z' and storing the result in a char. This is 97 + 100, from an ASCII chart. This is 197, which is over 127, the maximum value that can be stored in a signed char (which is obviously what you implementation uses for "char"). So it wraps.

    [edit] As for your second post, that sounds about right.

    If the new type is larger (can hold more values) than the previous type, no data is lost. But if you cast downwards, from a large variable into a small variable, you might lose some data, depending on the actual value stored in the large variable. For example, (long)1 can be converted to a char properly, but (long)123456 cannot. [/edit]
    Last edited by dwks; 06-28-2007 at 04:25 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    as a side note, char is supposed to be unsigned by default.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Nooo . . . it depends on the implementation. "char" can be "signed char" or "unsigned char", depending on the implementation. If you want a signed char or unsigned char, say so specifically.

    [edit] And on nearly all platforms, "char" is the same as "signed char".

    In fact, the keyword "signed" was added to the language so that you could use signed chars on platforms where the default char is unsigned. It's completely redundant in all other cases. [/edit]
    Last edited by dwks; 06-28-2007 at 04:34 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    oh. thanks for setting me straight.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    thanks for both of you

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Also, in case you're interested in the exact integer promotion rules, you can find them around. Here's one copy: http://www.devarticles.com/c/a/Cplus...ic-Data-Types/

    I haven't read them, so I don't know how extensive they are. But if they are not in-depth enough for you, just use google to find some other page.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert integral types into pointer types?
    By rohit99 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 09:57 AM
  2. Problem with Dawson's integral
    By lordbubonicus in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 02:44 PM
  3. integral counterpart to pow()?
    By cyberfish in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2007, 09:21 PM
  4. Hashing non integral types
    By Puppis in forum C++ Programming
    Replies: 0
    Last Post: 06-08-2005, 04:17 PM
  5. integral constant overflow error?
    By paperbox005 in forum C Programming
    Replies: 3
    Last Post: 05-06-2005, 09:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21