[C] difference between getchar ,scanf etc

  • Thread starter Thread starter jd12345
  • Start date Start date
  • Tags Tags
    Difference
Click For Summary
SUMMARY

The discussion centers on the differences between the C standard input functions: getchar(), scanf(), and getc(). While getchar() reads input character by character and is often used for low-level input, scanf() scans formatted input based on specified fields, such as integers. The ctrl+D command signals the end of input for all functions, but getchar() processes characters before returning EOF, while scanf() returns EOF if it cannot parse input. The functions getc() and fgetc() serve similar purposes, with getc() reading from a specified file stream and fgetc() being its equivalent.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with standard input/output operations in C
  • Knowledge of formatted input parsing using scanf()
  • Basic concepts of file handling in C
NEXT STEPS
  • Learn the differences between getchar() and getc() in C
  • Explore advanced input parsing techniques using scanf() with various format specifiers
  • Investigate the use of fgetc() and its applications in file handling
  • Review examples of input handling from the C Standard Library documentation
USEFUL FOR

C programmers, software developers, and students learning about input/output functions in C, particularly those interested in understanding low-level input handling and formatted data parsing.

jd12345
Messages
251
Reaction score
2
What is the difference between both? I tried the standard input and output program using both and only found one difference. Using getchar ctrl+D was the end of file but using scanf ctrl+D did not work. So other than this is there any difference.
Also I read there are others like getc etc. What are they used for?
If they all take input from user and do the same job why are there so many functions for the same thing?
 
Technology news on Phys.org


Hi jd12345!

Ctrl+D works the same for all of them - it ends the standard input (on e.g. Linux).

However, there may or may not still be unread input in the input buffer.
getchar() will read the input buffer character by character.
scanf() will scan the input for the fields that you specify.

If you type a couple of characters, getchar() won't read those immediately.
It will only do so when you press Enter, just like all other functions that work on standard input.

If you type a couple of character and the press ctrl+D, getchar() will still read those characters successfully one by one first, before returning EOF.
Similary, scanf() will scan what it can.
Its return value depends on the situation.
If it could not parse anything successfully, it will return EOF.

The other functions like getc() are slightly different again.
getchar() implicity reads from standard input, while getc() will read from the file stream you specify.
 


I like Serena said:
scanf() will scan the input for the fields that you specify.
What do you mean by that?

I understood how getchar works but I am still not clear about scanf.
Also what is the use of so many functions which do more or less similar things? If you could give an example where one of them is more uselful than the other I think it would help explain me better.
Thank you!
 
scanf("%d %d", &i, &j) scans the input buffer.
It will skip white space and try to find digits.
The first group of digits is stored as an integer in "i".
Then the first field will have been successfully scanned.
This is an advanced method to parse input.

getchar() is a shorthand for getc(stdin).
One might say that getchar() is redundant.
It is a low level function.

In particular there is a set of functions that reads implicitly from stdin.
There is another set of functions, prefixed with "f", that reads from a FILE pointer.

Furthermore fgetc() and getc() are equivalent.
I don't know why both of them exist.
The function getc() should have been defined in the place of getchar() to be consistent.

As for examples, see for instance:
http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/

It shows an example how to use fgetc().
Similarly this same website contains an example for each of the other functions.
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 19 ·
Replies
19
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
86
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 15 ·
Replies
15
Views
6K
Replies
4
Views
2K