在理解printf(“%d \ n")的方式后,({int n; scanf(“%d",& n); n * n;}});在C中工作 [英] On understanding how printf("%d\n", ( { int n; scanf("%d", &n); n*n; } )); works in C

查看:144
本文介绍了在理解printf(“%d \ n")的方式后,({int n; scanf(“%d",& n); n * n;}});在C中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过

我想知道它如何工作以及是否符合标准?

I was wondering how does this work and if this conforms the standard?

推荐答案

此代码使用了称为语句表达式的"GNU C"功能,括号内的复合语句可用作表达式,其类型和值与复合语句中最后一条语句的结果匹配.这在语法上不是有效的C语言,而是GCC功能(也为其他一些编译器所采用)是由于被认为对编写不重复评估其参数的宏而被认为很重要而添加的.

This code is using a "GNU C" feature called statement-expressions, whereby a parentheses-enclosed compound statement can be used as an expression, whose type and value match the result of the last statement in the compound statement. This is not syntactically valid C, but a GCC feature (also adopted by some other compilers) that was added presumably because it was deemed important for writing macros which do not evaluate their arguments more than once.

如果您在必须阅读的代码中遇到它,您应该知道它的含义和作用,但是我会避免自己使用它.这是令人困惑,不必要和非标准的.静态内联函数几乎总是可以移植相同的东西.

You should be aware of what it is and what it does in case you encounter it in code you have to read, but I would avoid using it yourself. It's confusing, unnecessary, and non-standard. The same thing can almost always be achieved portably with static inline functions.

这篇关于在理解printf(“%d \ n")的方式后,({int n; scanf(“%d",& n); n * n;}});在C中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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