0

I feel stupid, why is not printing "ROFL" in this program? I am testing for the crack.c program:

#include <stdio.h>
#include <cs50.h>

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

int main (int argc, string argv[])
{
    char key[] = {'R', 'O', 'F', 'L'};
    printf("key: %s\n", key);
}

here what it prints out: ~/workspace/pset2/ $ ./crack2 50324 key: ROFLh.a ~/workspace/pset2/ $ ./crack2 50324 key: ROFL,

0

A string in c must end with a special character that informs the end of the string, this character is '\ 0', otherwise we run the risk of going beyond the end of the array and printing garbage, as in this case. The solution is simple:

char key[] = {'R', 'O', 'F', 'L, '\0'};

You must log in to answer this question.

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