是否有和没有括号循环使用C不同的方式处理? [英] Are loops with and without parenthesis handled differently in C?
问题描述
我是通过在调试一些C / CUDA code,像踩着
I was stepping through some C/CUDA code in the debugger, something like:
for(uint i = threadIdx.x; i < 8379; i+=256)
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
和我心乱如麻,因为调试是在一步路过它,虽然输出是正确的。我意识到,当我把花括号我的循环如下面的代码片段,它像预期的那样表现调试
And I was utterly confused because the debugger was passing by it in one step, although the output was correct. I realised that when I put curly brackets around my loop as in the following snippet, it behaved in the debugger as expected.
for(uint i = threadIdx.x; i < 8379; i+=256) {
sum += d_PartialHistograms[blockIdx.x + i * HISTOGRAM64_BIN_COUNT];
}
所以是括号,免费在C或调试器区别对待的循环,或者是特别CUDA。
So is are parenthesis-free for loops treated differently in C or in the debugger, or perhaps it is particular to CUDA.
感谢
推荐答案
在调试器将执行一个语句在一个时间。检查了这一点:
The debugger executes one statement at a time. Check this out:
int sum = 0; /* one assignment statement */
for (int k = 0; k < 10; k++) sum += k; /* one for statement */
和与此比较
int sum = 0; /* one assignment statement */
for (int k = 0; k < 10; k++)
{ /* for statement with the body
in a block of statements */
sum += k; /* assignment statement */
}
在上面的第一个例子中,总和+ = K
是为
语句的一个组成部分;在第二个例子中,它本身就是一个完整的声明。
In the first example above, the sum += k
is an integral part of the for
statement; in the 2nd example, it is a full statement on its own.
这篇关于是否有和没有括号循环使用C不同的方式处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!