dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] expression precedence

  1. #1
    Join Date
    May 2018
    Posts
    67

    [RESOLVED] expression precedence

    I'd like to be sure to understand operators precedence in expression:

    The */ % operators have the same precedence and theirs associativity is left to right, so if I have:

    My question: if operators have the same precedence, as in this case, I have to follow associativity criteria or precedence?

    3.2*3%4/1*2 It's the same as :

    (((3.2*3)%4)/1)*2 ok!

    or

    (3.2*3)%(4/(1*2)) error!?!? ("*" ha bigger precedence than "/" ???)

    I think if operators have the same precedence I have to associativity criteria.

    thanks

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,464

    Re: expression precedence

    See https://docs.microsoft.com/en-us/cpp...-of-evaluation

    3.2 * 3 % 4 / 1 * 2

    (((3.2 * 3) % 4) / 1) * 2

    (3.2 * 3) % (4 / (1 * 2))

    These are all illegal expressions as % operates on integer numbers! If you try to compile them with VS you get

    error C2296: '%': illegal, left operand has type 'double'
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,252

    Re: expression precedence

    Quote Originally Posted by zio_mangrovia View Post
    3.2*3%4/1*2 It's the same as :

    (((3.2*3)%4)/1)*2 ok!

    or

    (3.2*3)%(4/(1*2)) error!?!? ("*" ha bigger precedence than "/" ???)
    The latter is wrong!
    Victor Nijegorodov

  4. #4
    Join Date
    May 2018
    Posts
    67

    Re: expression precedence

    Quote Originally Posted by 2kaud View Post
    These are all illegal expressions as % operates on integer numbers! If you try to compile them with VS you get
    That's right. But It was to understand precedence criteria.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,464

    Re: expression precedence

    Quote Originally Posted by zio_mangrovia View Post
    That's right. But It was to understand precedence criteria.
    Then consider legal statements! For the (legal) code below

    Code:
    cout << 3 * 3 % 4 / 1 * 2 << endl;
    
    cout << (((3 * 3) % 4) / 1) * 2 << endl;
    
    cout << (3 * 3) % (4 / (1 * 2)) << endl;
    this gives

    Code:
    2
    2
    1
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.7)

  6. #6
    Join Date
    May 2018
    Posts
    67

    Re: expression precedence

    Conclusion: if operators have same precedence (first case without () ) It's necessary follow associativity criteria that, in this case, It's Left to Right.
    right?

    Thank you very much
    Last edited by zio_mangrovia; July 1st, 2018 at 04:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)