If a compiler has a certain type (ex. ptrdiff_t) as an embedded type, I don’t want to typedef it again. I know that below code does not work properly that I expected.
#ifndef ptrdiff_t
typedef long int ptrdiff_t;
#endif
How can I check a certain type is already defined in the C compiler?
In your question you are a bit confusing 2 different things:
There are built in types, like
int,float, etc. These are standard types and they are defined is all compilers. Types like__int64were introduced and standardized later. This means that they are defined in all recent compilers but only in some of the older compilers. You do not need to do anything to use them. At the same time you cannot figure out in your code if they are defined or not. This can be figured out only from the docs on the compiler. You can write:This approach allows you to create sort of
compiler independent environment.Besides the built in types, there are types that come from the headers. Some headers have constructs like:
Note that the macro-processor definitions stay apart from the definition of the type. You cannot check if the type is defined or not, but you can easily check if a macro definition is defined.
What headers are included in your code you deside yourself. This means that these definitions are not
in the compiler itself. They are in the set of definitions of the current translation unit. For compiler they have little difference from other type definitions that you write in your own code.Some compiler or system headers for not have “guarding defns” like in the example above. In this case the only thing that you can do is to track from what headers thay are coming and include/not include these headers, maby using your own
#ifdefguards around the#includestatements.