As you can tell by the code (pasted below), my program is supposed to get the hash as a command line argument, and then prompt the user for a "guess" as to what the plaintext password might be. It then takes the guess, runs it through crypt() and compares the result to the original hash. If they are equal, it's supposed to claim "that's the password", if not, it should let the user know they are not equal.

However, when I test using "./crack 50fkUxYHbnXGw" as the command line arguments, and input "rofl" as the guess, the program claims they are not equal even though when it prints the result it is the exact same as the hash.

I've spent an hour trying to figure this out and I haven't made any headway - I'm hoping someone here can point me in the right direction. I'm sure I'm missing something small.


int main(int argc, string argv[1])

    while (argc != 2)
        printf("This program requires one command line argument to run.");
        return 1;

    // initialize hash to a variable
    string hash = argv[1];

    // initialize the known salt to an array
    char salt[3] = "50";
    salt[2] = '\0';

    //get a guess
    printf("Input guess\n");
    string guess = GetString();

    // hash the guess using cryptf
    string result = crypt(guess, salt);
    printf("%s\n", result);
    // compare the result to the hash given
    if (result == hash) { // if the guess is correct print and exit
        printf("%s is the password!\n", guess); 
    else {
        printf("Sorry, %s is NOT the password!\n", guess);

The result of running this program with the aformentioned parameters is:

~/workspace/pset2/crack/ $ ./crack 50fkUxYHbnXGw
Input guess
Sorry, rofl is NOT the password!

1 Answer 1


You should use strcmp, not "==" when comparing two strings in c. The strcmp function, included in string.h takes in two parameters, your two strings, and outputs 0 if the strings are equal. If you need more info, check it out on cs50 reference. Therefore, the problem lies in:

if (result == hash)

Hope this helped!

  • 1
    Hey thanks! That did the trick! So why can't we use "==" to compare strings in c? Jul 16, 2017 at 3:43
  • @PhilBrandvold Well, to fully understand the answer, you'll have to know about c pointers (talked about in psets 4 and 5 a lot), but here goes... So a string in c is not really its own data structure, rather it is just an array of chars. When you initialize a string in c, say, string one = "hello", you actually get passed back a pointer to the first char in the string, in this case, 'h'. A pointer is, basically, an address to the memory you just allocated to make the string. For example, the address to string one could be a number like 5. An address to a string two, which could have the same
    – Jason_V
    Jul 16, 2017 at 4:31
  • @PhilBrandvold content as string one, would still be different, as it is in a different spot in memory. So when you use the == operator, you are actually comparing the pointers of the two strings, which will never be the same, as they are in different spots in memory. That's why someone created the function strcmp to actually compare the chars in a string, rather than the pointers to the first char, which, again, will always be different. Sorry if the explanation confused you even more, but if you want, come back after pset4's lecture and I promise you it will make more sense!
    – Jason_V
    Jul 16, 2017 at 4:36

You must log in to answer this question.

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