简体   繁体   中英

Is it possible to typedef a preprocessor directive?

Having recently began my studies of C, I have read about typedef and I understand that typedef is a alias name of an existing type.

Is it possible therefore to typedef a preprocessor directive? For example,

typedef #include I;

to be able to later do

I <stdio.h>

instead of

#include <stdio.h>

You almost answered this yourself: typedef is used to alias an existing type . It is not a crude text substitution mechanism. (Pithily put, one use for it is that is saves you from having to write struct all over the place: see typedef struct vs struct definitions )

But a preprocessor directive like #include is not a type, so it can't be used in the way you want.

Note also that for each translation unit , the preprocessing stage finishes before any compilation begins. Your idea looks reliant on the compiler interacting with the preprocessor. The language is not set up that way.

As a final remark, note that the typedef mechanism is substantially different in C and C++. In C, all the typedefs are injected into a separate namespace . See understanding C namespaces . This is all pretty complex stuff, and I've introduced a fair number of terms here, which I've italicised . Get to grips with the basics first.

Nope. as others have pointed out, pre-processor runs before compiler, and probably does not do anything special with that line (because it does not start with # , though you could use #define to change this).

Furthermore, you can't use macros for pre-processor directives. If you could, you could write #define I #include , but you can't, won't work. And even if you could, you definitely shouldn't (unless you were writing some intentionally obfuscated code or doing source code layout art or whatever), that'd be considered bad code by just about anybody.

If there is something you actually want to achieve (instead of just being curious or wanting to save typing), you could ask about your actual problem.

As I commented, you should work to make your C code readable by others (including yourself in 6 months from now). So using I <stdio.h> instead of #include <stdio.h> is really a bad idea (because your I is much less readable).

Is it possible therefore to typedef a preprocessor directive?

No that is not possible...

However, remember that you can always generate C code by other means (from other sources). In your case, I believe it would be a bad idea in practice, but you might consider using some other preprocessor -perhaps GPP or m4 - (or some script, perhaps even a simple awk one) to generate your foo.c file from eg some foo.mahendra one; then you'll modify your build procedure accordingly (if you use GNU make for build automation , you'll just add a dependency and a rule for that).

Read about the C preprocessor . Today, it is generally part of the compiler. In the previous century, it was a separate program /lib/cpp . Read documentation of GNU cpp . Read also more about C11 , eg the (draft) standard n1570 , and some sites like this one.

BTW, sometimes it is worth looking at the outcome of the preprocessor. Given some file foo.c you might run (assuming that GCC is your compiler) gcc -C -E foo.c > foo.i (perhaps with additional preprocessing options , like -I/usr/local/include , just after gcc ) to get into foo.i the preprocessed form of foo.c . That preprocessed foo.i file is a textual file that you can inspect with some editor or pager .

It is not possible to use a typedef to alias a preprocessor directive. typedef is a keyword, handled by the compiler, not the preprocessor. The preprocessor removes comments from your code and replaces the preprocessor directives such as #include with the appropriate output. When you compile ac program there are really three things that happen:

  1. First, the preprocessor removes the comments from your code and replaces the corresponding preprocessor directives such as #include with the appropriate preprocessor output.
  2. Then, the compiler takes the preprocessed code and outputs the compiled code into object files.
  3. Lastly, the linker processes the compiled code, combines the object files into one executable file if needed and links the executable file with the necessary libraries.

So although you can't use typedef to alias a preprocessor directive, you can (although you really probably shouldn't) do something like this:

uselessTypedef.h

int NUMBER

uselessTypedef.c

#include <stdio.h>

// note the whitespace between the typedef, #include and the semicolon
// typedef #include "uselessTypedef.h"; doesn't compile because of the way the preprocessor and compiler handle things
typedef
#include "uselessTypedef.h"
;

NUMBER main(NUMBER argc, char* argv[])
{
    NUMBER myint = 12345;
    printf("value of NUMBER myint: %d\n", myint);
    return 0;
}

this code compiles and prints value of NUMBER myint: %d\\n

The compiler never sees the following code:

typedef
#include "uselessTypedef.h"
;

Instead, the compiler sees this:

typedef
int NUMBER
;

The compiler ignores whitespace, so that code is equivelent to typedef int NUMBER;

( #include ) is an pre-processor macros and are lexical replacement tool for "including external file". Preprocessor are agnostic of language and have no understanding and what you are trying to do.

On the other hand typedef is a feature that lets you create your own alias for types. A typedef , in spite of the name, does not define a new type; it merely creates a new name for an existing type. For example, given:

typedef int my_int;

my_int is a new name for int ; my_int and int are exactly the same type. Similarly, given the struct definition above, you can write:

typedef struct foo foo;

The type already has a name, struct foo . The typedef declaration gives the same type a new name, foo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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