Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

toupper() of 1 or 0

Avatar of Stephen Kairys
Stephen KairysFlag for United States of America asked on
9 Comments1 Solution435 ViewsLast Modified:
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

(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?


Avatar of Peter Kwan
Peter KwanFlag of Hong Kong imageAnalyst Programmer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers