-1

I'm trying to write a function that outputs an input string 10 times. Here is the code.

#include <stdio.h>

int print10times(){

    char input[] = "";
    printf("Which string would you like to print 10 times? ");
    scanf("%s", input);

    for (int i=0; i<10; i++){
        printf("%s\n", input);
    }

    return 0;
}

int main(){

    print10times();
    return 0;
    
}

The output I'm getting looks like this:

Which string would you like to print 10 times? Hello
H
H☺
H☻
H♥
H♦
H♣
H♠
H
H
H

Could someone let me know why this doesn't print "Hello" 10 times?

Thanks.

Alan N
  • 21
  • 6
  • 4
    It's not printf that's the problem - your scanf is UB due to buffer overflow. I can't find an exact duplicate question, but some advanced information is at https://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c – o11c Jul 09 '21 at 03:27
  • 3
    prepare more space for input. like `char input[50]`. and `scnaf("%49s", input)` – ianfun Jul 09 '21 at 03:27
  • @ianfun Thanks, putting a value in the input[] did the trick. Just curious, how much space does the declaration of a char without a value have by default? Also, when I had the char input[] like the original, I can printf the entire "Hello" string when outside the for loop. Do you know why that is? – Alan N Jul 09 '21 at 03:31
  • 5
    Your declaration `char input[] = "";` allocated space for a string of up to 0 characters. :-) (Plus trailing `\0` byte, so the array will have a total size of 1.) – Steve Summit Jul 09 '21 at 03:33
  • 3
    "*I can printf the entire "Hello" string when outside the for loop. Do you know why that is?*. Reading/writing invalid memory is Undefined Behaviour. UB means the result is unpredictable including crash, wrong values, appear to "work", etc. [How dangerous is it to access an array out of bounds?](https://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – kaylum Jul 09 '21 at 03:34
  • `char input[] = "XYZ";` allocates enough space for the initializer - it is equivalent to `char input[sizeof("XYZ")] = "XYZ";` - in that example the array has a size of 4 because there are three characters plus the '\0' terminator. So `char input[] = "";` allocates enough space for 0 characters plus the '\0' terminator. – Jerry Jeremiah Jul 09 '21 at 04:00

2 Answers2

2

You declare char input[] = "";, which makes input a string of length 1 (containing only the terminating '\0'). It will not magically grow when you try to read more than zero characters into it, and instead you will access memory behind it (potentially destroying its content, crashing, or worse).

For example, declare it char input[80] = ""; instead, and you have space for up to 79 characters. This is still a potential issue, as your typed input could be longer, but it will work for up to 79 chars.

Aganju
  • 6,295
  • 1
  • 12
  • 23
0

You have to give more space to input[] because char input[]=""; as you know that char take only 1 byte so in this case it reads only h of "hello" okk because there already a character called null character which also consume spaces if you want allocate memory at run time so you can use dynamic memory functions.. Overall just replace char input[]=""; with char input[50]; simple.....