Like Tree3Likes
  • 1 Post By std10093
  • 1 Post By hk_mp5kpdw
  • 1 Post By Salem

Need some help understanding this definition.

This is a discussion on Need some help understanding this definition. within the C Programming forums, part of the General Programming Boards category; Code: #define TO_UPPER(x) ( IS_LOWER_CASE (x) ? (x) - 'a' + 'A' : (x) ) the above code converts lowercase ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    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. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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
    ddang likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    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'.
    ddang likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well, until you run into other character coding schemes anyway.
    https://en.wikipedia.org/wiki/Extend...terchange_Code
    ddang likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    The ? : operators comprise a "Ternary Conditional" Hope that helps!

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    "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.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Definition of myself
    By ahmed el sakka in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2011, 11:33 AM
  2. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  3. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  4. definition of ITypeInfo
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 02-15-2008, 04:51 AM
  5. Definition?
    By reRanger in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2004, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21