1

Can anyone tell me why this code crashes? It's simple, if the length of the string is > than 16, ask again for a string. It works if I write control = 1 inside the if statement, but it should work the same without it, 'cause the value of control at that point is 1, am I right? thans (I'm learning)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(void)
{
    int control = 1;
    char word[16] ;
    printf("Enter a word: ");

    while(control == 1)
    {
        scanf("%s", word);

        int len = strlen(word);
        printf("Lenght is: %d\n", len);

        if (len >= 16) 
        {
            printf("Word lenght to long, enter a new one: ");
        }

        else
        {
            control = 0;
        }

    }
    printf("This is the word: %s\n", word );

}
jotape
  • 319
  • 2
  • 6
  • 14

3 Answers3

8

char word[16] allocates 16 bytes of store for a string.

scanf() then reads a string into that store.

If you read in more than the amount of allocated store, memory is corrupted after the end of the store.

That's why you crash.

  • 3
    Also remember that the terminating NULL character counts as one of those 16 chars that have been allocated. – Code-Apprentice Aug 05 '12 at 22:54
  • Sure wish SO would only count having the best answer as say 10 points, rather than 10 points per upvote. If I write an answer to a lock-free problem, which takes many years of experience, I get maybe one or two votes, because who else knows? I write a simple homily on a basic C questions, hey, eight votes. This is beer'n'pretzels voting. It's not actually reflecting anything much other than a feel-good factor for the recipient. –  Aug 05 '12 at 23:33
  • Also consider the more esoteric Q&As have less viewers, thus less votes. – Code-Apprentice Aug 05 '12 at 23:43
2

The problem is that if the user types more than the 15 characters which you have allocated space for, then the computer will merrily write all of them in memory past the end of your array. This will result in "undefined behavior" including crashing your program.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

As others have noted, your fundamental problem is that you're allocating 16 characters for the string, and scanf will happily allow you to write past those 16 characters into memory that doesn't belong to you.

Be aware that C will allow you to do this with arrays generally, and understand how standard C strings work: you need to null-terminate them, meaning that you'll always need an extra space in the array for a null-terminating character \0.

There is a way to limit scanf with respect to C strings, using a field width specifier with %s, like so:

char input[17];  // room for 16 characters plus null-terminator

// here scanf will stop after reading 16 characters:
scanf("%16s", input);

With this code, you can safely use scanf to fill your string with no more than 16 characters, and scanf will null-terminate the string for you.

But as others have also noted, scanf is pretty poor at handling user input. It's usually better to use fgets and manage the input string on your own, piece-by-piece.

pb2q
  • 58,613
  • 19
  • 146
  • 147