Is this way of getting a formated input from stdin better than default scanf?

#include <stdio.h>
#include <stdlib.h>
#define BUF_SIZE 1024
int main() {
char *buf = (char*)malloc(BUF_SIZE*sizeof(char));
fgets(buf, BUF_SIZE, stdin);
int a = 0;
sscanf(buf, "%d", &a);
printf("%d\n", a);
free(buf);
return 0;
}
I saw so many people saying to avoid scanf
, so i made this. Is it better for using a string buffer on getting the user input?
I tried to make a better way to get the user input, avoiding scanf
. So, i would like to know if this is really a better option, or if i can make it better.
Answer
I saw so many people saying to avoid scanf, so i made this. Is it better for using a string buffer on getting the user input?
They meant something different. You are stil using scanf
. Avoiding scanf
means: "write your own input parsing function"
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions