• 0

why does this bit of code work?


Question

7 answers to this question

Recommended Posts

  • 0

Because char is interchangeable with integer. They have slightly different properties in most languages, but a char and int are essentially just variables of different length.

 

One thing that puzzles me though - I may be wrong here, but I assume '' will equate to the (nul) character, which has a value of 0? Edit: didn't see the space in there!

Link to comment
Share on other sites

  • 0

I assume it's supposed to convert lower case chars into upper case chars?

 

ASCII value for a space (' ') is 32. Integer representations of lower case chars are 32 higher than those of their equivalent upper case chars. So if you subtract 32 from a lower case char it will become an upper case char.

 

Example: A = 65, a = 97

'a' - ' ' = 'A'

97 - 32 = 65

  • Like 2
Link to comment
Share on other sites

  • 0

So in it's basic form, the alphabet is represented by a computer as a list of numbers called ASCII (see here). So the char data type which represents a character of the alphabet (as well as punctuation and various other types of "symbol") is actually just a number between 0 and 255.

While this is convenient for a computer to interpret, it also allows for some weird coding like what you've demonstrated. As C?i correctly pointed out, the space character is actually represented by the number 32 (see the link I posted earlier), so in the code you posted, you're converting a lower case character to it's upper case equivalent (the ASCII numbers for upper case letters are all 32 lower than their lower case alternatives. (A = 65, a = 97; 97 - 65 = 32).

It's worth bearing in mind that while this is possible, it's not recommended as it's confusing to read ('a' minus space is 'A'? What the heck?), so try to avoid this code if possible. It's always better to be obvious in your intentions when writing code, as it makes your code easier to read and maintain at a later date. :)

Appendix 1

ASCII is just one way of encoding characters, and its also very limited. For more advanced computer programs, Unicode is often preferable as it provides the ability to represent almost and character in any language (Chinese, Japanese, Greek, Arabic, etc), whereas ASCII cannot do this. Since you're a relative beginner though, there's no need to worry about this. Encoding strings is almost a science in its own right.

Appendix 2

As another weirdness, you can also do this:

 

unsigned int neow = 'NEOW';
unsigned int in = 'IN';

printf("neowin = %d", neow + in); // "neowin = 1313192133"
Since each character in "NEOW" and "IN" represents a single byte, 4 together represent a 4 byte integer, and therefore you can populate numbers using strings! I must insist however that you NEVER, EVER, use this code, since it's absolutely the worst code in the world.
Link to comment
Share on other sites

  • 0

So in it's basic form, the alphabet is represented by a computer as a list of numbers called ASCII (see here).

Good old asciitable.com

I used to have that image printed out and stuck on the wall next to my monitor. It's like the multiplication table, FOR GROWNUPS!

  • Like 1
Link to comment
Share on other sites

  • 0

It's the same as doing

'1' - '0' = 1 

Converting any char (that is a number (0-9)) into an integer is as easy as subtracting the '0' char.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.