简体   繁体   English

个人预处理程序指令

[英]Personal Preprocessor Directives

Being a C novice I would like to hear what Macro "define"s developers are using. 作为一个C新手我想听听宏“定义” S开发者使用的是什么。 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? 我可以在这些宏中添加\\ n \\ t等吗? 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. 还链接到良好的预处理程序技巧和窍门。

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 可以通过转义换行符来添加空格

#define SOMETHING whatever\
This is part of the macro

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. 查看编辑器宏会更好,因此您可以键入快捷方式并让编辑器展开它。

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. 不要编写自己不熟悉的cpp指令-这会使您的代码难以理解,并在某些时候进行维护。

Try to find some good C code to read - good C code does not use these things, for a good reason. 尝试找到一些不错的C代码以供阅读-出于充分的理由,好的C代码不会使用这些东西。

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. 作为一个警告示例,请查看Steve Bourne的Bourne shell原始资源 ,他在其中使用宏以pidgin Algol风格编写代码。

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. 可以这样做,但是在C语言世界中通常不使用这种“个人语言”,特别是如果您希望将来会有其他人阅读您的代码。

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. 如果您只是为自己执行此操作,则可以#define您想要的任何内容,但是期望一旦与(或为)其他任何人一起工作,您将无法继续使用这种方法。

Using C macros unnecessarily can lead you into a world of pain, especially if you attempt to use it to expand code. 不必要地使用C宏会使您陷入痛苦的世界,尤其是当您尝试使用它来扩展代码时。 There are uses for C macros, but this is not it. C宏有用途,但事实并非如此。

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. 编辑:我知道我的答案与您的问题有关,但我想我应该提一下,因为您说您是C新手。 Search for "C macro pitfalls" to get a full list of reasons why not to use macros. 搜索“ C宏陷阱”以获得不使用宏的原因的完整列表。 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): 但是,在Stroustrup的第一版中,他做了一些我喜欢的事情(有时):

#define Kase break; case

I became accustomed to the Python elif construct, so I often define the following: 我习惯了Python elif构造,因此经常定义以下内容:

#define elif(test) else if(test)

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). 我这样做的目的不是减少键入,而是在保持一致的代码宽度的同时保持缩进逻辑(我不允许我的代码宽度超过80个字符)。 I say this because to me this... 我说这是因为对我来说...

if(...) ...
else if(...) ...
else ...

...should be... ...应该...

if(...)
{
    ...
}

else
    if(...)
    {
        ...
    }

    else
    {
        ...
    }

With my macro this becomes: 使用我的宏,它变为:

if(...)
{
    ...
}

elif(...)
{
    ...
}

else
{
    ...
}

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. 所有编译器都不保证“块范围”变量的优化obj代码。

for example, the following code, when compiled with out any optimization options to gcc, prints two separate addresses for &i. 例如,以下代码在编译时不包含gcc的任何优化选项时,将为&i打印两个单独的地址。 And the same code when compiled with -O2 option will print the same address in both the blocks. 使用-O2选项编译时,相同的代码将在两个块中打印相同的地址。

{
    int i;
    printf("address of i in first block is %u\n", &i);
}
{
    int i;
    printf("address of i in sec block is %u\n", &i);
}

Naming the language constructs appropriately makes the code more readable. 适当地命名语言构造会使代码更具可读性。 I like your idea, if you put it in the following way. 如果您采用以下方式,我喜欢您的想法。

#define GREEN 1
#define YELLOW 2
#define RED 3

# define NUM_COLORS 3

#define COLOR_ITER (color,i)           \
    for(i=GREEN, color = colors[i];    \
        i < NUM_COLORS;                \
        color = colors[++i])

int colors[3] = {GREEN, YELLOW, RED};

int
fun () {

    int j;
    color_t clr;

    COLOR_ITER(clr, j) {
        paint(clr);
    }

}

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. 在这里,无论其写法如何,宏(按其名称)COLOR_ITER都意味着您正在循环查找所有可用的颜色,并对每种颜色执行“操作”。 And this is a very easy-to-use macro. 这是一个非常易于使用的宏。

And your quesion 还有你的问题

Can I use the proprocessor to replace my sourcecode with my personal include to formatted source without the include ? 我可以使用预处理器将我的源代码替换为我的个人include到没有include的格式化源吗?

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. 您可以在输入代码时使用编辑器命令自动设置代码格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM