Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 803
James Raitsev Avatar asked Dec 21 '11 02:12

James Raitsev


People also ask

What is type of in JavaScript?

Typeof in JavaScript is an operator used for type checking and returns the data type of the operand passed to it. The operand can be any variable, function, or object whose type you want to find out using the typeof operator.

Why is typeof null object?

In JavaScript, typeof null is 'object', which incorrectly suggests that null is an object (it isn't, it's a primitive value, consult my blog post on categorizing values for details). This is a bug and one that unfortunately can't be fixed, because it would break existing code.

Does typeof return a string?

typeof is generally always guaranteed to return a string for any operand it is supplied with. Even with undeclared identifiers, typeof will return "undefined" instead of throwing an error.

Is typeof C#?

The typeof is an operator keyword which is used to get a type at the compile-time. Or in other words, this operator is used to get the System. Type object for a type.


2 Answers

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

like image 197
tpg2114 Avatar answered Oct 10 '22 00:10

tpg2114


(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) 
like image 32
Andrew Edgecombe Avatar answered Oct 10 '22 02:10

Andrew Edgecombe