表达式中的复合语句 [英] Compound statements in expressions

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

问题描述

看看这个C片段:


#include< stdio.h>


int test(int * var)

{

返回(* var + = 5);

}


int main( void)

{

int var;


var = 1;

printf(" %i%i%i \ n",({var + = 4; var;}),

(printf("%i \ n",test(& var)) ,var),

(var + = 10,var - = 4));

}


使用GCC编译时3.3,运行时输出以下内容:

6

10 16 16


现在我想知道 - 这些是C

标准允许的结构类型,还是GCC扩展?如果它们是标准化的,那么

标准化语义。也就是说,他们打算执行哪种顺序,(var + = 4,var)之间的区别是什么?和({var

+ = 4; var;})",以及(expr1,expr2,expr3)"真的意思是什么时候

它不是函数的参数列表。


另外,为什么我必须写({var + = 4; VAR;})" - 为什么我不能跳过外面的

括号?


感谢您的关注!


Fredrik Tolf

Take a look at this C snippet:

#include <stdio.h>

int test(int *var)
{
return(*var += 5);
}

int main(void)
{
int var;

var = 1;
printf("%i %i %i\n", ({var += 4; var;}),
(printf("%i\n", test(&var)), var),
(var += 10, var -= 4));
}

When compiled with GCC 3.3, it outputs the following when run:
6
10 16 16

Now I''m wondering - Are these types of constructs allowed by the C
standard, or are they GCC extensions? If they are standardized, what is
the standardized semantics. That is, in which order are they intended to
be executed, what''s the difference between "(var += 4, var)" and "({var
+=4; var;})", and what does "(expr1, expr2, expr3)" really mean when
it''s not an argument list to a function.

Also, why must I write "({var += 4; var;})" - Why can''t I skip the outer
parentheses?

Thanks for your attention!

Fredrik Tolf

推荐答案

Fredrik Tolf< fr ***** @ dolda2000.com>写道:
Fredrik Tolf <fr*****@dolda2000.com> writes:
看看这个C片段:

#include< stdio.h>

