我如何解释这个简单的 C 代码的输出? [英] How do I explain the output of this simple C code?

查看:53
本文介绍了我如何解释这个简单的 C 代码的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
int i=10;
printf("%d",printf("%d",i));
return(0);
}

Turbo C 输出

102

我是初学者.那么你能解释一下这段代码是如何工作的吗?

I am a beginner. So can you explain how this code works?

推荐答案

<的文档code>printf 声明它将返回一个整数,表示写入输出流的字符数.

The documentation for printf states that it will return an integer that represents the number of characters written to the output stream.

这意味着您可以在另一个对 printf 的调用中使用 printf 的返回值来满足 %d 格式说明符,并且第二个(outer) call 将打印出第一次调用中写入的字符数.

That means you can use the return value of printf to satisfy a %d format specifier in another call to printf, and the second (outer) call will print out the number of characters written in the first call.

i 等于 10,所以第一次调用 printf 输出数字 10 并返回 2(字符串"10"中的字符数),传递给第二次调用printf,打印2,给你最终的输出 102.

i is equal to 10, so the first call to printf outputs the number 10 and returns 2 (number of characters in the string "10"), which is passed to the second call to printf, which prints 2, giving you the final output 102.

这篇关于我如何解释这个简单的 C 代码的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