
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return values of printf() and scanf() in C
The printf() and scanf() functions are required for output and input respectively in C. Both of these functions are library functions and are defined in the stdio.h header file.
Details about the return values of the printf() and scanf() functions are given as follows −
The printf() function
The printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value.
A program that demonstrates this is as follows −
Example
#include <stdio.h> int main(){ char str[] = "THE SKY IS BLUE"; printf("\nThe value returned by printf() for the above string is : %d", printf("%s", str)); return 0; }
Output
The output of the above program is as follows −
THE SKY IS BLUE The value returned by printf() for the above string is : 15
Now let us understand the above program.
First, the string is initialized. Then the string is displayed using printf() as well as the value returned by printf(). The code snippet that shows this is as follows −
char str[] = "THE SKY IS BLUE"; printf("\nThe value returned by printf() for the above string is : %d", printf("%s", str));
The scanf() function
The scanf() function is used for obtaining the input from the user. It returns the number of input values that are scanned. If there is some input failure or error then it returns EOF (end-of-file).
A program that demonstrates this is as follows −
Example
#include int main(){ int x, y, z; printf("The value returned by the scanf() function is : %d", scanf("%d%d%d", &x, &y, &z)); printf("\nx = %d", x); printf("\ny = %d", y); printf("\nz = %d", z); return 0; }
Output
The output of the above program is as follows −
7 5 4 The value returned by the scanf() function is : 3 x = 7 y = 5 z = 2
Now let us understand the above program.
There are 3 int variables i.e. x, y and z. Their values are entered by the user using the scanf() function and the return value of scanf() is printed. The code snippet that shows this is as follows −
int x, y, z; printf("The value returned by the scanf() function is : %d", scanf("%d%d%d", &x, &y, &z));
Then the values of x, y and z that were obtained from the user are printed. The code snippet that shows this is as follows −
printf("\nx = %d", x); printf("\ny = %d", y); printf("\nz = %d", z);
- Related Questions & Answers
- scanf() and fscanf() in C
- Difference between scanf() and gets() in C
- printf(), sprintf() and fprintf() in C
- C function argument and return values
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- Execution of printf with ++ operators in C
- What is the difference between printf() and cout in C++?
- What is the use of `%p` in printf in C?
- Find and return array positions of multiple values JavaScript
- printf() function in PHP
- Return the fractional and integral parts of array values in Numpy
- What is the use of %n in printf()?
- What are printf conversion characters and their types?
- Date Formatting Using printf
- Return the discrete linear convolution of two one-dimensional sequences and return the middle values in Python