printf函数原型
int printf(const char * p,...);
返回值 返回值为整型,是输出字符的个数,即格式化字符串中字符的个数。
举例
int main()
{
int count = printf("ni hao csdn,%d\n",111);
//这里的格式化字符串包括逗号、空格、换行符、字母及数字共计16个字符
printf("n = %d\n",n);
}
输出结果
n = 16;
一个面试题
int main()
{
printf("%d\n",printf("%d\n",printf("hello!\n")));
}
这样的题目第一眼看上去有一点懵,其实它考的就是printf函数的返回值,及输出字符的个数。 输出结果
//由内而外依次分析
printf("hello!\n");
//在屏幕上输出hello!并进行换行,其返回值为7。
printf("%d\n",printf("hello!\n"));
/*由上一步可以知道其实 printf("hello!\n") == 7 所以输出7并进行换行*/
printf("%d\n",printf("%d\n",printf("hello!\n")));
//尽管这一行很长,但是经过简化,他可以这样表示
printf("%d\n",2);
//因为printf("%d\n",printf("hello!\n"))的返回值为2
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有