Say*_*Pal 3 c python python-3.x
打印功能所做的事超出了预期,并且这种行为因语言而异。请查看给定的代码。
Python 3代码:
n=print("Interesting")
print(n)
Run Code Online (Sandbox Code Playgroud)
输出:
Interesting
None
Run Code Online (Sandbox Code Playgroud)
C代码:
#include<stdio.h>
int main(){
int n = printf("Interesting");
printf("\n%d",n);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Interesting
11
Run Code Online (Sandbox Code Playgroud)
我希望输出是某种错误,但是两种语言对它的处理方式不同。请解释为什么会发生这种情况,并且打印功能除了显示以外还能执行其他操作吗?
print并且printf是可以具有返回值的函数。在Python中,print只需返回None。
在C中,的签名printf为int printf( const char* format, ... );。它返回一个等于输出的字符数的整数。返回值为负表示发生错误。