I'm struggling with the output to the "Validating Caesar's Key" part of PSet 2. First-timer here, like many, and appreciative of any insight. When I compile and then run this program with "./caesar 2", for example, I get the output "Usage: ./caesar key" despite its being numeric. I think I'm getting hung up on the isdigit() portion. Not sure that can be combined with a Boolean expression? Or if I've done that part wrong? TIA!

Screenshot of sandbox

1 Answer 1


The problem lies here:

if(isdigit(argv[1][i] != true)

You need to fully understand how isdigit() works, specifically the return value, and the numeric value of true.

The is() family of functions all return an int. When true, each will return a different number that is a power of 2. This goes back to the history of programming and register values used in assembler and machine language programming. When false, they all return 0.

The function isdigit() doesn't return a bool, it returns an int. If the result is not a digit, isdigit() will return 0. But, when the char is a digit, isdigit() returns 2048.

Next, you have to understand how true and false are evaluated numerically. false is always evaluated as 0 and 0 is always evaluated as false.

True is a little different. Any non-zero number evaluates as true, even negative numbers. If it isn't 0, it's true. However, true only evaluates as 1.

So here's why your test is always failing. It is checking to see if the return value of isdigit() equals true. In other words, it is doing one of two comparisons. When it's not a digit, is 0 == 1? When it is a digit, is 2048 == 1? Both tests fail.

So, knowing that a non-zero value always evaluates to true, you can use the following. It's also more efficient because it only checks one value. It doesn't have to cast two values and compare them. (You'll learn about casting later, if you haven't already.)

if( isdigit(argv[1][i]) )

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you @Cliff B. (And, wow: this was my first time posting a question on StackExchange, and I'm so impressed that folks take the time to explain!) I was able to alter my code by asking if (is digit... == 0) and it seems to be compiling/running correctly. Hope my problem and Cliff's explanation are helpful to others!
    – LippStick
    Commented Sep 11, 2019 at 19:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .