Search code examples
cc-preprocessor

Type of #define variables


If I have:

#define MAXLINE    5000

What type is MAXLINE understood to be? Should I assume it is an int? Can I test it somehow?

In general, how can one determine the type of #defineed variable?


Solution

  • It has no type. It is a simple text substitution. The text 5000 will be dropped in place wherever MAXLINE appears as a token.

    For example:

    int a = MAXLINE;
    

    will put the value 5000 in a.

    While

    char *MAXLINE2 = "MAXLINE";
    

    will not result in

    char *50002 = "5000";
    

    So, if you want type-checking, macro's are not the way to go. You will want to declare static constants instead, that way type-checking is done by the compiler.

    For information on the differences between static, const, and #define, there are many sources, including this question: Static, define, and const in C