首页 > 技术相关 > C语言逗号运算符的一些问题

C语言逗号运算符的一些问题

2011年10月16日 发表评论 阅读评论

前段时间,某童鞋碰到一个和逗号运算符相关的诡异问题,当时我也查了下运算符优先级,仔细想了半天才想明白,当时就想把关于逗号运算符的一些东西写一下,但由于比较忙,一直没写,现在补上。

在这里,为了省事,直接把邮件内容附上(假如那位童鞋你看到了,侵犯了你的版权,别生气哈~),我承认我真的很懒。。。

===============================================

对了能解释下这个问题么?

 void main ()
{
    bool test=false;
    int a = 1;

    test == true ? printf("hello\t"), printf("world\n"),a++ :
    printf("Ha\t"), a++, a++;

    printf("%d", a);
}

Test = false 的时候 a = 3;

Test = true 的时候 a = 4;

 void main ()
{
    bool test=false;
    int a = 1;

    test == true ? printf("hello\t"), printf("world\n"),a++ :
                         (printf("Ha\t"), a++, a++);

    printf("%d", a);
}

Test = false 的时候 a = 3;

Test = true 的时候 a =1;

最近写程序的时候发现的貌似逗号和问号表达式连用的时候有些奇怪

==================================

在C语言中,逗号运算符优先级是最低的,所以第一个表达式等于:

(test == true ? printf(“hello\t”), printf(“world\n”),a++ :

printf(“Ha\t”)), a++, a++;

所以:

Test = false 的时候 a = 3;

Test = true 的时候 a = 4;

? :运算符一般很容易理解成:管的范围是到分号,而事实上,按优先级,只管到逗号。之后的是另外一个表达式

对于这些不太清楚的优先级,一般都加括号,反正括号优先级够高。

逗号运算符的作用就是将两个表达式连接起来,整个表达式的值,等于最后一个表达式的值。

比如,上面改为:

test = true;

b = (test == true ? printf(“hello\t”), printf(“world\n”),a++ :printf(“Ha\t”), a++, a++);

printf(“%d,%d”, a,b);

则输出为 a=4,b=3

反之,如果去掉括号

test = true;

b = test == true ? printf(“hello\t”), printf(“world\n”),a++ :printf(“Ha\t”), a++, a++;

printf(“%d,%d”, a,b);

则a=4,b=1(等号的优先级比逗号高,但比?:低)

如果上面的test=false,

test = false;

b = test == true ? printf(“hello\t”), printf(“world\n”),a++ :printf(“Ha\t”), a++, a++;

printf(“%d,%d”, a,b);

则a=3,b=3

因为printf的返回值,打印成功的话,返回打印的char个数,否则,返回负数。



