C / C ++ Math Operation Order

So, I know that C ++ has operator precedence and that

int x = ++i + i++;

- undefined, because pre ++ and post ++ are on the same level, and therefore it is impossible to determine which one will be calculated first. But I was wondering if

int i = 1/2/3;

- undefined. The reason I ask is because there are several ways to look at this (1/2) / 3 OR 1 / (2/3). I assume this behavior is undefined, but I would like to confirm it.

+5
source share
5 answers

In your example, the compiler can evaluate "1", "2" and "3" in whatever order he likes, and then apply sections from left to right.

++ + ++. ++ .

, , , .

+5

++ , , -, , (1/2)/3, :

, ( , ), . , a = b = c a = (b = c), (a = b) = c - .

+17

- undefined, i .

:

int i = (1 / 2) / 3;

/ .

+5

, :

#include <iostream>

using namespace std;

int main (int argc, char *argv[]) {
    int i = 16/2/2/2;
    cout<<i<<endl;
    return 0;
}

"2" 1 16.

+2

, undefined, int, . double float, .

0

All Articles