1

I am going through book [Let us C-by Yashwant Kanetkar ], here it stated:

When we compile a program, before the source code passes to the compiler, it is examined by the C preprocessor for any macro definition. When it sees the #define directive, it goes through the entire program in search of macro templates; wherever it finds one, it replaces the macro template with the appropriate macro expansion. Only after this procedure has been completed, is the program handled over to the compiler.

My question is that, before the program is passed to compiler, how can Preprocessor program is able to read the TOKENS corresponding to the macro templates? Is preprocessor program also able to divide the program into TOKENS.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Minion
  • 964
  • 14
  • 16
  • 6
    Close the book and put it aside. Don't throw it away! If you ever have a chance to meet the author, take the book and beat him over the head with it. That's the only way this book can be useful. Now find yourself [a proper C textbook](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and get started. Yes I have looked at the book (don't ask). – n. m. could be an AI Feb 18 '17 at 16:24
  • Your question is not clear? Are you referring to the tokenization? – Rishi Feb 18 '17 at 17:22

2 Answers2

3

That description is confusing (so I won't recommend that book; read instead the K&R The C Programming Language book). The preprocessor does not go through the entire program, it has previously processed some input. Only past preprocessed input matters for the behavior of the preprocessor (in other words, the preprocessor is a single-pass mechanism).

Read wikipage on C preprocessor, then read documentation of GNU cpp and other documentation on preprocessor, and the wikibook chapter on C programming/Preprocessor.

In current C compilers (for performance reasons) the preprocessor is no longer a separate program, it is part of the compiler itself. For recent GCC look into libcpp/ (its preprocessor library, internal to the compiler).

If using the GCC compiler, you can get the preprocessed form of your source code file csource.c by running gcc -C -E csource.c > csource.i then looking inside the generated preprocessed form csource.i (e.g. with a pager or an editor).

(I strongly recommend doing that once in a while; you'll learn a lot; and yes, you could be surprised by the amount of code pulled by a usual #include <stdio.h> directive)

I believe your book is explaining wrongly. The preprocessor handles every preprocessing directive. When it encounters a #define it stores in some preprocessor symbol table the definition of that symbol. When it encounters after that #define an occurrence of that preprocessor symbol, it does the appropriate substitution.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • how preprocessor is able to locate all the Macro Template TOKENS in the source code.? this is my questions , please help me out guys its really very confusing to me . – Minion Feb 18 '17 at 16:13
  • It does not work like you believe. Please follow the several links I have given. My feeling is that the book you are reading is not very good. – Basile Starynkevitch Feb 18 '17 at 16:14
  • Which book i should prefer according to you ? Thank-you – Minion Feb 18 '17 at 16:18
  • 2
    @Matec009: I learned from K&R 1st Edition 5+ years before the 2nd Edition was released — there wasn't any serious discussion back then. These days, K&R 2nd is dated in places — it covers C90 (not C99 or C11), and has examples in the Unix chapter that no longer work. I'd provisionally suggest [C Programming: A Modern Approach (2nd Edition)](http://knking.com/books/c2/index.html) by K. N. King. I've got it; I've read most of it; I didn't find anything awful in it. Otherwise, go to [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303) and choose. – Jonathan Leffler Feb 18 '17 at 21:30
1

In book K & R The C Programming Language.

Page No: 88

C provides certain language facilities by means of a preprocessor, which is conceptually a separate first step in compilation.

In book Compiler Principles, Techniques and Tools by Aho, Lam, Sethi and Ullman

Page No. 3

The task of collecting the source program is sometimes entrusted to a separate program, called a preprocessor. The preprocessor may also expand shorthands, called macros, into source language statements. The modified source program is then fed to compiler.

In GCC GNU Documentation

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation.

Andn read this too.

So from these three official sources, one can say that the Preprocessor is a separate program run by Compiler. So in book Let Us C by Yashwant P Kanetkar that Preprocessor is a program that processes before the compiler as its name suggests is no wrong, and the expanded code can be seen in file.i.


Now let's come to your question,

In book K & R The C Programming Language.

Page No: 89

Substitution are made only for tokens and do not take place within quoted strings.

and as Basile told in his answer that

In current C compilers (for performance reasons) the preprocessor is no longer a separate program, it is part of the compiler itself.

and compiling is a long process that passes through several phases, Preprocessor actually comes after the program is converted in tokens, but as sources says that it is the process of before compilation that means it is done before any kind of intermediate code generation, and yes, breaking program into tokens is the first step of compiler before any intermediate code generation.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
  • 1
    The preprocessor *could be* (and historically was, as `/lib/cpp`) a *separate* program. Actually, it is now incorporated in the compiler (e.g. look into source code of [GCC](http://gcc.gnu.org/) for its `libcpp/`...). – Basile Starynkevitch Feb 25 '17 at 14:37
  • BTW, I am a man (and grand-father 7 times in 2017). So you could `s:his/her:his:` in your answer! `Basile` is the French (& official) spelling of my Christian name. My patron saint is [Basil the Great](https://en.wikipedia.org/wiki/Basil_of_Caesarea) of Caesarea (English spelling) – Basile Starynkevitch Feb 25 '17 at 14:38
  • Yeah, and if the conversion of the program into tokens is the task of the compiler, preprocessor only replaces tokens and preprocessor is the process before compilation, these statements are contradictory. So here the meaning of **before** compilation means before **intermediate code generation**. Btw I edited my answer to `his`. :) – Siraj Alam Feb 25 '17 at 14:41