为什么4出现在输出中? [英] Why is 4 coming in the output?

查看:77
本文介绍了为什么4出现在输出中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main(){
int k=55,l;
l=(k==65?printf("k=65"):printf("k!=65"));
printf("\n\n%d\n",l);
return 0;
}





我的尝试:



输出:





k = 65

4



//为什么这个输出?



What I have tried:

output:


k=65
4

//why this output?

推荐答案

它没有。

它打印:

It doesn't.
It prints:
k!=65

5

试试看看!

但如果这样line

Try it and see!
But if this line

int k=55,l;

更改为

Changed to

int k=65,l;



它会。



因为 printf 返回它打印的字符数,用于加载 k 然后 l

实际上你写的是:


It would.

Because printf returns the number of characters it printed, which you use to load k and then l
Effectively what you wrote was:

int main()
    {
    int k=55,l;
    if (k==65)
        k = printf("k=65");
    else
        k = printf("k!=65");
    l = k;
    printf("\n\n%d\n",l);
    return 0;
    }

更准确地写出你所做的是:

A clearer way to wrote exactly what you did would be:

int main()
    {
    int k=55,l;
    l = k = printf(k == 65 ? "k=65" : "k!=65");
    printf("\n\n%d\n",l);
    return 0;
    }


这篇关于为什么4出现在输出中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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