Setting the scope of a MACRO

Go To StackoverFlow.com

2

I have a general doubt ..

Is there a way we limit the scope of a MACRO within a .C file just like a static function ?

2012-04-04 03:39
by codingfreak


2

You can place the macro in the .c file where you want it to be used instead of a header file and it won't be accessible from other files (although some compilers allow inclusion of .c files but no one does that, well no one that's sensible).

Also mentioned below is the use of #undef but that can quickly start to get messy if you use that macro a lot.

2012-04-04 03:42
by Jesus Ramos
Well, every compiler allows inclusion of .c files. And plenty of people do it, because sometimes it is sensible. Your basic answer of restricting it via translation unit seems reasonable - Carl Norum 2012-04-04 03:48
@CarlNorum Well some people use StyleCop or FxCop where you can actually cause that to be a compiler error, at least in industry anyways. Never really thought of a scenario where including a .c file was applicable or couldn't be separated into including a .h file with exposed prototypes - Jesus Ramos 2012-04-04 03:49
Neither of those tools have anything to do with C, do they? And they're certainly not compilers - Carl Norum 2012-04-04 03:50
@CarlNorum That's true but you can apply them to C++ (at least FxCop IIRC). What's an example where including the .c file is a better way than including a prototype via a .h - Jesus Ramos 2012-04-04 03:52
Sometimes you don't want to include just prototypes. For instance, maybe you have some static data that isn't really a "header", and you want to include it in a specific translation unit. Filename extensions don't have anything to do with the language - that's all I mean - Carl Norum 2012-04-04 03:54
@CarlNorum The file extension thing is true. I can see that happening but that might become problematic if you multiply define symbols because of that inclusion. Just strikes me as odd, only ever seen that once when grading assignments and it just struck me as odd - Jesus Ramos 2012-04-04 03:58


3

Macros are done by the pre-processor. The pre-processor reads all files being processed and applies macros and macro logic, the results of which are then passed to the compiler.

Once a macro is defined, its value will be used everywhere the macro is referenced, even in other files.

Please see the GCC Documentation for details regarding macro usage.

2012-04-04 03:46
by Ariel


3

The general practice is to #undef the macro when you're done with it. Error prone, but it works.

Macros don't have any sort of block scope.

2012-04-04 03:47
by Pubby


2

All macros are already like static functions, in that they can only be used in the translation unit in which they're defined. If you want to restrict the areas where you can use a particular macro, just define it in a sensible place.

2012-04-04 03:51
by Carl Norum


0

a macro is evaluated by the preprocessor, not by the compiler. it doesn't know anything about compilation units, so you cannot restrict it's use to one. instead it is evaluated within the translation unit.

the macros life cycle starts in the line it is defined (all lines above it do know nothing about the macro), and it ends either at the end of the translation unit or whenever it get's undefined using "#undef"

2012-04-09 14:48
by umläute


0

All C macros are limited to the translation unit (a single C file) unless they are defined in a header and being included to every translation units.

Unfortunately, a translation unit is often big, easily hundreds to thousands of lines of code, and macros are context dependent and it would be much more useful if it can be limited to much smaller context (such as a block scope). Lacking scope limits macro usage in C, mostly global constants, a few universal simple routines, and often need all capital names or some trick to manage pollutions).

However, higher order functions can be easily achieved with macros. Think about how we use natural language, where we may use "it" to refer any thing too long to repeat within the context. A scoped macro system will enable the same ability.

I have developed MyDef, which is essentially a scoped macro system.

2015-04-15 13:10
by Hui Zhou
Ads