本文作者: Sigma    在新浪微博关注SigmaSigmaWeibo    RSS订阅本博客
本文链接: http://www.sigma.me/2011/10/16/c-lang-comma-operator.html
本博客采用知识共享署名—非商业性-禁止演绎使用3.0协议进行许可,转载请保留作者和原文链接。

  1. 2011年10月16日15:54 | #1

    好诡异的用法。。。为毛不直接用if。。。

  2. 2011年10月17日01:37 | #2

    @grapeot

    不知道,其实直接括号括起来也行啊,反正我是这样的,不清楚的就加括号

  3. lc
    2011年11月16日15:13 | #3

    太菜了 没明白。。。。

  4. 2019年1月9日11:23 | #4

    I just wanted to make a quick comment so as to thank you for some of the precious information you are placing at this website. My time consuming internet look up has at the end been compensated with useful suggestions to talk about with my family and friends. I ‘d assert that many of us website visitors are undoubtedly fortunate to live in a fabulous website with so many lovely people with interesting points. I feel very grateful to have used your web site and look forward to plenty of more excellent times reading here. Thank you again for all the details.

  5. 2019年1月9日18:23 | #5

    Thanks for your entire effort on this web site. Debby really loves setting aside time for investigation and it is simple to grasp why. My partner and i notice all about the lively way you deliver rewarding tips and tricks via your blog and in addition foster contribution from other people on this idea so our own princess is really discovering a great deal. Enjoy the remaining portion of the year. You are always doing a pretty cool job.

  6. 2019年1月10日14:00 | #6

    I in addition to my guys came reading the excellent tips and hints found on your site while all of the sudden got a terrible feeling I had not thanked the site owner for those tips. These boys are already stimulated to read through all of them and already have really been loving those things. Thank you for simply being considerably helpful as well as for making a choice on these kinds of tremendous guides most people are really desirous to be aware of. My honest apologies for not saying thanks to sooner.

  7. 2019年1月11日09:05 | #7

    I and my pals have already been looking at the great advice found on the website and so all of a sudden came up with a terrible suspicion I had not expressed respect to the site owner for those techniques. All the people became as a result warmed to study all of them and have really been enjoying them. Thanks for genuinely considerably helpful and also for deciding upon certain good tips millions of individuals are really needing to be informed on. My personal honest regret for not expressing appreciation to earlier.

  8. 2019年1月12日05:02 | #8

    I actually wanted to send a simple message to appreciate you for some of the superb tips and hints you are showing on this website. My time consuming internet search has at the end been recognized with sensible ideas to talk about with my visitors. I would admit that we site visitors actually are very lucky to live in a decent place with so many brilliant professionals with insightful plans. I feel somewhat blessed to have come across your entire website page and look forward to many more fun minutes reading here. Thank you once again for all the details.

  9. 2019年1月13日00:50 | #9

    I needed to create you one bit of note to thank you yet again relating to the pleasant knowledge you’ve featured at this time. It’s so remarkably open-handed with you to make unhampered exactly what most people could have sold as an ebook in order to make some money for their own end, especially seeing that you could have done it in the event you considered necessary. Those guidelines also served like a good way to fully grasp that other individuals have the identical dream similar to my very own to understand whole lot more pertaining to this issue. I am sure there are a lot more fun moments up front for individuals that take a look at your site.

  10. 2019年1月13日18:11 | #10

    I not to mention my buddies appeared to be looking through the nice tips located on your site and so before long got a terrible feeling I had not thanked the website owner for those techniques. Most of the men are already certainly passionate to read through them and already have in truth been enjoying those things. Many thanks for getting indeed accommodating as well as for choosing certain smart topics most people are really wanting to know about. My very own sincere apologies for not expressing gratitude to sooner.

  11. 2019年1月14日11:41 | #11

    My spouse and i were absolutely ecstatic Jordan managed to finish off his reports from the precious recommendations he grabbed through your web site. It is now and again perplexing to simply find yourself making a gift of secrets that many many others have been selling. And we also grasp we need you to appreciate because of that. These illustrations you’ve made, the simple blog navigation, the friendships you can help instill – it’s got most overwhelming, and it is making our son and us do think the topic is enjoyable, which is certainly truly essential. Many thanks for everything!

  12. 2019年1月15日00:21 | #12

    My wife and i got joyful Albert managed to carry out his preliminary research using the precious recommendations he grabbed using your web site. It’s not at all simplistic to simply always be handing out helpful tips a number of people have been selling. Therefore we fully understand we have the blog owner to thank for this. All the explanations you made, the simple site navigation, the friendships you assist to promote – it’s most powerful, and it is aiding our son and our family know that the issue is pleasurable, and that is especially important. Thank you for the whole thing!

  13. 2019年1月15日00:27 | #13

    A lot of thanks for all your valuable work on this blog. Debby loves participating in investigation and it is obvious why. A number of us learn all of the compelling method you create vital thoughts via your blog and as well foster contribution from others on the situation so my princess has been discovering a lot of things. Have fun with the rest of the year. You’re the one doing a splendid job.

  14. 2019年1月15日08:15 | #14

    My spouse and i felt absolutely lucky when Jordan managed to conclude his research while using the precious recommendations he came across out of your site. It’s not at all simplistic just to possibly be giving for free things which often some people could have been trying to sell. And we all see we have got the website owner to thank for this. The specific explanations you have made, the simple website menu, the friendships you make it easier to create – it’s most sensational, and it is aiding our son and our family recognize that the topic is satisfying, which is highly mandatory. Thanks for all the pieces!

  15. 2019年1月15日17:02 | #15

    Thank you a lot for giving everyone a very special chance to read critical reviews from this site. It can be very pleasant plus stuffed with fun for me and my office peers to visit your site at minimum 3 times in one week to read through the new tips you have. And lastly, we’re usually motivated with your eye-popping techniques served by you. Certain two tips in this article are definitely the very best we have all had.

  16. 2019年1月16日10:13 | #16

    I want to show thanks to this writer just for bailing me out of this type of predicament. Right after surfing around through the world wide web and getting techniques that were not powerful, I figured my entire life was done. Being alive without the presence of answers to the issues you’ve solved as a result of your good report is a serious case, and ones which may have adversely affected my career if I had not come across your web blog. The knowledge and kindness in touching the whole thing was tremendous. I am not sure what I would have done if I had not come upon such a point like this. I can also now relish my future. Thank you so much for the expert and result oriented guide. I won’t think twice to recommend the blog to any person who needs guidance on this situation.

  17. 2019年1月16日19:30 | #17

    I have to express my passion for your kindness supporting individuals who must have help on this important subject matter. Your personal commitment to getting the solution all through came to be incredibly helpful and have in most cases encouraged individuals like me to attain their targets. Your amazing useful suggestions indicates a lot to me and especially to my peers. Thanks a lot; from everyone of us.

  18. 2019年1月17日05:36 | #18

    I must express my thanks to this writer just for bailing me out of this dilemma. Just after exploring throughout the world wide web and getting proposals which are not productive, I assumed my life was done. Living minus the answers to the difficulties you’ve solved through this review is a critical case, and those that would have in a negative way affected my career if I hadn’t noticed your site. That talents and kindness in touching all the pieces was tremendous. I am not sure what I would’ve done if I hadn’t come across such a step like this. I can now look forward to my future. Thanks for your time so much for the reliable and result oriented help. I will not be reluctant to recommend the blog to anyone who should receive assistance on this topic.

  19. 2019年1月17日13:32 | #19

    I in addition to my buddies were found to be going through the great strategies found on the website while at once developed a terrible suspicion I never thanked the web blog owner for those secrets. All of the boys ended up absolutely passionate to study all of them and already have honestly been having fun with these things. I appreciate you for simply being indeed considerate and also for picking out such helpful things most people are really needing to be informed on. My personal honest apologies for not expressing gratitude to you earlier.

  20. 2019年1月17日18:02 | #20

    Thank you a lot for providing individuals with remarkably pleasant possiblity to discover important secrets from this site. It’s usually very good and packed with amusement for me and my office mates to search your website on the least 3 times a week to see the latest guidance you have got. Of course, we’re always amazed for the outstanding tricks served by you. Some 1 tips in this posting are certainly the most effective I have ever had.

  21. 2019年1月17日23:38 | #21

    My spouse and i felt absolutely ecstatic when Michael could carry out his research using the ideas he grabbed through the blog. It is now and again perplexing to just always be handing out secrets which often many people might have been selling. And we see we have the blog owner to give thanks to because of that. The entire illustrations you’ve made, the simple blog navigation, the relationships your site make it possible to foster – it’s all extraordinary, and it’s really facilitating our son in addition to our family feel that that topic is interesting, which is certainly highly important. Thank you for the whole lot!

  22. 2019年1月18日07:26 | #22

    I’m also commenting to make you know of the fantastic discovery my friend’s girl enjoyed reading through your site. She discovered a wide variety of pieces, with the inclusion of how it is like to have an incredible giving spirit to get certain people very easily grasp specified very confusing matters. You really surpassed readers’ expected results. Thanks for providing such beneficial, trusted, explanatory and also unique tips about the topic to Tanya.

  23. 2019年1月18日14:52 | #23

    Thank you so much for providing individuals with remarkably marvellous opportunity to read in detail from this blog. It is usually very awesome plus stuffed with a lot of fun for me and my office colleagues to search your web site the equivalent of 3 times in a week to read through the new stuff you will have. And lastly, we’re usually satisfied concerning the tremendous tips and hints you give. Some 1 areas in this post are essentially the most effective I have ever had.

  24. 2019年1月19日00:30 | #24

    I simply wanted to develop a brief word to say thanks to you for all the wonderful information you are posting on this site. My long internet research has finally been compensated with reasonable knowledge to talk about with my colleagues. I ‘d repeat that we site visitors are rather lucky to live in a perfect site with so many perfect individuals with useful tricks. I feel truly lucky to have encountered your web site and look forward to really more excellent minutes reading here. Thanks a lot once again for all the details.

  25. 2019年1月19日07:32 | #25

    I intended to send you a very little word just to say thanks a lot over again for those amazing pointers you’ve documented here. This has been really seriously generous of people like you to present unreservedly just what many people could have supplied as an e book in order to make some profit on their own, most importantly given that you might well have tried it in case you desired. These secrets also acted like a great way to know that other individuals have a similar fervor just as my personal own to learn a great deal more when considering this condition. I am certain there are numerous more fun moments ahead for individuals that browse through your blog.

  26. 2019年1月19日15:19 | #26

    I want to show my respect for your kind-heartedness in support of individuals who have the need for help with in this content. Your special dedication to passing the solution all-around appears to be extremely powerful and has without exception made folks like me to realize their endeavors. This informative advice signifies a lot to me and especially to my office workers. With thanks; from each one of us.

  27. 2019年1月20日01:31 | #27

    My wife and i have been now contented Michael managed to carry out his investigations out of the precious recommendations he received from your own web site. It is now and again perplexing to simply choose to be handing out solutions which people today may have been making money from. And we also already know we need the website owner to be grateful to for this. The main explanations you made, the simple site navigation, the friendships your site assist to create – it’s all awesome, and it is aiding our son in addition to us reckon that that article is fun, which is exceptionally serious. Thanks for the whole lot!

  28. 2019年1月20日08:17 | #28

    I together with my friends have already been digesting the good tips located on your website and immediately got a horrible suspicion I never thanked the website owner for those secrets. My men ended up totally stimulated to learn them and have in truth been taking advantage of those things. I appreciate you for simply being quite helpful as well as for settling on certain helpful things most people are really needing to be informed on. Our own honest regret for not saying thanks to you sooner.

  29. 2019年1月20日12:33 | #29

    I have to show some appreciation to you just for bailing me out of this predicament. As a result of surfing around through the world-wide-web and finding suggestions which are not helpful, I believed my entire life was gone. Being alive without the presence of answers to the issues you have fixed all through your entire article is a critical case, as well as the kind that would have negatively affected my entire career if I hadn’t come across the blog. The know-how and kindness in touching all the things was tremendous. I’m not sure what I would’ve done if I had not come across such a point like this. I am able to at this point look forward to my future. Thanks for your time very much for your high quality and sensible guide. I will not be reluctant to propose your blog to anybody who needs counselling on this problem.

  30. 2019年1月20日17:00 | #30

    My spouse and i were delighted that Peter managed to conclude his web research through the entire ideas he gained in your blog. It’s not at all simplistic just to possibly be handing out tips and hints which often the rest might have been selling. We really see we need the writer to give thanks to for this. The specific illustrations you’ve made, the easy site navigation, the friendships you can help to engender – it is many awesome, and it’s making our son and us consider that that matter is excellent, which is certainly extraordinarily fundamental. Thanks for all!

  31. 2019年1月21日00:39 | #31

    I wanted to write you the very little note to help thank you so much once again regarding the stunning knowledge you have shown on this page. It is certainly pretty open-handed with you to convey freely just what numerous people would’ve sold for an e book to help with making some money on their own, certainly considering that you could possibly have done it if you wanted. These good tips also served to be a easy way to be certain that some people have the identical dreams just as my own to know way more when it comes to this condition. I am certain there are millions of more pleasant sessions in the future for individuals that discover your blog.

  32. 2019年1月21日02:31 | #32

    My wife and i got more than happy Chris managed to conclude his analysis through your ideas he obtained from your web pages. It’s not at all simplistic to just possibly be offering methods that many the others could have been selling. And we acknowledge we’ve got you to give thanks to because of that. The illustrations you have made, the easy site navigation, the friendships your site aid to instill – it’s everything great, and it’s really assisting our son and the family reason why this matter is cool, which is highly important. Many thanks for the whole lot!

  33. 2019年1月21日09:04 | #33

    I definitely wanted to write a quick message to say thanks to you for all the unique secrets you are giving at this site. My prolonged internet lookup has at the end been honored with awesome details to talk about with my companions. I ‘d express that many of us visitors are unequivocally endowed to live in a perfect network with very many marvellous individuals with interesting points. I feel pretty happy to have used your website page and look forward to tons of more entertaining times reading here. Thanks once more for a lot of things.

  34. 2019年1月21日16:24 | #34

    I must express my appreciation to you just for bailing me out of this crisis. After looking out through the online world and coming across tricks which were not powerful, I figured my entire life was gone. Living without the approaches to the difficulties you’ve resolved by means of the write-up is a crucial case, as well as ones which might have in a negative way damaged my career if I had not noticed your web page. That talents and kindness in dealing with a lot of stuff was useful. I don’t know what I would’ve done if I hadn’t discovered such a thing like this. I can at this point relish my future. Thank you very much for the reliable and result oriented help. I will not be reluctant to suggest your blog post to any person who needs and wants tips about this issue.

  35. 2019年1月22日02:41 | #35

    Thank you for your own work on this web site. Kim enjoys setting aside time for investigation and it is simple to grasp why. We hear all concerning the powerful ways you create efficient solutions on this web site and increase contribution from visitors about this area of interest then our girl is without question starting to learn a whole lot. Take advantage of the remaining portion of the year. You’re doing a splendid job.

  36. 2019年1月22日10:53 | #36

    I want to get across my love for your generosity supporting those people that really need help on this particular issue. Your personal dedication to passing the message all around had become particularly insightful and have usually encouraged those like me to realize their goals. Your personal informative help and advice implies a lot a person like me and somewhat more to my fellow workers. Best wishes; from everyone of us.

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

无觅相关文章插件,快速提升流量