个人预处理器指令
作为一个 C 新手,我想听听宏“定义”开发人员正在使用什么。 我一直在考虑将它们放在标题中以跳过我已经习惯的冗长内容:
#define TS_ typedef struct {
#define _TS(x) } x;
#define I(x)_ { int i; for ( i = 1; i <= x; i++ ) {
#define _I } }
我可以在这些宏中添加 \n \t 等吗? 因为我想传递我的源代码减去额外的包含:
#define TS_ typedef struct {\n
#define _TS(x) } x;\n
#define I(x)_ { int i;\n\tfor ( i = 1; i <= x; i++ ) {\n
#define _I \t}\n}\n
这些有用吗?
即:我可以使用处理器将我的源代码替换为我的个人包含到格式化源而不包含包含吗?
良好的预处理器提示和技巧的链接也值得赞赏。
Being a C novice I would like to hear what Macro "define"s developers are using. I've been thinking about putting these in a header to skip verbosity I've become used to:
#define TS_ typedef struct {
#define _TS(x) } x;
#define I(x)_ { int i; for ( i = 1; i <= x; i++ ) {
#define _I } }
Can I add \n \t etc within these macros? As I would like to pass on my sourcecode minus the extra include:
#define TS_ typedef struct {\n
#define _TS(x) } x;\n
#define I(x)_ { int i;\n\tfor ( i = 1; i <= x; i++ ) {\n
#define _I \t}\n}\n
Would these work?
ie: Can I use the proprocessor to replace my sourcecode with my personal include to formatted source without the include ?
Links to good preprocessor tips and tricks also appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在开始之前,请勿使用以下划线开头的宏名称 - 这些名称是为编译器和标准库编写者保留的,并且不得在您自己的代码中使用。
另外,我想说你建议的宏都是非常糟糕的主意,因为它们向读者隐藏了正在发生的事情。 它们的唯一理由似乎是为您节省很少的打字量。 一般来说,只有在没有明智的选择时才应该使用宏。 在这种情况下,有一种情况——只需编写代码即可。
Before you get started, do not use macro names that begin with an underscore - these are reserved for compiler and standard library writers, and must not be used in your own code.
Additionally, I would say that the macros you suggest are all very bad ideas, because they hide from the reader what is going on. The only justification for them seems to be to save you a very small amount of typing. Generally, you should only be using macros when there is no sensible alternative. In this case there is one - simply write the code.
您可以通过转义换行符来添加空格
,但正如其他人所说,这并不是一个很好的方法。
最好研究一下编辑器宏,这样您就可以输入快捷方式并让编辑器展开它。
You can put whitespace in by escaping the newline
But as others have said it's not really a great way to to do this.
It would be much better to look into editor macros so you could type the shortcut and have the editor expand it.
你正走入一条错误的道路。 不要编写其他人不熟悉的您自己的 cpp 指令 - 这将使您的代码难以理解,并且在某些时候难以维护。
尝试找到一些好的 C 代码来阅读 - 好的 C 代码不使用这些东西,这是有充分理由的。
You are headed into a wrong path. DO NOT make up your own cpp directives that are unfamiliar to others - this will make your code hard to understand, and at some point maintain.
Try to find some good C code to read - good C code does not use these things, for a good reason.
不要这样做。 其他人将无法读取您的代码。
作为一个警示示例,请查看 Steve Bourne 的 Bourne shell 原始来源,他使用宏以一种 pidgin Algol 风格编写代码。
DON'T DO IT. Nobody else will be able to read your code.
As a cautionary example, check out Steve Bourne's original sources for the Bourne shell, where he used macros to write the code in a kind of pidgin Algol style.
您可以这样做,但是这种“个人语言”在 C 世界中并不普遍使用,特别是如果您希望其他人将来能够阅读您的代码。
如果您只是为了自己做这件事,那么请随意
#define
任何您想要的东西,但预计一旦您开始与其他人(或为)其他人一起工作,您将无法继续使用这种东西。You could do this, but this sort of "personal language" is not generally used in the C world, especially if you expect anybody else to read your code in the future.
If you're doing this just for yourself, then feel free to
#define
whatever you want, but expect that once you start working with (or for) anybody else, you won't be able to continue using this sort of thing.不必要地使用 C 宏可能会导致您陷入痛苦的世界,尤其是当您尝试使用它来扩展代码时。 C 宏有一些用途,但不是这样。
编辑:我意识到我的回答与你的问题无关,但我想我应该提到这一点,因为你说你是一个 C 新手。 搜索“C 宏陷阱”以获得不使用宏的完整原因列表。 之前已经在此处讨论过。
Using C macros unnecessarily can lead you into a world of pain, especially if you attempt to use it to expand code. There are uses for C macros, but this is not it.
Edit: I realize that my answer is tangential to your question, but I thought I should mention this since you say you are a C novice. Search for "C macro pitfalls" to get a full list of reasons why not to use macros. It's been previously discussed here.
总的来说,我强烈同意其他受访者的观点,他们告诉您不要纯粹为了节省打字而定义自己的宏。 这种混淆是不值得的。 另外,您建议的特定宏是令人发指的。 然而,在斯特劳斯特鲁普的第一版中,他做了一些我更喜欢的事情(有时):
In general, I strongly agree with the other respondents who tell you not to define your own macros purely for the sake of saving typing. The obfuscation is not worth it. Also, the particular macros you suggest are heinous. However, in Stroustrup's 1st Ed, he does something I rather like (sometimes):
我习惯了 Python elif 结构,所以我经常定义以下内容:
我这样做的目的不是为了减少打字,而是为了保持缩进逻辑,同时保持一致的代码宽度(我不这样做)让我的代码长度超过 80 个字符)。 我这么说是因为对我来说......
应该是......
使用我的宏,这将变成:
I became accustomed to the Python elif construct, so I often define the following:
My purpose in doing this isn't to reduce typing, it's to keep indentation logical while maintaining consistent code width (I don't let my code go wider than 80 characters). I say this because to me this...
...should be...
With my macro this becomes:
将循环变量传递给宏总是更好。
块 - 宏具有某些优化问题。 所有编译器都不保证“块作用域”变量的优化 obj 代码。
例如,下面的代码在没有任何 gcc 优化选项的情况下进行编译时,会打印 &i 的两个单独的地址。 使用 -O2 选项编译时相同的代码将在两个块中打印相同的地址。
适当地命名语言结构可以使代码更具可读性。
我喜欢你的想法,如果你用以下方式表达的话。
在这里,无论它是如何编写的,宏 COLOR_ITER 从其名称来看都意味着您正在循环所有可用的颜色并为每种颜色执行“某些操作”。 这是一个非常易于使用的宏。
还有你的问题
正如每个人都解释的那样,在这种情况下预处理器不会帮助你。
您可以在键入代码时使用编辑器命令自动格式化代码。
It is always better to pass the loop variable to the macro.
A block - a macro has certain optimization problems. All compilers do not guarantee an optimized obj code for the "block scope" variables.
for example, the following code, when compiled with out any optimization options to gcc, prints two separate addresses for &i. And the same code when compiled with -O2 option will print the same address in both the blocks.
Naming the language constructs appropriately makes the code more readable.
I like your idea, if you put it in the following way.
Here, regardless of how it is written, the macro, COLOR_ITER, by its name, implies that you are looping for all available colors and doing "something" for each color. And this is a very easy-to-use macro.
And your quesion
As everybody explained preprocessor will not help you in this case.
You can use your editor commands to automatically format your code, as you type it.