-2

I'm learning C from the K&R book, I found a suggested solution online to a task in the book.The task and suggested answer can be found here (the last solution on that page) http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_13

Where there is this line of code :

putchar('\260' + (MIN(wl[j]-i, 2)));

So for example ,if the function MIN returned 2 ,we add it to '\260'

putchar('\260' + 2);

What is this method of adding an int to a char? what is this '\260' value?

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
blender28
  • 3
  • 2

1 Answers1

0

'\260' is the character represented by the octal number 260, which is 176 in decimal.

Use of putchar('\260' + 2); relies on the graphical representation of a non-ASCII character represented by the integer value 178.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • The funny thing that `int i = '\260'` will consider the rhs constant as char and yield `i= -80` by sign-extending it (on the `signed char` platform of course...) https://ideone.com/YdV6Dz – Eugene Sh. Nov 23 '16 at 21:01
  • @EugeneSh.: Correct conclusion, incorrect reasoning. Character constants are of type `int`. The sign of the value of a character constant outside the range of 0 .. `CHAR_MAX` is determined by the signedness of type `char`. [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 6.4.4.4p10. – Keith Thompson Nov 23 '16 at 21:35
  • @KeithThompson Thanks. It says it is implementation defined. *"..containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."* – Eugene Sh. Nov 23 '16 at 21:42
  • The web site shows an image of the supposed output of the program. I don't know what character set the author is using -- apparently one in which 0xb0, 0xb1, and 0xb2 are suitable for use in a histogram, but in both Unicode and Windows-1252 those characters are DEGREE SIGN, PLUS-MINUS SIGN, and SUPERSCRIPT TWO, respectively. – Keith Thompson Nov 23 '16 at 21:42
  • @EugeneSh.: Ah, but in the previous paragraph it says that an octal or hexadecimal escape sequence is constrained to be in the range of `unsigned char`. But the result (for `CHAR_BIT==8, plain `char` signed) of converting `176` from `unsigned char` to `char` is implementation-defined. Ick. – Keith Thompson Nov 23 '16 at 21:48