so i did try it with using a for loop but every single time my output skips the value of index 0 and i just dont understand why.... here is my code :
// take char by char input and print it as string
#include <stdio.h>
void input(){
printf("this program takes char by char input for string\n");
int length;
printf("provide us with the total length of the string : ");
scanf("%d",&length);
char arr[length];
char placeholder;
for(int i=0;i<length;i++){
if(i==length){
arr[i]='\0';
}
printf("input char[%d] : ",i);
scanf("%c",&placeholder);
printf("\n");
arr[i]=placeholder;
}
}
int main(){
input();
}
the output im getting :
this program takes char by char input for string
provide us with the total length of the string : 10
input char[0] : // it got skipped
input char[1] : // this is where i can enter value
if(i==length)This condition cannot possibly be true. It is insidefor(int i=0;i<length;i++)loop. soi<lengthis true inside. You ar reassigningarr[i]anyway later, so this is doubly useless.%donly reads the 10 and leaves the {newline} buffered, so when you subsequently use%cit reads the {newline} for 'char[0]' and then your next input into 'char[1]` etc. I'm sure this is a dupe but don't have one bookmarked. @n.m. also if you did accessarr[length]that would be outside the valid bounds of the array causing Undefined Behaviorscanf. Read input lines withfgets, then parse them.fgets()is weak when reading user input that may contain null characters.scanf("%999[^\n]%n", ...)is a building block to read such. Standard C still lacks a real good line input function.