-7

this is the code (copy & pasted):

#include <stdio.h>
int main(){
    char x,y,result;
    //Sample 1:
    x =  35;
    y =  85;
    result =  x + y;
    printf("Sample 1: %hi + %hi = %hi\n",x ,y, result);
    //Sample 2:
    x =  85;
    y =  85;
    result = x + y;
    printf("Sample 2: %hi + %hi = %hi\n",x ,y, result);
return 0; 
}

I've tried to compile it but it doesn't work. Am I stupid or is it "int" or "short" instead of char at the beginning? Once I change this it works, but I'm worried that it should work as is... Does the program really just add x and y and show the result? That's what it does if I use short instead of char.

Thanks in advance!

E: Why the down votes? What is wrong with my post?

  • 4
    `but it doesn't work` - please describe what errors you get, and what actually does not work (output you get versus output you expect) – Andreas Fester Oct 29 '14 at 14:57
  • It compiles fine here. What does not work ? Does it compile ? If no, what errors do you get on which lines ? If you write "it doesn't work", the question will most likely be downvoted. – Jabberwocky Oct 29 '14 at 14:59
  • And, please include **how** you compile it - with `gcc -std=c99 -pedantic -Wall` I get no errors or warnings – Andreas Fester Oct 29 '14 at 14:59
  • 2
    Just a polite reminder, your downvotes are because you are (possibly) abusing stackoverflow. It seems like 1st/year-0 University PRojects to me (or you are learning C yourself). Stackoverflow is for problems that are not common/blockers to professional people. No one is trying to be mean to you :) – ha9u63a7 Oct 29 '14 at 15:00
  • 4
    When you say that something does not work, you should explain two things: (#1) what did you expect your program to do, and (#2) what did the program do instead. Also, the title of your post is a downvote magnet. – Sergey Kalinichenko Oct 29 '14 at 15:00
  • @hagubear: I don't think the OP is "possibly abusing" SO. Yes there wasn't quite as much info in the question as to what exactly wasn't understood and that is a problem. But SO is only "for professionals"? I disagree... it is a teaching/learning tool and this appears to be a very genuine and legitimate misunderstanding and quite an important point for anyone working in C to understand, professional or student. There's nothing wrong with a uni project or self learning in my opinion if there is a genuine point someone can't understand... everybody learns. – Jimbo Oct 29 '14 at 15:10
  • @Jimbo Google Stack Overflow. "**Stack Overflow is a question and answer site for professional and enthusiast programmers**. It's 100% free, no registration required. Take the 2-minute tour." It is not a site for teaching and learning programming and it never was. This has been debated endlessly on Meta. – Lundin Oct 29 '14 at 15:18
  • Questions with code should describe what is expected to happen, what does happen, error/exception messages and what was found during testing and debugging so far. Anything else should expect a down or close vote. – Martin James Oct 29 '14 at 16:25

2 Answers2

4

Thoughts:

For an introductory course, this is a terrible example. Depending on your implementation, char is either a signed or unsigned number. And the code will behave very differently depending on this fact.

That being said, yes, this code is basically adding two numbers and printing the result. I agree that the %hi is odd. That expects a short int. I'd personally expect either %hhi or just %i, and let integer promotion do it's thing.

If the numbers are unsigned chars

  • 85 + 35 == 120, which is probably less than CHAR_MAX (which is probably 255). So there's no problem and everything works fine.
  • 85 + 85 == 170, which is probably less than CHAR_MAX (which is probably 255). So there's no problem and everything works fine.

If the numbers are signed chars

  • 85 + 35 == 120, which is probably less than CHAR_MAX (which is probably 127). So there's no problem and everything works fine.
  • 85 + 85 == 170, which is probably greater than CHAR_MAX. This causes signed integer overflow, which is undefined behavior.
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

The output of the program appears to be

Sample 1: 35 + 85 = 120
Sample 2: 85 + 85 = -86

I compiled this on http://ideone.com/ and it worked fine.

The output is in fact what you would expect. The program is working! The reason you are seeing a number that you do not expect is due to the width of a char data type - 1 byte.

The C standard does not dictate whether char is signed or unsigned but assuming it is signed it can represent numbers in the range -128 to 127 (a char is 8 bits or 1 byte). 85 + 85 = 170 which is outside of this range... the MSB of the byte becomes 1 and the number system wraps round to give you a negative number. Try reading up on twos compliment arithmetic.

The arithmetic is:

 01010101 +
 01010101
 --------
 10101010

Because the data type is signed and the MSB is set, the number is now negative, in this case -86

Note: Bill Lynch's answer... he has rightly pointed out that signed overflow is UB

Jimbo
  • 4,352
  • 3
  • 27
  • 44
  • 2
    Signed integer overflow is actually just undefined: http://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is – Bill Lynch Oct 29 '14 at 15:02
  • 1
    Very good point, thanks Bill. +1 to your main answer and comment :) Interesting link... learnt something – Jimbo Oct 29 '14 at 15:04