Developer community
0 votes
4.8k views
asked by Expert (13.2k points) 27 41 59 | 4.8k 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,

float).

Example:

#include<stdio.h>

int main() {

char a[10];

printf("Enter a: \n");

scanf("%s",a);

return 0;

}

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

Example:

#include<stdio.h>

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) 10 22 34

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked by keem Expert (13.2k points) 27 41 59 | 1.4k views
0 votes
1 answer
asked by keem Expert (13.2k points) 27 41 59 | 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

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users