Return values of printf(), scanf(), getchar(), putchar()

The return values ​​of printf(), scanf(), getchar(), and putchar() are all int types, so pay attention to this

1. The return value of printf() is the number of printed characters, and the null character '\0' will not be added when printing the string

example:

#include<stdio.h>
int main(void)
{     int n;     n=printf("The number of characters printed is:"); //The return value is the number of printed characters excluding '\0'     printf("% d\n", n);     return n; } The final result is:





The number of characters printed is: 36 2.
The return value of scanf() returns the number of items successfully read

#include<stdio.h>
#include<string.h>
int main(void)  
{     int i,n,m,ch;     ch=scanf("%d %d %d\n", &i, &n, &m) ;     //ch=scanf("%*d %*d %d\n", &i, &n, &m); The modifier * is used here to make scanf() skip the corresponding item printf("%d     \ n",ch);     return ch; } Here // is used to distinguish the results of the two cases, making the effect more obvious:







3
// 1
3. The return value of getchar() is the ASCII code of the first character entered by the user

#include<stdio.h>
int main(void)   
{
    printf("%d\n", getchar());
    return 0;
}
输入:1 2 3

Result: 49 //This is consistent with the ASCLL code of number 1 
4. The return value of putchar() returns the original character, but if a series of characters are input, only the first character will be returned

5. If the four functions of printf(), scanf(), getchar(), and putchar() encounter an error or detect the end of the file (it will be different under different systems), they will return EOF

EOF is a value, or a negative value. Generally, it is defined as -1, but it may be other negative values. It is precisely because EOF is a negative value that the return value of all four functions is int. type

Guess you like

Origin blog.csdn.net/qq_40999917/article/details/124948833