Terabojin Posted October 6, 2013 Share Posted October 6, 2013 Hey guys, I ran into something that has me puzzled....why does if(ch1 > 'Z') ch1 -= ' '; work? Link to comment Share on other sites More sharing options...
0 Brian M. Veteran Posted October 6, 2013 Veteran Share Posted October 6, 2013 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 More sharing options...
0 Coi Posted October 6, 2013 Share Posted October 6, 2013 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 Squirrelington and Andre S. 2 Share Link to comment Share on other sites More sharing options...
0 Terabojin Posted October 6, 2013 Author Share Posted October 6, 2013 yes it converts lower case to upper case, that much I do know, but wasn't sure how or why it worked Link to comment Share on other sites More sharing options...
0 +Majesticmerc MVC Posted October 6, 2013 MVC Share Posted October 6, 2013 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. Karl L. 1 Share Link to comment Share on other sites More sharing options...
0 Terabojin Posted October 6, 2013 Author Share Posted October 6, 2013 Thank you for your explanations! Makes perfect sense now lol. Link to comment Share on other sites More sharing options...
0 GreenMartian Posted October 7, 2013 Share Posted October 7, 2013 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! +Majesticmerc 1 Share Link to comment Share on other sites More sharing options...
0 firey Posted October 7, 2013 Share Posted October 7, 2013 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 More sharing options...
Question
Terabojin
Hey guys,
I ran into something that has me puzzled....why does if(ch1 > 'Z') ch1 -= ' '; work?
Link to comment
Share on other sites
7 answers to this question
Recommended Posts