0

I'm doing some tests with a user inputting strings into my program through the command line, but I'm confused by something. When I specify the maximum size of a char[] array, the program is still somehow making the char[] longer than the max length I specified, with no errors. Why is this the case? And why does sizeof(input) show 5 bytes, despite input containing 14 characters?

#define MAX_LENGTH 5

int main(){

    char input[MAX_LENGTH];
    printf("Max length: %d\n", MAX_LENGTH);
    printf("strlen(input) = %d\n", strlen(input));
    printf("sizeof(input) = %d\n", sizeof(input));

    printf("Enter string: ");
    scanf(" %s", input);

    printf("strlen(input) = %d\n", strlen(input));
    printf("sizeof(input) = %d\n", sizeof(input));
    printf("Input: %s", input);

    return 0;
}

I would expect this to either scan the first 5 characters of the user's input; but instead, it gives input the entirety of what is typed...?

cmd line output:

Max length: 5
strlen(input) = 0
sizeof(input) = 5
Enter string: foobarpizzapie
strlen(input) = 14
sizeof(input) = 5
Input: foobarpizzapie
Drakinite
  • 363
  • 1
  • 13
  • 2
    `char input[MAX_LENGTH];` is uninitialized, calling `strlen` on it invokes *undefined behavior* – UnholySheep Feb 18 '19 at 18:50
  • 1
    Also, entering a string that's larger than `MAX_LENGHT-1` via `scanf` is *undefined behavior* – UnholySheep Feb 18 '19 at 18:50
  • Can you explain what you mean by undefined behavior? Like, if it's "undefined behavior" why does the program still run without crashing? – Drakinite Feb 18 '19 at 18:52
  • 1
    When your code invokes *undefined behavior* the compiler is free to produce a program that does *anything* - that includes appearing to run correctly. Or it might crash or delete your entire harddrive – UnholySheep Feb 18 '19 at 18:53
  • Yeesh, thanks for the explanation. Is there a way to prevent this from happening; that is, in this case, ensuring that the user's input is smaller than `MAX_LENGTH`? – Drakinite Feb 18 '19 at 18:58
  • 1
    Replace `scanf(" %s", input);` with `fgets(input, MAX_LENGTH, stdin)` see also: https://stackoverflow.com/questions/4023895/how-do-i-read-a-string-entered-by-the-user-in-c – UnholySheep Feb 18 '19 at 19:02
  • This example shows how to set a maximum number of characters to read in the scanf: http://www.cplusplus.com/reference/cstdio/scanf/#example - replace %s with %NNs where NN is 1 less than the buffer size. – Gem Taylor Feb 18 '19 at 19:21
  • regarding: `scanf(" %s", input);` when calling any of the `scanf()` family of functions 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input format specifiers '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is one less than the length of the input buffer because those specifiers always append a NUl byte to the input. This also avoids undefined behavior from overflowing the input buffer – user3629249 Feb 19 '19 at 02:33
  • regarding: `printf("strlen(input) = %d\n", strlen(input));` this results in undefined behavior! The buffer `input[]` contains garbage (because it has not been initialized) so it is unknown where the call to `strlen()` will encounter a NUl byte. Remember that `strlen()` stops when it encounters a NUL byte and does not count the NUL byte – user3629249 Feb 19 '19 at 02:36

0 Answers0