Sign in to follow this  
Followers 0
Terabojin

why does this bit of code work?

8 posts in this topic

Hey guys,

 

I ran into something that has me puzzled....why does if(ch1 > 'Z') ch1 -= ' '; work? 

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

2 people like this

Share this post


Link to post
Share on other sites

yes it converts lower case to upper case, that much I do know, but wasn't sure how or why it worked

Share this post


Link to post
Share on other sites

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.
1 person likes this

Share this post


Link to post
Share on other sites

Thank you for your explanations!  Makes perfect sense now lol.

Share this post


Link to post
Share on other sites

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!

1 person likes this

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.