English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
ofprintf()
andscanf()
method requires output and input respectively in these functions in C. Both are library functions defined in the stdio.h header file.
relatedprintf()
andscanf()
The details of the return values of the function are as follows-
printf()
methodTheprintf()
method is used to print output. It returns the number of characters printed. If there is an error, it returns a negative value.
A program demonstrating this is as follows-
#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 Result
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's understand the above program.
First, the string is initialized. Then usingprintf()
and the returned value displays the stringprintf()
. The code block showing 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));
scanf()
methodThescanf()
method is used to get user input. It returns the number of input values scanned. If there are some input failures or errors, it returns EOF (end of file).
A program demonstrating this is as follows-
#include int main() { int x, y, z; printf("The value returned by the printf() function for the above string is: %d", scanf("%d%d%d", &x, &y, &z)); printf("\nx = %d", x); printf("\ny = %d", y); printf("\nz = %d", z); return 0; }
Output Result
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's understand the above program.
has3three int variables, namely x, y, and z. The user usesscanf()
method is used to input its value,scanf()
and print the returned value. The code block showing 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 print the x, y, and z values obtained from the user. The code block showing this is as follows-
printf("\nx = %d", x); printf("\ny = %d", y); printf("\nz = %d", z);