When we define a variable name using #define in macro like: #define BUFFER_SIZE buff 5, and then in our main function declare a variable which has the same name like: int buff=8, Does the second buff overwrite the first one? I mean generally is a macro definition considered related to a non macro definition in main or are they totally separate?

Your macro is wrong

#define BUFFEFR_SIZE 5

You don't put a variable name in the macro

char buf = BUFFER_SIZE;

Edited 4 Years Ago by Ancient Dragon

Thanks you are right,mine was incorrect but what about the answer of my question. I mean can you overwrite a variable defined as a macro with a non macro variable?

Macros, like all preprocessor directives, are expanded before the compiler is run; thus, no, you cannot override a macro definition with a C language statement.

A bit more explanation may be in order here. When you go to compile a program in C (or C++), you are actually invoking at least two different programs (conceptually, anyway; not all C compilers work quite this way, but all have the same stages), each of whose output is piped to the next. The first program is call a preprocessor, which performs a series of textual transformations on the source code and passes the result on to the compiler proper. The preprocessor is, in effect, a separate language translator, which goes through the source code searching for the tokens of this sub-language in order to process them. In the case of a macro, what the pre-processor does is seach for the defined macro names in the source file, and replace them with the text of the macro. Thus, when you have the lines

#define BUFFEFR_SIZE 5

char buffer[BUFFER_SIZE];

The preprocessor replaces it with the code

char buffer[5];

Parameterized macros are only a little more complex:

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

int foo = 17;
int bar = 23;
int quux;

quux = MIN(foo, bar);

becomes

int foo = 17;
int bar = 23;
int quux;

quux = (((foo) < (bar)) ? (foo) : (bar));

Note all the parens around each sub-statement, there? That's because macro expansion is, in a word, blind; it does no checking of the values being expanded to make sure that the statements are valid. Macros are dangerous; they can be abused in any number of ways, which is why you generally want to avoid all but the simplest of them in new code.

Edited 4 Years Ago by Schol-R-LEA

don't use a macro to declare a variable -- just too confusing.

Does the second buff overwrite the first one

Depends on where the first one is declared. If its in the same block enclosed with { and } then there will be a compiler error for duplicate symbol. If the first one is somewhere else then the compiler may produce just a warning or nothing at all. In that case the second declaration will override the first.

Good question.

Write a short program and output the value. Does it compile properly? If so, what does it output?

@ Ancient Dragon: Thank you again for your answer. Actually, I am not going to use them in my program. I need to bring an example of overwritten. I tried lots of examples but I could not find one, I just get some errors. I would appriciate if you could help me to find one.

I need to bring an example of overwritten.

Then you need to explain what you mean by "overwritten" more clearly. Because the way you've described it so far, an example that actually compiles is impossible.

I mean overwrite the macro variable with a non macro variable. I am not sure whether there is any example or not but if any I would appreciate your help.

I mean overwrite the macro variable with a non macro variable

A macro isn't a variable. The name of a macro is physically replaced with its value in your file prior to compilation of the code. So if you have #define FOO 5, every use of FOO will be textually replaced with 5.

This article has been dead for over six months. Start a new discussion instead.