Stephen Kairys
asked on
toupper() of 1 or 0
In parsing an arglist in a C program, I meant to say (to enable validation if
the arg in question had a 'V' or 'v' in the "2nd" positoin e.g. -XV or -xv
if (toupper(argv[argc-1][2]) == 'V') then
do_validation = TRUE;
but I had a typo:
if (toupper(argv[argc -1][2] == 'V'))
do_validation = TRUE;
so in effect I was taking toupper() not of 'V', 'v', or whatever, but of the entire
expression:
(argv[argc -1][2] == 'V')
As blatant as that typo was, I found that do_validation was still being set to TRUE
if the value in question was 'V' (but not 'v', or anything else) e.g.
-XV sets is to TRUE
-xv does not
-x does not
-X does not
I want to be sure I understand why. As I see it, if the char in question
argv[argc -1][2]
is 'V', then the expression:
(argv[argc -1][2] == 'V')
evaiuates to 1.
toupper(1) is (char)1.
For any other value, including NULL (which would be the case if I had -X)
(argv[argc -1][2] == 'V')
evaluates to 0
toupper(0) is (char)0.
Ergo, my test still works, as long as I have an UPPERCASE 'V' .
Of course, I will fix this, but as I may have some sites running -XV right now, I want to
rest assured they will be enabling the validation flag.
So...is my analysis correct?
Thanks,
Steve
the arg in question had a 'V' or 'v' in the "2nd" positoin e.g. -XV or -xv
if (toupper(argv[argc-1][2]) == 'V') then
do_validation = TRUE;
but I had a typo:
if (toupper(argv[argc -1][2] == 'V'))
do_validation = TRUE;
so in effect I was taking toupper() not of 'V', 'v', or whatever, but of the entire
expression:
(argv[argc -1][2] == 'V')
As blatant as that typo was, I found that do_validation was still being set to TRUE
if the value in question was 'V' (but not 'v', or anything else) e.g.
-XV sets is to TRUE
-xv does not
-x does not
-X does not
I want to be sure I understand why. As I see it, if the char in question
argv[argc -1][2]
is 'V', then the expression:
(argv[argc -1][2] == 'V')
evaiuates to 1.
toupper(1) is (char)1.
For any other value, including NULL (which would be the case if I had -X)
(argv[argc -1][2] == 'V')
evaluates to 0
toupper(0) is (char)0.
Ergo, my test still works, as long as I have an UPPERCASE 'V' .
Of course, I will fix this, but as I may have some sites running -XV right now, I want to
rest assured they will be enabling the validation flag.
So...is my analysis correct?
Thanks,
Steve
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As pkwan says, your analysis is correct.
Note that there are two toupper functions: toupper and _toupper. They behave differently. Check your implementation in the header file. One uses a lookup table, which is the proper way to do it since ASCII is very different from EBCDIC. The other switches between upper and lowercase which is absolutely mental. I can't remember which one does which but if you get strange behaviour, check the implementation in the header file. Use the one that uses the lookup table: not the one that exclusive ors 0x20.
Note that there are two toupper functions: toupper and _toupper. They behave differently. Check your implementation in the header file. One uses a lookup table, which is the proper way to do it since ASCII is very different from EBCDIC. The other switches between upper and lowercase which is absolutely mental. I can't remember which one does which but if you get strange behaviour, check the implementation in the header file. Use the one that uses the lookup table: not the one that exclusive ors 0x20.
ASKER
PKWAN - Thank you for your confirmation.
DEEPUDEEPAM- I suppose I could chck for 'v' or 'V' but using toupper() involves one less IF clause, is easier (in my opinion, anyway :) ) to read,involves less typing, etc.
CUP-I wrote a test program which revealed that
toupper(1) is 1 and toupper(0) is 0. Does that
result mean that I have the "proper" toupper() in my C?
Just out of curiosity, how would I tell, just from the header file, which implemntation is used?
Thanks to everyone for their responses. I'll check back again no later than Monday.
-Steve
DEEPUDEEPAM- I suppose I could chck for 'v' or 'V' but using toupper() involves one less IF clause, is easier (in my opinion, anyway :) ) to read,involves less typing, etc.
CUP-I wrote a test program which revealed that
toupper(1) is 1 and toupper(0) is 0. Does that
result mean that I have the "proper" toupper() in my C?
Just out of curiosity, how would I tell, just from the header file, which implemntation is used?
Thanks to everyone for their responses. I'll check back again no later than Monday.
-Steve
Very vendor dependent. Have a look in stdlib.h
In some headers, toupper is a macro: not a library function. On Windows, it is a library function. On the ones where it is a macro, it will normally be either
#define toupper(x) (x ^ 0x20) // this is the one to avoid
#define toupper(x) (upperlut[x]) // or something similar - these are OK
Also note that toupper only works for a-z. It does not work on the European characters with diacritics.
In some headers, toupper is a macro: not a library function. On Windows, it is a library function. On the ones where it is a macro, it will normally be either
#define toupper(x) (x ^ 0x20) // this is the one to avoid
#define toupper(x) (upperlut[x]) // or something similar - these are OK
Also note that toupper only works for a-z. It does not work on the European characters with diacritics.
Hi steve,
>> one less IF clause, is easier (in my opinion, anyway :) ) to read,involves less typing, etc.
its fine in case of readablity and understanding, that i agree But if you are realy caring about execution speed better use two condisions or a macro.
Deepu
>> one less IF clause, is easier (in my opinion, anyway :) ) to read,involves less typing, etc.
its fine in case of readablity and understanding, that i agree But if you are realy caring about execution speed better use two condisions or a macro.
Deepu
ASKER
CUP-
My stdlib.h does not have a #define for toupper().
DEEPUDEEPAM-
Interesting point. In my case, however, this code is executed only a smalL# of times when parsing the command line parameters, so I'm not concerned with speed; if it were in a loop I executed, say, 10,000 times, it might be another story :)
Thank you both for your responises.
Steve
My stdlib.h does not have a #define for toupper().
DEEPUDEEPAM-
Interesting point. In my case, however, this code is executed only a smalL# of times when parsing the command line parameters, so I'm not concerned with speed; if it were in a loop I executed, say, 10,000 times, it might be another story :)
Thank you both for your responises.
Steve
ASKER
CUP-
Hey, did you see my repsonse from last week?
>>My stdlib.h does not have a #define for toupper().<<
I'm just curious where I can find the #define in that case, and (if I can't), how I'd know which implentation my compiler uses.
Thanks
Steve
Hey, did you see my repsonse from last week?
>>My stdlib.h does not have a #define for toupper().<<
I'm just curious where I can find the #define in that case, and (if I can't), how I'd know which implentation my compiler uses.
Thanks
Steve
ASKER
Paul,
Thanks for checking in!,
I was still hoping to hear back from CUP
regading the below:
>>My stdlib.h does not have a #define for toupper().<<
however, no big deal if he can't respond...I'll go along with your recommendation.
-Steve
Thanks for checking in!,
I was still hoping to hear back from CUP
regading the below:
>>My stdlib.h does not have a #define for toupper().<<
however, no big deal if he can't respond...I'll go along with your recommendation.
-Steve
you can check both the conditions here.Why you are going for a function call.It will take some prossessing time
Better you can check like this,
if((argv[argc -1][2] == 'v') || (argv[argc -1][2] == 'V'))
Can youy explain your question in detail?
Deepu