Thread: Need some help understanding this definition.

1. Need some help understanding this definition.

Code:
`#define TO_UPPER(x) ( IS_LOWER_CASE (x) ? (x) - 'a' + 'A' : (x) )`

the above code converts lowercase to uppercase.

I don't quite understand "( IS_LOWER_CASE (x) ? (x) - 'a' + 'A' : (x) )"
The thing in the parenthesis, I think, is saying if x is lower case, then do (x)-'a'+'A'x).

But I don't understand how "(x)-'a'+'A'x)" converts lower case letters to uppercase letters.

2. If IS_LOWER_CASE, then do
Code:
`(x) - 'a' + 'A'`
else do
Code:
`x`
(actually do nothing because x is already caps

The ascii table is the answer

3. Yep, that's what it looks like to me as well.

How does it convert? Well, if you wanted to convert 'c' to 'C' for example... 'c' - 'a' will yield the numeric value 2. When you add 2 to 'A' the result is 'C'.

4. Well, until you run into other character coding schemes anyway.
https://en.wikipedia.org/wiki/Extend...terchange_Code

5. Also mind that usually we use these two functions (you have to include ctype.h)
isupper

islower

//Dexter if I ask you for an autograph, will I find myself with a "I used to be an adventurer like you... then I took an arrow to the knee." on my forehead?

6. The ? : operators comprise a "Ternary Conditional" Hope that helps!

7. "Characters" are just a number stored in memory that corresponds to a particular "symbol" in what's called ASCII (which just numbers every symbol, so 'a' has a different number to 'b', etc. etc. etc.). In memory they are just a number (which is why you can do maths with the "char" type in C), but when printed to the screen they are turned into the symbols from the ASCII set.

The character you start with is just stored in memory as its "ASCII code". For example 'g' is really stored as the number 103 (you can test this quite easily in C code by just printing a character variable with the value 'g' to the screen as either a character or an integer). To convert it to upper case, because of the way the ASCII table is laid out, you just need to find the equivalent symbol, but for an upper case character. If you look at the table linked to in one of the posts about, you could just take away 32 ('G' is 71, but 'g' is 103). But some people don't like magic numbers in their code because it confuses people like yourself as to what that 32 represents.

So instead what they do is they take away the number for 'a' (which just gives you how many letters along in the alphabet you are), and then add the number for 'A' (which moves you that same number of letters along but in the upper-case section of the ASCII table). Voila, upper-case conversion.

Personally, I think it's archaic, antiquated, and no more clear than just using 32 (and defining 32 with a sensible #define name if you really need to that explains what it is). And with Unicode (a replacement for ASCII with MANY more symbols from every language in the world ), things like "upper-case" and "lower-case" lose their meaning and become much more difficult to calculate, hence we normally use the suggested library functions for things like that (and, still, even in Unicode for Roman characters and the basic alphabet, Unicode shares the same values as ASCII so you can still just add 32).

Hope that helps.