multiplying chars

This is a discussion on multiplying chars within the C Programming forums, part of the General Programming Boards category; Code: char test1 = 15; char test2 = 10; char test3 = test1*test2; printf("%d ", test3); Why does this code ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    multiplying chars

    Code:
    char test1 = 15;
    char test2 = 10;
    char test3 = test1*test2;
    
    printf("%d ", test3);
    Why does this code result in printing out -106? Is there any way I can easily make test3 into the correct value of 150, or is it just stupid using chars like this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Chars are signed by default [in most compilers] and any value with the highest bit set (that is, a value above 127 in most architectures) will be considered a negative number. In this case, 150 is bigger than 127, so it is treated as a negative number - essentially 150-256 => -106

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    The value of char in C is (-128 -> 127), or using unsigned char instead of char if you want 150.
    Last edited by bbebfe; 11-20-2008 at 06:08 AM.
    Do you know why bbebfe is NOT bbebfe?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bbebfe View Post
    The value of char in C is (-128 -> 127)
    This is of course not GUARANTEED (a DEC-10 would perhaps have 4 chars per 36-bit word -> 9 bit per char and a range of -256..255, to give an example), but yes, it's negative because it's above the positive range of the char numbers in this architecture.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Is there such a thing as unsigned char, like with int?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Never mind, took me 30 secs to test it myself, thanks for the answers.

  7. #7
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    matsp is right, How max and min of a type, there is no definite value within C, It's depends on the C compiler and target system. You can get the max and min value of char by referring the CHAR_MAX and CHAR_MIN macros defined in <limits.h>
    Code:
    printf("max=&#37;d, min=%d\n", CHAR_MAX, CHAR_MIN);
    Do you know why bbebfe is NOT bbebfe?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > Chars are signed by default
    Without qualification, whether a 'char' is signed or unsigned is implementation-specific.

    gcc (for example) has a flag to change the behaviour, should it happen to matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > Chars are signed by default
    Without qualification, whether a 'char' is signed or unsigned is implementation-specific.

    gcc (for example) has a flag to change the behaviour, should it happen to matter.
    Yes, that's why I put "[in most compilers]". And I know a few other compilers also have an option to make char unsigned by default. I don't know of a compiler that without flags make char unsigned, but I wouldn't be surprised if they do exist.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 05:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 01:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 01:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 03:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 05:29 PM

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