The Wayback Machine - https://web.archive.org/web/20241122155331/https://www.geeksforgeeks.org/g-fact-11/
Open In App

What is return type of getchar(), fgetc() and getc() ?

Last Updated : 28 May, 2017
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In C, return type of getchar(), fgetc() and getc() is int (not char). So it is recommended to assign the returned values of these functions to an integer type variable.




char ch;  /* May cause problems */  
while ((ch = getchar()) != EOF) 
{
   putchar(ch);
}


Here is a version that uses integer to compare the value of getchar().




int in;  
while ((in = getchar()) != EOF) 
{
   putchar(in);
}


See this for more details.


Previous Article
Next Article

Similar Reads

Difference between getc(), getchar(), getch() and getche()
All of these functions read a character from input and return an integer value. The integer is returned to accommodate a special value used to indicate failure. The value EOF is generally used for this purpose. getc() It reads a single character from a given input stream and returns the corresponding integer value (typically ASCII value of read cha
2 min read
EOF, getc() and feof() in C
In C/C++, getc() returns the End of File (EOF) when the end of the file is reached. getc() also returns EOF when it fails. So, only comparing the value returned by getc() with EOF is not sufficient to check for the actual end of the file. To solve this problem, C provides feof() which returns a non-zero value only if the end of the file has reached
3 min read
getchar Function in C
C getchar is a standard library function that takes a single input character from standard input. The major difference between getchar and getc is that getc can take input from any no of input streams but getchar can take input from a single standard input stream. It is defined inside the <stdio.h> header file.Just like getchar, there is also
3 min read
Implicit return type int in C
Predict the output of following C program. #include <stdio.h> fun(int x) { return x*x; } int main(void) { printf("%d", fun(10)); return 0; } Output: 100 The important thing to note is, there is no return type for fun(), the program still compiles and runs fine in most of the C compilers. In C, if we do not specify a return type, com
1 min read
Difference between Type Casting and Type Conversion
1. Type Casting: In typing casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In typing casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that's why it is also called narrowing conversion. Synta
3 min read
Return values of printf() and scanf() in C/C++
What values do the printf() and scanf() functions return ? printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error Example 1: The printf() function in the code written below returns 6. As 'CODING' contains 6 characters. C/C++ Code // C/C++ program to demonstrate return value // of printf()
2 min read
What does main() return in C and C++?
C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our thinking about need of return 0 statement in our co
3 min read
Difference between return and printf in C
In C programming, return and print serve fundamentally different purposes and they are used in distinct contexts to achieve specific tasks. Let's see each of them and see what are their functions and differences. 1. Return Statement In C, return is a statement used within a function to terminate the function's execution and return a value to the ca
2 min read
C Function Arguments and Function Return Values
Prerequisite: Functions in C A function in C can be called either with arguments or without arguments. These functions may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values. Hence the function prototype of a function
6 min read
How can I return multiple values from a function?
We all know that a function in C can return only one value. So how do we achieve the purpose of returning multiple values. Well, first take a look at the declaration of a function. int foo(int arg1, int arg2); So we can notice here that our interface to the function is through arguments and return value only. (Unless we talk about modifying the glo
2 min read
Return Statement vs Exit() in main() in C++
The return statement in C++ is a keyword used to return the program control from the called function to the calling function. On the other hand, the exit() function in C is a standard library function of <stdlib.h> that is used to terminate the process explicitly. The operation of the two may look different but in the case of the main() funct
3 min read
How to return multiple values from a function in C or C++?
New programmers are usually in the search of ways to return multiple values from a function. Unfortunately, C and C++ do not allow this directly. But fortunately, with a little bit of clever programming, we can easily achieve this. Below are the methods to return multiple values from a function in C: By using pointers.By using structures.By using A
7 min read
Return Statement in C
Pre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void returns nothing, etc. In C, we can only return a single
4 min read
How to Pass or Return a Structure To or From a Function in C?
A structure is a user-defined data type in C. A structure collects several variables in one place. In a structure, each variable is called a member. The types of data contained in a structure can differ from those in an array (e.g., integer, float, character). Syntax: struct geeksforgeeks { char name[20]; int roll_no; char branch[20]; } How to Pass
3 min read
Return From Void Functions in C++
Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but they can do return values. Some of the cases are list
2 min read
Data type of character constants in C and C++
In C, data type of character constants is int, but in C++, data type of same is char. If we save below program as test.c then we get 4 as output (assuming size of integer is 4 bytes) and if we save the same program as test.cpp then we get 1(assuming size of char is 1 byte) C/C++ Code // C++ program demonstrating that data type of character // const
1 min read
C | Storage Classes and Type Qualifiers | Question 1
Which of the following is not a storage class specifier in C? (A) auto (B) register (C) static (D) extern (E) volatile Answer: (E)Explanation: volatile is not a storage class specifier. volatile and const are type qualifiers. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
C | Storage Classes and Type Qualifiers | Question 3
#include <stdio.h> int main() { static int i=5; if (--i){ printf("%d ",i); main(); } } (A) 4 3 2 1 (B) 1 2 3 4 (C) 4 4 4 4 (D) 0 0 0 0 Answer: (A) Explanation: Since i is static variable, it is shared among all calls to main(). So is reduced by 1 by every function call.Quiz of this Question
1 min read
C | Storage Classes and Type Qualifiers | Question 19
#include <stdio.h> int main() { int x = 5; int const * ptr = &x; ++(*ptr); printf("%d", x); return 0; } (A) Compiler Error (B) Runtime Error (C) 6 (D) 5 Answer: (A) Explanation: See following declarations to know the difference between constant pointer and a pointer to a constant. int * const ptr —> ptr is constant pointer. Y
1 min read
C | Storage Classes and Type Qualifiers | Question 6
#include<stdio.h> int main() { typedef static int *i; int j; i a = &j; printf("%d", *a); return 0; } (A) Runtime Error (B) 0 (C) Garbage Value (D) Compiler Error Answer: (D) Explanation: Compiler Error -> Multiple Storage classes for a. In C, typedef is considered as a storage class. The Error message may be different on diff
1 min read
C | Storage Classes and Type Qualifiers | Question 7
Output? #include<stdio.h> int main() { typedef int i; i a = 0; printf("%d", a); return 0; } (A) Compiler Error (B) Runtime Error (C) 0 (D) 1 Answer: (C) Explanation: There is no problem with the program. It simply creates a user defined type i and creates a variable a of type i.
1 min read
C | Storage Classes and Type Qualifiers | Question 8
#include<stdio.h> int main() { typedef int *i; int j = 10; i *a = &j; printf("%d", **a); return 0; } (A) Compiler Error (B) Garbage Value (C) 10 (D) 0 Answer: (A) Explanation: Compiler Error -> Initialization with incompatible pointer type. The line typedef int *i makes i as type int *. So, the declaration of a means a is poi
1 min read
C | Storage Classes and Type Qualifiers | Question 9
Output? #include <stdio.h> int fun() { static int num = 16; return num--; } int main() { for(fun(); fun(); fun()) printf("%d ", fun()); return 0; } (A) Infinite loop (B) 13 10 7 4 1 (C) 14 11 8 5 2 (D) 15 12 8 5 2 Answer: (C) Explanation: Since num is static in fun(), the old value of num is preserved for subsequent functions calls.
2 min read
C | Storage Classes and Type Qualifiers | Question 19
#include <stdio.h> int main() { int x = 10; static int y = x; if(x == y) printf("Equal"); else if(x > y) printf("Greater"); else printf("Less"); return 0; } (A) Compiler Error (B) Equal (C) Greater (D) Less Answer: (A) Explanation: In C, static variables can only be initialized using constant literals. This is
1 min read
C | Storage Classes and Type Qualifiers | Question 11
Consider the following C function int f(int n) { static int i = 1; if (n >= 5) return n; n = n+i; i++; return f(n); } The value returned by f(1) is (GATE CS 2004) (A) 5 (B) 6 (C) 7 (D) 8 Answer: (C) Explanation: Since i is static, first line of f() is executed only once. Execution of f(1) i = 1 n = 2 i = 2 Call f(2) i = 2 n = 4 i = 3 Call f(4) i
1 min read
C | Storage Classes and Type Qualifiers | Question 12
In C, static storage class cannot be used with: (A)Global variable (B)Function parameter (C)Function name (D)Local variable Answer: (B)Explanation: Declaring a global variable as static limits its scope to the same file in which it is defined. A static function is only accessible to the same file in which it is defined. A local variable declared as
1 min read
C | Storage Classes and Type Qualifiers | Question 19
Output? (GATE CS 2012) #include <stdio.h> int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "\n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" \n %d %d " , a, b); } (A) 3 1 4 1 4 2 (B) 4 2 6 1 6 1 (C
2 min read
C | Storage Classes and Type Qualifiers | Question 19
Output? #include <stdio.h> int main() { register int i = 10; int *ptr = &i; printf("%d", *ptr); return 0; } (A) Prints 10 on all compilers (B) May generate compiler Error (C) Prints 0 on all compilers (D) May generate runtime Error Answer: (B) Explanation: See point 1 of Register KeywordQuiz of this Question
1 min read
C | Storage Classes and Type Qualifiers | Question 19
#include <stdio.h> int main() { extern int i; printf("%d ", i); { int i = 10; printf("%d ", i); } } (A) 0 10 (B) Compiler Error (C) 0 0 (D) 10 10 Answer: (B) Explanation: See extern keywordQuiz of this Question
1 min read
C | Storage Classes and Type Qualifiers | Question 17
Output? #include <stdio.h> int main(void) { int i = 10; const int *ptr = &i; *ptr = 100; printf("i = %d\n", i); return 0; } (A) i = 100 (B) i = 10 (C) Compiler Error (D) Runtime Error Answer: (C) Explanation: Note that ptr is a pointer to a constant. So value pointed cannot be changed using the pointer ptr. See Const Qualifier i
1 min read
three90RightbarBannerImg