Why program stops after reading into an array?

Hi,
I'm just trying to figure out how to read into an array (word[5]) properly and there is something that I don't understand.

My program does simple thing - reads a word from user and then changes something in it.
But my concern is - let's say C = 3, I'm inputing words in this order:

cat
cool
tree

Everything works fine, but again: C = 3:

cat
horse //BOOM.

Process returned 0, program ended.

I know that the array holds only 4 letters and the last space is for the newline character, but I don't know why the program stops after inputing this one letter too many. Why it does not read the last 3rd word? Even in scanf I wrote that it should read only first 4 letters, but it didn't change this problem.
If there is something that I understand badly, please correct me.

Also, I know this is C++ forum but I believe there are people who know C language very well too.
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int C, i;
    char word[5];                 
    printf("Number of words: ");  

    do{
    scanf( " %d", &C);
    if(C<1 || C>50)
        printf("Bad value!\n");
    }while(C<1 || C>50);


    for(i=0; i<C; ++i)
    {
    scanf("%4s", &word);
    shorten(word);
    }

    return 0;
}
Last edited on
Hello obeeey,

First question is this a C program or should it be a C++ program?

Next there is not enough of the program to compile and test it. I have to guess at what is missing.


I know that the array holds only 4 letters and the last space is for the newline character, but I don't know why the program stops after inputing this one letter too many. Why it does not read the last 3rd word? Even in scanf I wrote that it should read only first 4 letters, but it didn't change this problem.
If there is something that I understand badly, please correct me.


A C-string ends with the terminating null (\0) not a new line (\n).
I believe you have answered your own question. "word" has s size of five. That is four letters and the terminating null (\0). Try increasing the size of the array to 10 and see what happens.

It has been a long time since I worked with C, so I will have to check into the scanf("%4s", &word); to see how it works.

After I get the programming running I will have a better idea.

Hope that helps,

Andy
It's a weirdness of entering an overly long word on the N-1th iteration.

The program doesn't stop or crash, it just carries on, consumes the rest of the input, which just happens to be the last thing it would do anyway.

Eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int C, i;
    char word[5];
    printf("Number of words: ");

    do{
    scanf( " %d", &C);
    if(C<1 || C>50)
        printf("Bad value!\n");
    }while(C<1 || C>50);


    for(i=0; i<C; ++i)
    {
    scanf("%4s", word);  //!! no & here
    printf("Entered=%s=\n",word);
    //shorten(word);
    }

    return 0;
}

$ ./a.out 
Number of words: 3
horse
Entered=hors=
Entered=e=
cat
Entered=cat=

vs

$ ./a.out 
Number of words: 3
cat
Entered=cat=
horse
Entered=hors=
Entered=e=

Topic archived. No new replies allowed.