📜  在C / C++中返回printf()和scanf()的值

📅  最后修改于: 2021-05-25 19:35:43             🧑  作者: Mango

printf()scanf()函数返回什么值?

printf():返回已打印的字符总数,如果输出错误或编码错误,则返回负值
示例1:下面编写的代码中的printf()函数返回6。因为“ CODING”包含6个字符。

CPP
// C/C++ program to demonstrate return value
// of printf()
#include 
 
int main()
{
    char st[] = "CODING";
 
    printf("While printing ");
    printf(", the value returned by printf() is : %d",
                                    printf("%s", st));
 
    return 0;
}


CPP
// C/C++ program to demonstrate return value
// of printf()
 
#include 
int main()
{
    long int n = 123456789;
 
    printf("While printing ");
    printf(", the value returned by printf() is : %d",
                                    printf("%ld", n));
 
    return 0;
}


CPP
// C/C++ program to demonstrate return value
// of printf()
 
#include 
int main()
{
    char a[100], b[100], c[100];
 
    // scanf() with one input
    printf("\n First scanf() returns : %d",
                            scanf("%s", a));
 
    // scanf() with two inputs
    printf("\n Second scanf() returns : %d",
                       scanf("%s%s", a, b));
 
    // scanf() with three inputs
    printf("\n Third scanf() returns : %d",
                  scanf("%s%s%s", a, b, c));
 
    return 0;
}


输出
While printing CODING, the value returned by printf() is : 6

示例2:下面编写的代码中的printf()函数返回9。因为’123456789’包含9个字符。

CPP

// C/C++ program to demonstrate return value
// of printf()
 
#include 
int main()
{
    long int n = 123456789;
 
    printf("While printing ");
    printf(", the value returned by printf() is : %d",
                                    printf("%ld", n));
 
    return 0;
}
输出
While printing 123456789, the value returned by printf() is : 9

scanf():返回已成功扫描的输入的总数,如果在分配第一个接收参数之前发生输入失败,则返回EOF。
示例1:下面编写的代码中的第一个scanf()函数返回1,因为它正在扫描1个项目。类似地,第二scanf()在扫描2个输入时返回2,而第三scanf()在扫描3个输入时返回3。

CPP

// C/C++ program to demonstrate return value
// of printf()
 
#include 
int main()
{
    char a[100], b[100], c[100];
 
    // scanf() with one input
    printf("\n First scanf() returns : %d",
                            scanf("%s", a));
 
    // scanf() with two inputs
    printf("\n Second scanf() returns : %d",
                       scanf("%s%s", a, b));
 
    // scanf() with three inputs
    printf("\n Third scanf() returns : %d",
                  scanf("%s%s%s", a, b, c));
 
    return 0;
}
Input:
Hey!
welcome to
geeks for geeks


Output:
 First scanf() returns : 1
 Second scanf() returns : 2
 Third scanf() returns : 3
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。