Constants C

If I declare a constant, let's say

"# define MONTHS = 12" in C I know that the preprocessor directive will replace everything that MONTHS uses, but want to know if memory is allocated for storage 12 ?, If so, what will the label be and what will the data type be.

+4
source share
9 answers

You most likely want your definition as such:

  #define MONTHS 12

 / * some code here ... * /

 int payAnnual = payMonthly * MONTHS;

To answer your question, memory will not be used. The preprocessor is not aware of concepts such as variables and memory. This is essentially an automated text editor. It will replace any occurrence of the MONTHS character with 12.

Since the pre-processor is so dumb, it is usually preferable to use a constant variable. This gives you the benefit of type checking and makes it easier to read compiler errors. And while you declare it static, the variable will be optimized. (Unless you declare a global static variable in C, it will be exported by default, so the compiler will not be able to optimize it completely.)

  static const int MONTHS = 12;
+20
source

The syntax is actually:

#define MONTHS 12 

No = Sign allowed. And in the compiled code for MONTHS, no memory is allocated, and it does not have a data type.

+9
source

The preprocessor simply makes symbolic changes to the source code. Consequently, the code will behave as if you just replaced all occurrences of MONTHS with yourself. And, therefore, it does not affect the use of memory.

Using compiler pre-definitions is a great way to increase the level of abstraction of your code without memory of performance impact.

Yours faithfully

+6
source

No memory is allocated for preprocessor characters. If you ever use it as a value somewhere, this can cause an appropriate distribution.

For instance:

 char something[1024*MONTHS];// 1024*12 bytes 

otherwise, the constant never survives compilation time. And it does not have a data type.

+3
source

Not #define - just find the n replacement with the preprocessor before it gets to the compilation stage. It replaces all instances of the symbol with a value throughout the source. Then the compiler takes over.
Therefore, do not think that memory is allocated.

+3
source

The whole point of the C preprocessor is that it does the replacement before compiling the code. therefore, # -determined constants are not stored at the point at which they are defined only when they are used.

+3
source

They are used in the same way as the actual integer, and the regular literal type for the integer is used in calculations where the macro name is displayed in the program.

Just as if you were announcing

 #define FOO "bar" 

Then the FOO entries will be replaced with the typical use of "bar", which is a pointer to some location of the string constant in the file of the program object that is mapped to memory.

There really is no difference.

+2
source

No,

 weeks = 12 * 4; 

In the same way as:

 weeks = MONTHS * 4; 

Is there 12 memory? No, therefore MONTHS.

0
source

if 12 is allocated, it is usage dependent and has nothing to do with the preprocessor at all

 Somefunction(MONTH); 

will expand to

 Somefunction(12); 

12 here must be saved somewhere, in which case it will be saved in an executable file. The type will probably depend on what Somefunction takes as an argument (or what the compiler decides is suitable for this use). There is no label for the character "12".

Pre-processing is done before the actual compilation, as someone already wrote, it looks like an automated text editor. Thus, the compilation stage does not completely know what the preprocessor did.

0
source

All Articles