C语言 如何在 C 中编写“虚拟”(什么都不做)行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22264284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a "dummy" (do nothing) line in C
提问by PopKernel
Is there a dedicated way to write a dummy line in C? (kind of like passin Python) If you are wondering why I don't just leave a blank line, I need something that I can attach an Xcode breakpoint to-- if there is nothing there the breakpoint will skip to the next line!! So far I've been using sleep(0)for this purpose. I was wondering if there was a better/more efficient/more official way to accomplish this.
有没有专门的方法可以在 C 中编写虚拟行?(有点像pass在 Python 中)如果你想知道为什么我不只留下一个空行,我需要一些可以附加 Xcode 断点的东西——如果那里没有任何东西,断点将跳到下一行!到目前为止,我一直在sleep(0)为此目的使用。我想知道是否有更好/更有效/更官方的方法来实现这一点。
Oh, and I'm using Objective-C, so if there is anything that was added in Obj-C that fits this purpose, feel free to include it.
哦,我正在使用 Objective-C,所以如果在 Obj-C 中添加了符合此目的的任何内容,请随意包含它。
采纳答案by Merlevede
Add a trivial assignment
添加一个简单的任务
var = var;
回答by Enrico Susatyo
Put a semi colon. It works in C and Obj-C (and Java, and Swift, and many other languages).
放一个分号。它适用于 C 和 Obj-C(以及 Java、Swift 和许多其他语言)。
;
回答by ElGavilan
sleep(0)works, so does a null statement (just a semicolon ;on a single line) and the statement i+1;
sleep(0)有效,null 语句(;单行只是一个分号)和语句也有效i+1;
Compiler optimization will usually result in no machine code being generated for these statements.
编译器优化通常不会为这些语句生成机器代码。
回答by felixphew
Most of the answers already posted will do what you want, but if you need a block, try a pair of braces:
大多数已经发布的答案都可以满足您的需求,但是如果您需要一个块,请尝试使用一对大括号:
{}
And, if you need an actual statement that actually does nothing, and yet will survive compilation, then a little inline assembly can do the trick:
而且,如果您需要一个实际上什么都不做的实际语句,但仍然可以编译通过,那么一点内联汇编可以解决问题:
asm("nop");
Technically this isn't portable; but the nopinstruction exists in basically any instruction set you care to name. Also guaranteed* not to be compiled out!
从技术上讲,这不是便携式的;但该nop指令基本上存在于您想命名的任何指令集中。也保证*不被编译出来!
回答by nhgrif
Just put the breakpoint on the first line you don't want executed. The breakpoint stops the execution before the code on that line is executed.
只需将断点放在您不想执行的第一行。断点在执行该行上的代码之前停止执行。
Without creating a "fake" line:
不创建“假”行:
someMethod();
// empty line
BREAKPOINT
someOtherMethod();
With creating a "fake" line:
创建一个“假”行:
someMethod();
BREAKPOINT
;
someOtherMethod();
Both of these result in the exact same result. The breakpoint stops at the same place.
这两者都导致完全相同的结果。断点停在同一个地方。
回答by Emmanuel Sellier
What about "YES" This is what I use when I wan't to switch off logs
“YES”怎么样这是我不想关闭日志时使用的
#define Log(s,...) YES
#define Log(s,...) YES
回答by qulinxao
just place this constant:
只需放置这个常量:
0xDEBAF;

