C中的运算符问题 [英] operator question in c

查看:81
本文介绍了C中的运算符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c

I am trying in c

main()
{
    int a=5;
    a=a++ + ++a + a++ + ++a + ++a;
    printf("%d",a);
}


我正在尝试用它来表达更多内容,但答案不充分.那么方法是什么
解决这种类型的表达. gcc编译器如何使用这种类型的表达式.


I am trying more expression with it but not sufficient answer. so what is the method
for solve this type of expression. how gcc compiler work with this type of expression.
please

推荐答案

从左到右顺序,多次通过...
前缀运算符优先.
后缀运算符最后.

还有额外的信用...
一些编译器在这方面是错误的.以这种形式编写代码可能很危险.在现实世界中,永远不要这样做.始终将此类操作分解为各个组成部分,以免冒因编译器错误而丢掉工作的风险.
Left to right order, multiple passes...
Prefix operators first.
Postfix operators last.

And for extra credit...
Some compilers are buggy in this regard. It can be dangerous to write code in this form. In the real world, never do this. Always break such operations down into constituent parts, so you don''t risk losing your job over a compiler bug.


对于大多数像样的编译器,您的代码都等效于此:
With most decent compilers your code is equivalent to this:
main()
{
    int a=5;

    ++a; ++a; ++a;          // perform prefix increment/decrement instructions
    // a == 8
    a=a + a + a + a + a;    // execute the statement without increment/decrement operators
    // a == 40
    a++; a++;               // perform postfix increment/decrement instructions
    // a == 42

    printf("%d",a);
}


但是,如果在运行这段糟糕的代码后C标准没有定义a的值,我不会感到惊讶.不要写这样的废话.在实践中我从未见过类似的代码.


However I wouldn''t be surprised if the C standard didn''t define the value of a after running this terrible piece of code. Don''t write such crap. I have never seen similar code in practice.


这篇关于C中的运算符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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