I know there are many questions with similar titles, I have not found the answer to my problem in any of them.
This is part of an assingment in a C programming workshop. I need to first get an integer from the user specifying the length of a string that I get subsequently. The existing answers deal either with a situation where the maximal input size is known at compilation time or with cases where the input can be arbitrarily large (which make for very complicated solution)
My attempt:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
char *input =(char*)malloc(sizeof(char)*(n + 1));
fgets(input, n, stdin);
input[n] = '\0';
printf("%s", input);
return 0;
}
This prints nothing, how come?