Home >Backend Development >C++ >Convert C/C++ programs to preprocessor code

Convert C/C++ programs to preprocessor code

王林
王林forward
2023-09-11 16:21:07705browse

Convert C/C++ programs to preprocessor code

Here we will see how to generate preprocessing or preprocessor code from the source code of a C or C program.

To view the preprocessed code using the g compiler, we must use the '-E ' option with g .

The preprocessor contains all # directives in the code and also extends the MACRO function.

Syntax

g++ -E program.cpp

Example

<span class="com">#define</span><span class="pln"> PI </span><span class="lit">3.1415</span>
<span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
<span class="kwd">   float</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> PI</span><span class="pun">,</span><span class="pln"> r </span><span class="pun">=</span><span class="pln"> </span><span class="lit">5</span><span class="pun">;</span>
<span class="kwd">   float</span><span class="pln"> c </span><span class="pun">=</span><span class="pln"> a </span><span class="pun">*</span><span class="pln"> r </span><span class="pun">*</span><span class="pln"> r</span><span class="pun">;</span>
<span class="kwd">   return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span>
<span class="pun">}</span>

Output

$ g++ -E test_prog.cpp
int main() {
   float a = 3.1415, r = 5;
   float c = a * r * r;
   return 0;
}

The above is the detailed content of Convert C/C++ programs to preprocessor code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete