My code is producing incorrect result. I don't know why.
When I give the length as 5 and enter the string as "vasanth", this should produce "vasan" as output. But it gives output as "vasa".
#include<stdio.h>
#include<conio.h>
int main(){
int n;
printf("Enter the length of the string : ");
scanf("%d", &n);
getchar();
char array[n];
fgets(array, n, stdin);
fputs(array, stdout);
return 0;
}
The last element is reserved for a terminating null-character. Allocate one more element to fix.
Quote from N1570 7.21.7.2 The fgets function:
Here you will find that the number of characters to read is at most one less than the specified
n.