Developer community
0 votes
asked by Expert (13.2k points) 29 44 63 | 5.4k views

1 Answer

0 votes
Differences between getchar and scanf functions for reading strings:

scanf getchar

1. Entering of each character should be followed

by return key. 1. Need not type return key.

2. Continuous stream of characters cannot be

directly supplied using scanf function.

2. Continuous stream of characters can be directly supplied

using getchar function

3. Scanf function can be used to provide data at

execution time irrespective of its type(int, char,




int main() {

char a[10];

printf("Enter a: \n");


return 0;


3. getchar() function is used only with character type.



int main() {

char a;

printf("Enter any character: \n");

a = getchar();

printf("Character entered:%c \n",a);

return 0;


4. scanf() returns the number of items read

successfully. A return value 0 indicates that no

fields were read. EOF(end of file) is returned in

case of an error or if end-of-file/end-of-string

character is encountered.

4. getchar() returns the character entered as the value of the

function. It returns EOF in case of an error. It is recommeded

to use getchar instead of scanf.
answered by Guru (39.1k points) 11 24 38

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked by keem Expert (13.2k points) 29 44 63 | 1.5k views
0 votes
1 answer
asked by keem Expert (13.2k points) 29 44 63 | 1.4k views
0 votes
1 answer
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers


10.7k users

3,754 questions
4,046 answers
10,709 users