Products > Programming

how do you identify what are the preprocessor and macros in c program

(1/17) > >>


I don't understand difference between preprocessor and macros in cprogramming language after reading material on internet

I have written following code and I think value is preprocessor directive. compiler sustitute value 1 with text 

--- Code: --- #include<stdio.h>

#define value   1
#define add(y) y + 1

int main ()
int x = value;
int y = 5;
printf("x = %d \n", x);
printf("y = %d ", add(y));

return 0;
--- End code ---

x = 1
y = 6

What is add(y) y + 1 ? Is it macros ?

how do you identify what are the  preprocessor and macros in c program ?

It's "preprocessor macro", a macro handled by the preprocessor.  They are not different things.

The short answer is that there is no easy way to tell if a symbol refers to a macro or not.

You basically have to do what the C-preprocessor does... read all of the code that occurs before the use of the symbol and see if there are any #defines for that symbol.

In a lot of cases authors will use all upper case for macro names -- e.g. FOO(...) refers to a macro named FOO and not a function. A lot of constants in the C standard library are implemented this way, e.g. EOF, the SEEK_* constants, ...  On the other hand, there are also functions (or things you would expect to be a function) which are implemented as macros on some systems -- e.g. getc().

Macros are a relatively simple mechanism which allowed early C programmers to work around the limitations of the compiler. Nowadays in many instances there are much better ways to accomplish what macros were used for in the past. If you google "why macros are evil" you'll find discussions like this:

Unless someone used a preprocessor wrong,(1) for practical purposes it doesn’t matter if something is a macro expansion or an actual function call. So you may continue making a call without ever knowing if it’s really a call or text substitution. You should not assume either anyway, because it may change with another release of some library.

One thing you must understand and remember, when writing your own macros, is that the preprocessor is dumb text substitution. It has no idea about C syntax: it just replaces sequences of characters in code.

As for satisfying your curiosity and help with debugging, both gcc and clang have the -E option. It stops the compilation process after the preprocessing stage and spits out a preprocessed file without doing anything else. The output is usually messy, as it’s not meant to be regularly used by humans, but debugging at this level happens to be ugly.

(1) Which may include using it at all. ;)


--- Quote from: golden_labels on March 03, 2022, 10:52:20 am --- both gcc and clang have the -E option.

--- End quote ---

I am using windows 10 operating system. What is command for winows


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version