在C中返回printf()和scanf()的值
的printf()和scanf()方法所需的输出和输入分别在这些功能中的C.两者都是库函数和在stdio.h头文件中定义。
有关printf()和scanf()函数的返回值的详细信息如下-
该printf()方法
该printf()方法用于打印输出。它返回打印的字符数。如果有错误,则返回负值。
一个演示这的程序如下-
示例
#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;
}输出结果
上面程序的输出如下-
THE SKY IS BLUE The value returned by printf() for the above string is : 15
现在让我们了解上面的程序。
首先,字符串被初始化。然后使用printf()和返回的值显示字符串printf()。显示此的代码段如下-
char str[] = "THE SKY IS BLUE";
printf("\nThe value returned by printf() for the above string is : %d", printf("%s", str));该scanf()方法
该scanf()方法用于获取用户的输入。它返回扫描的输入值的数量。如果存在某些输入失败或错误,则返回EOF(文件末尾)。
一个演示这的程序如下-
示例
#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;
}输出结果
上面程序的输出如下-
7 5 4 The value returned by the scanf() function is : 3 x = 7 y = 5 z = 2
现在让我们了解上面的程序。
有3个int变量,即x,y和z。用户使用scanf()方法输入其值,scanf()并打印的返回值。显示此的代码段如下-
int x, y, z;printf("The value returned by the scanf() function is : %d",scanf("%d%d%d", &x, &y, &z));然后打印从用户获得的x,y和z值。显示此的代码段如下-
printf("\nx = %d", x);
printf("\ny = %d", y);
printf("\nz = %d", z);