条件表达式 [英] conditional expression

查看:71
本文介绍了条件表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下计划:


#include< stdio.h>

#include< stdlib.h>


int

main(无效)

{

int i;

double f;


printf(" size =%u",sizeof((0)?f:i)); / * 0为零* /


putchar(''\ n'');

退出(0);

}


输出:

size = 8

因为''我'应该是一个int,在我的32位系统上,我预计

大小为4个字节。为什么会这样?

consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar(''\n'');
exit(0);
}

output:
size = 8

since ''i'' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?

推荐答案

chandanlinster写道:
chandanlinster wrote:

考虑以下程序:


#include< stdio.h>

#include< stdlib.h>


int

main(无效)

{

int i;

double f;


printf(" size =%u",sizeof((0)?f:i)); / * 0为零* /


putchar(''\ n'');

退出(0);

}


输出:

size = 8

因为''我'应该是一个int,在我的32位系统上,我预计

大小为4个字节。为什么会这样?
consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar(''\n'');
exit(0);
}

output:
size = 8

since ''i'' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?



sizeof运算符有点特殊,它的参数未被评估。


给定表达式,sizeof计算

表达式的结果类型的大小(不评估表达式)。


在这种情况下,?:的2.操作数是一个int,3.是双倍的,所以

转换规则会将结果类型声明为double - 即你计算大小的

类型。

The sizeof operator is a bit special, it''s arguments are not evaluated.

Given an expression, sizeof computes the size of the result type of that
expression (without evaluating the expression).

In this case, the 2. operand of ?: is an int, the 3. is a double so the
conversion rules would state the result type to be a double - that''s the
type you compute the size of.


chandanlinster写道:
chandanlinster wrote:

考虑以下程序:


#include< stdio.h>

#include< stdlib.h>


int

main(void)

{

int i;

double f;


printf(" size =% u,sizeof((0)?f:i)); / * 0为零* /


putchar(''\ n'');

退出(0);

}


输出:

size = 8

因为''我'应该是一个int,在我的32位系统上,我预计

大小为4个字节。为什么会这样?
consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar(''\n'');
exit(0);
}

output:
size = 8

since ''i'' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?



因为在你的系统上`sizeof(double)`是8(而且你运气不好用

printf格式化)。


类型`(无论如何)? f:我是'双';无论什么是什么,

的价值都没关系。


-

Chris" seeker" Dollin

人们是设计的一部分。忘记这一点是危险的。 / Star Cops /

Because on your system `sizeof (double)` is 8 (and you were unlucky with
the printf formatting).

The type of `(whatever) ? f : i` is `double`; it doesn''t matter what the
value of `whatever` is.

--
Chris "seeker" Dollin
"People are part of the design. It''s dangerous to forget that." /Star Cops/


Nils O. Sel?sdal写道:
Nils O. Sel?sdal wrote:

chandanlinster写道:
chandanlinster wrote:

>考虑以下程序:

#include< stdio.h>
#include< stdlib.h> ;

int
main(无效)
{
int i;
double f;

printf(" size =%u",sizeof((0)?f:i)); / * 0为零* /

putchar(''\ n'');
退出(0);
}
输出:
size = 8

因为''我'应该是一个int,在我的32位系统上我预计大小为4个字节。为什么会这样?
>consider the following program:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int i;
double f;

printf("size = %u", sizeof((0)?f:i)); /* 0 is zero */

putchar(''\n'');
exit(0);
}

output:
size = 8

since ''i'' is supposed to be an "int", on my 32-bit system I expected
the size to be 4 bytes. Why does this happen?



sizeof运算符有点特殊,它的参数未被计算。


The sizeof operator is a bit special, it''s arguments are not evaluated.



让我们删除复数,它是一元运算符: - |

Let''s drop the plurals, it''s an unary operator :-|


这篇关于条件表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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