Search code examples
ckernighan-and-ritchieputchar

Add an integer to a char in C


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?


Solution

  • '\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.