所以我在思考这个问题(这是一个家庭作业/考试复习问题):
在a[j++] = ++i;不使用前/后增量运算符的情况下记下等效表达式.如果没有这样的表达式可以解释原因.
我能够想出以下内容:
a[j] = i+=1;
j+=1;
我想不出一种方法来增加一个[]中的j作为一个后增量而不是j+=1;后来使用,我相信会导致答案没有这样的表达式可以提供(因为它的两行代码而不是一行)和只是说明你没有post增量运算符就不能发布增量.
我错过了什么或者我是否正确?我只是想仔细检查一下.提前致谢.
编辑:感谢@James McNellis,他提供了一种方式
a[(j+=1)-1] = (i+=1);
为什么以下表达式求值为0?
i > --i
Run Code Online (Sandbox Code Playgroud)
假设i = 5.从左到右评估表达式,我们评估左操作数(i)得到5并且我们评估右操作数(--i)得到4.所以表达式约应该计算为1.但是当我用gcc编译并运行它,它总是计算为0.我的思维过程中是否存在缺陷?
有人可以解释以下代码中发生的事情吗?(摘自GeeksForGeeks)
int main(){
int a = 10;
++a = 20; // works
printf("a = %d", a);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行语句++ a = 20时究竟发生了什么?另外,请澄清为什么此代码执行失败?
int main(){
int a = 10;
a++ = 20; // error
printf("a = %d", a);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图摆脱C ++中的指针和地址的束缚,并且在更改参数的函数中遇到麻烦。
下面的代码Loop run #1.在无限循环中编写,而不是递增value foo。
我的问题是:这里的代码有什么问题?
#include <iostream>
void Statement(int *foo) {
std::cout << "Loop run #" << *foo << ". ";
foo++;
}
int main() {
int foo = 1;
for (;;) {
Statement(&foo);
}
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include <iostream>
int main()
{
int i = 0;
std::cout << ++i << ' ' << --i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
Nicolai Josuttis 在他的《C++17 完整指南》一书中写道,在C++17之前,这个特定的示例可能会产生不可预测的结果。和1 0都0 1可能被产生,但更重要的0 0是还有可能的输出。我不明白为什么第三个序列应该被考虑为可能的。应该在评估第二个值之前评估++ior ,根据定义,第二个值不能产生两个零,不是吗?--i
可能重复:
c中的post和pre增量
我是C的新手,我在C中有一个Increment运算符程序
#include<stdio.h>
main(){
int a, b;
a = 2;
b = a + ++a + ++a;
printf("%d", b);
getchar();
}
Run Code Online (Sandbox Code Playgroud)
输出是10,有人可以解释一下输出将是10.
我今天看到了一个有趣的声明,包括后增量和预增量.请考虑以下计划 -
#include <stdio.h>
int main(){
int x, z;
x = 5;
z = x++ - 5; // increase the value of x after the statement completed.
printf("%d\n", z); // So the value here is 0. Simple.
x = 5;
z = 5 - ++x; // increase the value of x before the statement completed.
printf("%d\n", z); // So the value is -1.
// But, for these lines below..
x = 5;
z = x++ - ++x; // **The interesting …Run Code Online (Sandbox Code Playgroud) 我正在学习C,我的老师给了我们一些功课.我们必须确定一些代码的输出.我不明白怎么做y=4.
代码如下
int main() {
int w = 3, y = 3;
printf("w = %i\n", --w + w--);
printf("y = %i\n\n", y = w + y++);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但我无法弄清楚。它与n ++和++ n之间的差异有关(我以为我理解但显然不是)。
#include <stdio.h>
#include <math.h>
long algorithmA(int n);
long algorithmB(int n);
int main(){
long A, B;
A = B = 0;
int n = 1;
while(A >= B){
A = algorithmA(n);
B = algorithmB(n);
n++;
}
printf("At n = %d, Algorithm A performs in %ld seconds & "
"Algorithm B performs in %ld seconds.", n, A, B);
}
long algorithmA(int n){
return pow(n,4) * 86400 * 4;
}
long algorithmB(int n){
return pow(3,n);
}
Run Code Online (Sandbox Code Playgroud)
在这里您可能会告诉我,我正在尝试看算法A在什么时候优于算法B。功课中的功能和时间单位是给我的。
无论如何,我一直认为在while循环结束时“ ++”的顺序无关紧要。但是,如果我使用++ n而不是n …
我遇到了一个我无法理解的问题.a的输出是6,b是-6,但无论我用什么变量初始化它,c都保持不变.
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.