Too confuse

This is a discussion on Too confuse within the C++ Programming forums, part of the General Programming Boards category; Hey people, I am a newbie to C++. I have learn from the books that C++ has strong type checking, ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Too confuse

    Hey people,
    I am a newbie to C++. I have learn from the books
    that C++ has strong type checking, but when I tried out a small program below and compiled with
    MS-Visual C++ 6.0, it raise no warning or error?
    Can anyone explain why? Thanx

    #include <iostream.h>

    int main()
    {
    int a;
    char b;

    cout<<"size of a: "<<sizeof(a)<<endl;
    cout<<"size of b: "<<sizeof(b)<<endl;

    a = 10;
    b = 105; //why OK with integer 105 instead of a
    //character as b = 'H', no compiler //warning here???

    cout<<a<<" and "<<b;
    cout<<a + b; //Confuse??? why int + char is //OK, no error???no warning??
    return 0;

    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    char is really just a small integer (short int is bigger, and long int is bigger than short int). This is part of the historical link with C.

    So all that happens when you have
    char_var + int_var

    is promotion of the char_var to an int_var, and then the addition takes place as you would normally expect for integer arithmetic.

    When you say
    char b = 'A';
    this is a normal assignment of one char value to another char value

    Doing
    char b = 65;
    is an int to char assignment, which involves some truncation - which the compiler will normally do silently if it sees the value will fit in a char anyway.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    When the promotion takes place is a temporary variable created?
    Be a leader and not a follower.

  4. #4
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    think about it subdene!
    Such is life.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Do you know fountain? I could do with some help on this matter.
    Be a leader and not a follower.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    in most cases i do not think so. c compilers nowadays do many optimizations. here's an easy way to think of it.

    in every x86 computer, there are 4 general-purpose registers: eax, ebx, ecx, and edx. they hold 32-bit integers. 'al' in assembly stands for the least significant 8 bits of eax. 'ax' is the least significant 16 bits of eax.

    assembly code:
    Code:
    mov eax,0       ;eax = 0
    mov al,[memory_location]     ;al = *(memory_location)
    your compiler would normally pull a char variable out of memory and store it in al. al can then be promoted to eax with no trouble at all. eax is used instead of al by the assembler. sometimes your compiler will even optimize further, circumventing the whole promotion deal.

    in short: most of the time, no. however, if you're working with many chars and it's impossible to conviently use the bigger register, your compiler might need a temporary variable. but it's far more likely that your compiler has already prepared the code so that it is convienent to promote the char.

    however, if you're using floating point variables, that promotion has likely already taken place. all floating point variables are stored in their registers in 80-bit mode, bigger than any normal float or double. a simple change in commands would be needed.

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thanks for the explanation, it's a lot clearer now.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PayPal confuse me
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-20-2009, 11:56 AM
  2. a little confuse o_0???
    By omarbags in forum C++ Programming
    Replies: 15
    Last Post: 01-24-2008, 12:03 AM
  3. Simple Sorts Confuse ME =/
    By otchster in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 01:02 PM
  4. confuse about included files
    By C-Dumbie in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2002, 06:04 PM
  5. confuse with character array
    By dv007 in forum C Programming
    Replies: 6
    Last Post: 08-09-2002, 01:05 PM

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