0
int ln;
printf("how many letters are your name?\n");
scanf("%d", &ln);
printf("Ok...enter your name by characters: \n");
char name[ln];
for (int i = 0; i<=ln; i++){
    scanf("%s", &name[i]);
}

This code should transform a name in the array but the for-loop never ends. Someone who can help me?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Jack_01
  • 17
  • 3

5 Answers5

1

%s is scanning string so technically your whole name is considered as one element. So have to enter many strings . just replace %s by %c and code should be ready to use

Champion
  • 11
  • 2
0

Why for loop never ends?

Inside the loop, scanf("%s", &name[i]); attempts to read a name ln + 1 times, eventually attempting to save data outside name[] bounds.

Saving data outside name[] bounds is undefined behavior (UB). Anything may happen.


Loop not needed to read one line of input as a name and name[] too small.

//char name[ln];
//for (int i = 0; i<=ln; i++){
//   scanf("%s", &name[i]);
//}

char name[ln+1];  // One more for a \0
if (scanf("%s", &name[i]) ==1) {
  Success();
}

Recommend to not use scanf() and use fgets().

char name[ln+1+1]; / One more for a \n and 1 for a \0
if (fgets(name, sizeof name, stdin)) {
  name[strcspn(name, "\n")] = '\0';  // lop off potential \n
  Success();
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

in the for condition, try to remove that equality and see, because you are allocating ln bytes in name variable, so the variable i will start from 0 and go to ln-1

for (int i = 0; i<ln; i++){
       scanf("%c", &name[i]);
   }

also note im using %c here to scan character, %s scans string.

rohit901
  • 110
  • 11
0

Because your for loop must run one more time to end . simply change

i<=ln

to

i<ln
0

"Why for loop never ends?"

First things first, I do not think that the loop is infinite. For me it seems that you are just confusing %s with %c and interpret the waiting for more input as loop that "never ends".

You didn´t provided sufficient ressources to rebuild your issue, though.

But let´s get started:

  1. The %s conversion specifier of scanf() is for reading a string, not a single character. If you want to read a single character per iteration, use scanf("%c", &name[i]); or name[i] = getchar(); instead of scanf("%s", &name[i]); in the for loop.

  2. Your loop condition i <= ln is faulty, as you attempt to write one character more than expected with it, because index counting starts at 0, not 1. Use i < ln instead.

  3. A string needs to have a terminating null character at the end. name needs to have one element more than to just hold the letters of the input name. Also place name[ln] = '\0'; after the loop to insert a null character in the last element of VLA name.


Note:

If you want to read a variable amount of characters from stdin which is determined at run-time, you don´t need to read each character separate and stop until the counter reaches ln - 1.

Reading a string with a variable amount of characters is one point, where fgets() is more appropriate than scanf().

Use fgets (name, sizeof(name), stdin); which ensures that the read string plus the string-terminating null character will fit exactly into name and catch the name once.

int ln;

printf("How many letters are in your name?\n");
scanf("%d", &ln);
getchar();              // catching left newline from scanf.

char name[ln + 1];      // +1 for null character.

printf("Ok...enter your name: \n");

fgets(name, sizeof(name), stdin);   // Will read the name and append the null character.

getchar();              // catching left newline from fgets. 
   
Community
  • 1
  • 1