int test(int * var)
{
返回(* var + = 5);
}
int main(void)
{
int var;

var = 1;
printf("%i%i%i \ n",({var + = 4; var;}),
(printf("%) i\\\
,test(& var)),var),
(var + = 10,var - = 4));
}

编译时GCC 3.3,它在运行时输出以下内容:
6
10 16 16
现在我在想 - 这些类型的结构是否被C
所允许标准,还是GCC扩展?
Take a look at this C snippet:

#include <stdio.h>

int test(int *var)
{
return(*var += 5);
}

int main(void)
{
int var;

var = 1;
printf("%i %i %i\n", ({var += 4; var;}),
(printf("%i\n", test(&var)), var),
(var += 10, var -= 4));
}

When compiled with GCC 3.3, it outputs the following when run:
6
10 16 16

Now I''m wondering - Are these types of constructs allowed by the C
standard, or are they GCC extensions?




这是gcc扩展名。


< OT>

如果您的系统支持它,请尝试

info gcc''c extensions''''statement exprs''

< / OT>


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



It''s a gcc extension.

<OT>
If your system supports it, try
info gcc ''c extensions'' ''statement exprs''
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


" Fredrik Tolf" < FR ***** @ dolda2000.com>在消息中写道

news:10 *********************** @ pc7.dolda2000.com ..。
"Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
news:10***********************@pc7.dolda2000.com.. .
看看这个C片段:

#include< stdio.h>

int test(int * var)
{
return(* var + = 5);
}
int main(void)
{
int var;

var = 1;
printf("%i%i%i \ n",({var + = 4; var;}),
(printf("%i \ n", test(& var)),var),
(var + = 10,var - = 4));


使用GCC 3.3编译时,输出跟随运行时:
6
10 16 16

现在我想知道 - 这些类型的构造是否被C
标准所允许,或者它们是否是GCC扩展?如果它们是标准化的,那么标准化的语义是什么。也就是说,它们打算执行的顺序是什么,(var + = 4,var)之间的区别是什么?和({var
+ = 4; var;})",以及(expr1,expr2,expr3)"这真的意味着
它不是函数的参数列表。

另外,为什么我必须写({var + = 4; var;}) - 为什么我不能跳过外面的括号?

感谢您的关注!

Fredrik Tolf
Take a look at this C snippet:

#include <stdio.h>

int test(int *var)
{
return(*var += 5);
}

int main(void)
{
int var;

var = 1;
printf("%i %i %i\n", ({var += 4; var;}),
(printf("%i\n", test(&var)), var),
(var += 10, var -= 4));
}

When compiled with GCC 3.3, it outputs the following when run:
6
10 16 16

Now I''m wondering - Are these types of constructs allowed by the C
standard, or are they GCC extensions? If they are standardized, what is
the standardized semantics. That is, in which order are they intended to
be executed, what''s the difference between "(var += 4, var)" and "({var
+=4; var;})", and what does "(expr1, expr2, expr3)" really mean when
it''s not an argument list to a function.

Also, why must I write "({var += 4; var;})" - Why can''t I skip the outer
parentheses?

Thanks for your attention!

Fredrik Tolf



这不能在我的系统上编译 - 即VC7里面有{}的内容()。

我不认为它是有效的代码。

Dag



This does not compile on my system - i.e. VC7 with {} inside ().
I do not think it is valid code.

Dag


Dag Viken< da ******* @ earthlink.net>潦草地写下:
Dag Viken <da*******@earthlink.net> scribbled the following:
" Fredrik Tolf" < FR ***** @ dolda2000.com>在消息中写道
新闻:10 *********************** @ pc7.dolda2000.com ..。
"Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
news:10***********************@pc7.dolda2000.com.. .
看看这个C片段:

#include< stdio.h>

int test(int * var)
{
返回( * var + = 5);
}
int main(void)
{var /> int var;

var = 1;
printf("%i%i%i \ n",({var + = 4; var;}),
(printf("%i \ nn),test(&) var)),var),
(var + = 10,var - = 4));


使用GCC 3.3编译时,运行时会输出以下内容:
6
10 16 16

现在我想知道 - 这些类型的结构是否被C
标准所允许,或者它们是GCC扩展?如果它们是标准化的,那么标准化的语义是什么。也就是说,它们打算执行的顺序是什么,(var + = 4,var)之间的区别是什么?和({var
+ = 4; var;})",以及(expr1,expr2,expr3)"真的意思是什么时候
它不是一个函数的参数列表。
Take a look at this C snippet:

#include <stdio.h>

int test(int *var)
{
return(*var += 5);
}

int main(void)
{
int var;

var = 1;
printf("%i %i %i\n", ({var += 4; var;}),
(printf("%i\n", test(&var)), var),
(var += 10, var -= 4));
}

When compiled with GCC 3.3, it outputs the following when run:
6
10 16 16

Now I''m wondering - Are these types of constructs allowed by the C
standard, or are they GCC extensions? If they are standardized, what is
the standardized semantics. That is, in which order are they intended to
be executed, what''s the difference between "(var += 4, var)" and "({var
+=4; var;})", and what does "(expr1, expr2, expr3)" really mean when
it''s not an argument list to a function.


这不能在我的系统上编译 - 即VC7里面有{}。
我做的不认为它是有效的代码。

This does not compile on my system - i.e. VC7 with {} inside ().
I do not think it is valid code.




如前所述,它是GCC扩展。因此它是非标准的

代码,因此无法保证在*任何*特定编译器上有效。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ - http://www.helsinki.fi/~palaste ---------------------规则! -------- /

没有什么是永恒的 - 所以为什么不现在就把它摧毁?

- Quake



As has already been said, it''s a GCC extension. So it''s non-standard
code and thus not guaranteed to be valid on *any* specific compiler.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Nothing lasts forever - so why not destroy it now?"
- Quake


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

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