Type of #define variables

后端 未结 7 2168
一个人的身影
一个人的身影 2020-11-27 17:33

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?

相关标签:
7条回答
  • 2020-11-27 18:12

    We call this macro or preprocessor, which is used to string-replace source file contents. Read this: https://en.wikipedia.org/wiki/C_macro

    0 讨论(0)
  • 2020-11-27 18:15

    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

    0 讨论(0)
  • 2020-11-27 18:16

    Yes, you can assume it's an int.

    Well, actually all the other answers are correct. It's not C, it's just a directive that tells the preprocessor to do some textual substitutions, and as such it has no type. However, if you do not do any funky things with it (like the ## preprocessor trick), you will typically use MAXLINE like some kind of constant, and the preprocessor will replace it with 5000 which is indeed an explicit constant. And constants do have type: 5000 is an int. A constant written as a decimal integer, with no suffix (like U or L), will be interpreted by the compiler as an int, long int or unsigned long int: the first of these types that fits.

    But this has of course nothing to do with the preprecessor. You could rewrite your question as “what is the type of 5000?”, with no #define.

    0 讨论(0)
  • 2020-11-27 18:22

    It has no type. It's just a token which the preprocessor will put into the source code before passing the code to the compiler. You can do this (ridiculous) thing to declare a variable called x5000:

    #define APPEND(x,y) x ## y
    
    int main() {
            int APPEND(x,5000);
            x5000 = 3;
    }
    

    The preprocessor turns that into this before passing it the compiler proper:

    int main() {
            int x5000;
            x5000 = 3;
    }
    

    So, just because you see 5000 in a macro, it doesn't mean it needs to be numeric in any way.

    0 讨论(0)
  • 2020-11-27 18:26

    the compiler never sees that line of code, a preprocessor runs before the actual compilation and replace those macros with their literal values, see link below for more information

    http://www.cplusplus.com/doc/tutorial/preprocessor/

    0 讨论(0)
  • 2020-11-27 18:28

    (Very!) Broadly, your C compiler is going to perform 3 tasks when executed:

    1. Run a preprocessing pass over your source files,

    2. Run a compiler over the preprocessed source files

    3. Run a linker over the resulting object files.

    Lines starting with a #, like the line

    
    #define MAXLINE    5000
    

    is handled by the preprocessor phase. (simplistically) The preprocessor will parse a file and perform text substitutions for any macros that it detects. There is no concept of types within the preprocessor.

    Suppose that you have the following lines in your source file:

    
    #define MAXLINE    5000
    int someVariable = MAXLINE;     // line 2
    char someString[] = "MAXLINE";  // line 3
    

    The preprocessor will detect the macro MAXLINE on line 2, and will perform a text substitution. Note that on line 3 "MAXLINE" is not treated as a macro as it is a string literal.

    After the preprocessor phase has completed, the compilation phase will only see the following:

    
    int someVariable = 5000;        // line 2
    char someString[] = "MAXLINE";  // line 3
    

    (comments have been left in for clarity, but are normally removed by the preprocessor) You can probably use an option on the compiler to be able to inspect the output of the preprocessor. In gcc the -E option will do this.

    Note that while the preprocessor has no concept of type, there is no reason that you can't include a type in your macro for completeness. e.g.

    
    #define MAXLINE    ((int)5000)
    
    0 讨论(0)
提交回复
热议问题