Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的后缀+;行为不像我';我希望 < >我了解C++中减量/增量运算符的前缀/后缀表示法的基本区别。然而,在下一个例子中发生了一些让我感到困惑的事情_C++_Postfix Operator - Fatal编程技术网

C+中的后缀+;行为不像我';我希望 < >我了解C++中减量/增量运算符的前缀/后缀表示法的基本区别。然而,在下一个例子中发生了一些让我感到困惑的事情

C+中的后缀+;行为不像我';我希望 < >我了解C++中减量/增量运算符的前缀/后缀表示法的基本区别。然而,在下一个例子中发生了一些让我感到困惑的事情,c++,postfix-operator,C++,Postfix Operator,我在下面共享的代码打印了以下内容 5*4**3***4****2****1 但我猜它会打印这个 5*4**4***3****2****1 这是怎么发生的?向堆栈中推/弹出堆栈是否有问题 int var = 5; cout << var-- << '*'; //prints 5 and then decrements to 4. cout << var << "**"; //The value of var (now 4)

我在下面共享的代码打印了以下内容

5*4**3***4****2****1

但我猜它会打印这个

5*4**4***3****2****1

这是怎么发生的?向堆栈中推/弹出堆栈是否有问题

int var = 5;
cout << var-- << '*'; //prints 5 and then decrements to 4.
cout << var << "**"; //The value of var (now 4) 
                     //is printed to the console.
//The next line prints 3***4****.
//I would have guessed it prints  4***3****.
cout << var-- << "***" << var-- << "****";  
//No matter what the above line prints, 
//the value of var after the above lines is 2, so...
cout << var-- << "*****" << var << endl; //...Print 2, decrement to 1 
                                         //and then 1 is finally printed.
int-var=5;

欢迎来到未知行为的陌生世界。在同一语句中,对同一变量调用两次递增或递减运算符是未定义的行为,因此不要这样做:)

#包括
int main()
{
int i=1;
//该打印9、10还是12?编译器会有所不同。。。

std::cout这一行的问题:

cout << var-- << "***" << var-- << "****";

我可以在Windows 10笔记本电脑上使用Visual Studio 2019吗……我的几个学生也得到了与我相同的结果。有人有什么想法吗?@krsju小心警告消息:警告消息:和警告消息:防止逻辑错误的第一道防线。忽略此警告将自担风险。
g++-9.2-Wall-pedantic test.cpp
给出此警告:
警告:上面的代码可能未定义对“var”的操作[-Wsequence point]
。我认为这一操作在C++17中发生了更改。无论是否允许,它都将永远无法读取(或维护)代码imho。我同意。谢谢你的帮助。我很惊讶,因为这个例子来自一个全新的著名作家的教科书。我本以为这篇文章会警告这个问题,或者不会要求学生预测与未定义行为相关的输出。