+ 1
Why do we use putchar() or getchar() for input and output?
In C programming we can use printf for output and scanf for output. Then why do we need getchar() and putchar() or gets and puts for the same thing?
2 Réponses
+ 2
printf outputs formatted string, it can output variable arguments that puts and fputs cannot do. For a regular string literal, printf is slower because it must check each char in the string for % symbol... so puts() is better.
scanf can accept input strings, char, and numbers. It can take multiple values at once. getchar() can only take in one character at a time.
puts() outputs a string with a newline to stdout only. fputs() can output to any file but doesn't add a newline like puts().
gets() is bad practice ,we should never use it because if input is larger than string size it can overflow. Its deprecated so only use fgets() and forget that gets() exists.
+ 2
Abhijnan Saraswat Gogoi
Scanf - accept values,in case of strings it doesn't accept whitespace. While input a sentence it accept only the first word.
Gets - accept whitespace also ,so we can use it at wherever sentence input is required
Getchar - input only single character