#define's scope?

hello everyone I am now writing a few routines to test some of my gyro code. I was wondering if anyone knew what the scope of the #define was, I am calling it at the top of my function but I want to use the same name but different numbers inside a different function in the same file. I know I could just rename them and is something I think I might do, but could someone tell me what the scope of the #define command is? is it global to the file? function? inside a conditional? or global to the project?

#define is a preprocessor command, the scope is through whatever file(s) you include it in. Least that is how it works if you have them in .h files. If your #define is in a .c file it is global to the file. Hope this helps.

If you want to use different values I would suggest just using a global variable. What #define does is when you compile your code the compiler replaces wherever you have what your define is with its value.

example

#define VARIABLE rc_dig_in01;


printf("%d",VARIABLE);

will look like this after you compile it



printf("%d",rc_dig_in01);

Actually #define works from the place where it was defined to any other files being processed afterwards.

This is why you typically have the following statement in a header file:
#ifndef _GYRO_H
#define _GYRO_H
{code}
#endif

What this does is tell the pre processor to only compile each header file once… so the preprocessor keeps the #define in an internal lookup table while processing files.

If you are not familiar with how compiling works here is a brief overview:
First the pre processor goes over all the files before compile time and creates a new file which conforms to what is told using #statements … (i.e replace all instances of VARIABLE with rc_dig_in01)

You then typically have a main program, this main program may use additional functionality from other files, in order to access variables and functions from another file it uses a “header” the header is just a list of what is available.

This header is then connected to an actual C file which contains the functionality.
When the compiler compiles a single C file (whether it is main, or another file) it creates an intermediate .o file which contains assembly instructions and information for the linker on where to find functions that are referenced that are in another C file.

What the preprocessor directive i mentioned above does is prevent the same header from being re-included twice (and thus re-introducing the same variable and function names creating a conflict)

The only thing I would add to this is that it is only global after the line on which it is defined. I suppose this is the same as any other global variable, except global variables have to be defined at the top of the file, or the top of the function. #defines can be used anywhere.

I suppose this is the same as any other global variable, except global variables have to be defined at the top of the file, or the top of the function. #defines can be used anywhere.
#define does not create a variable, code is not generated, memory is not allocated. It is basically a command to the preprocessor to substitute one phrase for another.

ok thanks now lets see if anyone knows the answer to this one. If I have a code structure like so.

#define VAR 100
{code1}
#define VAR 50
{code2}

Which the compiler doesn’t crash on me, i’ve tried this, in the second segment of code(code2) what does the compiler put in place of VAR. 50 or 100? Is this an effective way to have my cake and eat it too?

I don’t see why you are using the #Define anyway. If you want to modify it in your code just make it a global variable. If you want one constant in the top and one constant in the bottom then make the #Define a different name.

I understand but I just want to know. :rolleyes:

What compiler are you using? All the compilers I’ve ever worked with would generate an error for redefining a pre-processor directive.

I am using the norm, mplab 7.2 and c18 2.4. Nothing unusual, if this clarifys any thing I am doing each one in a separate function.

No, the preprocessor doesn’t understand functions or anything about the code for that matter. All it understands is text. If both of those functions are in the same file, that should be causing an error. If not, then I don’t know. If I had MPLAB in front of me, I’d give it a shot right now. Since I don’t, I’d recommend printf-ing the value after each #define statement. That should tell you what you want to know.

You can also make cool little macro functions in the preprocessor. :smiley:

I did a quick test in Dev-C++ and all it does is give me a warning about the two #defines, and it does change the value to the second one. It would probably do the same thing in MPlab