You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A single '#' will create a string from the given argument, regardless of what that argument contains, while the double '##' will create a new token by concatenating the arguments.
An occurrence of a parameter in a function-like macro, unless it is the operand of # or ##, is expanded before substituting it and rescanning the whole for further expansion. Because g's parameter is the operand of #, the argument is not expanded but instead immediately stringified ("f(1,2)"). Because h's parameter is not the operand of # nor ##, the argument is first expanded (12), then substituted (g(12)), then rescanning and further expansion occurs ("12").
the expression x == y would yield 1 because through the "usual arithmetic conversions" the value of x is converted to unsigned and thus to 0xffffffff.
The expression (unsigned int)x == y is 1 as well. The only difference is that you do the conversion explicitly with a cast.
The expression x == (int)y will most likely be 1 as well because converting 0xffffffff to int yields -1 on most platforms (two's complement negatives). Strictly speaking this is implementation-defined behavior and thus might vary on different platforms.
Safest is to check that the number is in range before casting:
Why does it tend to get into an infinite loop if I use continue in a while loop, but works fine in a for loop?
The loop-counter increment i++ gets ignored in while loop if I use it after continue, but it works if it is in for loop.
If continue ignores subsequent statements, then why doesn't it ignore the third statement of the for loop then, which contains the counter increment i++? Isn't the third statement of for loop subsequent to continue as well and should be ignored, given the third statement of for loop is executed after the loop body?
while(i<10) //causes infinite loop
{
...
continuei++
...
}
for(i=0;i<10;i++) //works fine and exits after 10 iterations
{
...
continue
...
}
The reason is because the continue statement will short-circuit the statements that follow it in the loop body. Since the way you wrote the while loop has the increment statement following the continue statement, it gets short-circuited. You can solve this by changing your while loop.
A lot of text books claim that:
for (i=0; i<N; ++i) {
/*...*/
}
is equivalent to:
i=0;
while (i<N) {
/*...*/++i;
}
But, in reality, it is really like:
j=0;
while ((i=j++) <N) {
/*...*/
}
Or, to be a little more pedantic:
i=0;
if (i<10) do {
/*...*/
} while (++i, (i<10));
These are more equivalent, since now if the body of the while has a continue, the increment still occurs, just like in a for. The latter alternative only executes the increment after the iteration has completed, just like for (the former executes the increment before the iteration, deferring to save it in i until after the iteration).
Modifying the value of i more than once without a sequence point in between the modifications results in undefined behavior. So, the results of your code are undefined.
View Post on Blog
UPDATE: 看起来今年的计概考试并没有类似往年题一样的未定义行为出现,可以说今年收敛了一些。故这里说,zy 和 chj 比起来,还是排雷 chj 😁
除非有说明,均在 gcc 下测试
Nice websites
往年题?
其中,
gcc -E
认为的是注意到 C 的运算符处理和空格有关,故结果为
而 VC 认为的是
注意到第二句时 x 已自减,故结果为
h(x) != g(x) ?
也就是说,碰到
#
或##
后,宏就不会递归展开No print?
这是非常神奇的, 但是和宏没什么关系。
首先,
sizeof
返回的是unsigned long int
,d
是int
类型,这两个比较的时候会比较迷,现行的是 unsigned preserving 策略,即把int
变成unsigned int
,再行比较。于是
TOTAL_ELEMENTS-2
还是unsigned long int
,而 -1 就变成巨大无比,条件自然就不可能成立了。所以呢,把
TOTAL_ELEMENTS
换成由unsigned int a=7;
定义的a
,还是一样的。还有一点有意思的是:
continue
indo...while
跟在后面的
while
也是循环控制的一种,所以continue
不会跳过while
拓展知识:how
for
equalswhile
?著名的变态题
著名的 ALE(bushi) 如是说:
Stackoverflow 如是说
简单翻译成人话就是,想一口气多次改变变量值的行为是未定义的。所以考试考一个未定义的东西有什么用呢?
所以“往年题”部分提到的例子也是未定义的。
讲道理说,不同主流编译器在同样的计算机上,使用相同的合法的程序,产生不同的结果,要么是编译器的附加功能,要么就是未定义的运算了吧!
这只是普通的标签
坑就在
deefault
的l
打成了1
😂居然编译器给过了!?
原来假的
defau1t
被处理成了一个标签供goto
使用,case
和default
也类似于 C 里的标签,共用类似的语法,毕竟case
本身就可以直接跳到任何地方,包括if
里面。case
真的可以随便跳吗?跳过了变量初始化就会报错了,编译都过不了
The text was updated successfully, but these errors were encountered: