12.2 – Generating expanding source code (intermediate source code)

by subbu on December 15, 2013

Expanding source or intermediate code:

In the previous session we have learned that, a program called preprocessor process the source code before compilation of the program and  generates the expanded source code or intermediate code, which is saved with .i extension (prog.i). In this session we are going to discourse how expanded source in different platforms can be generated

How to generate expanded source in Turbo C

In the build process, output of preprocessor would be expanded or intermediate code. We can tap the intermediate code using a tool called CPP (C pre processor) in Turbo C. Input to the CPP is a C file and output of the CPP is an intermediate file. Configure turbo C as explained in the previous session under How to execute a C program in command prompt using turbo C compiler.

1. Open the text editor to write the program

>edit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

#define OUT printf
int main()
{
 OUT("Hello\n");
 OUT("World");
 return 0;
}

3. Turbo C provides a tool called “CPP”. It accepts a C program as the input and generates equal expanded source.

>cpp prog.c

4. Now we can see the expanded or intermediate code

>edit prog.i
prog.c 1:
prog.c 2: int main()
prog.c 3: {
prog.c 4: printf("Hello\n");
prog.c 5: printf("World");
prog.c 6: return 0;
prog.c 7: }
prog.c 8:
prog.c 9:

5. Compile the program using Turbo C compiler

>tcc prog.c

6. Execute the program

>prog

Hello World

Expanding source code

How to generate expanded source in Windows using gcc

Now we will see how we can generate expanded or intermediate source using an option -E using MinGW gcc compiler on windows command prompt

1. Open the text editor to write the program

>edit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor

#define OUT printf
int main()
{
 OUT("Hello\n");
 OUT("World");
 return 0;
}

3. Compile the program using “gcc”. Note: -E is a compiler option to generate expanded code -o is a compiler option to generate intermediate file with file name (prog.i)

>gcc -E -o prog.i prog.c

4. Now we can see the expanded or intermediate code using text editor

>edit Prog.i
# 1 "prog.c"
# 1 ""
# 1 "prog.c"
int main()
{
 printf("Hello\n");
 printf("World");
 return 0;
}

Expanding source with MinGW

How to generate expanded source in Linux using gcc

Now we will see how we can generate expanded or intermediate source using an option -E using gcc compiler in Linux

1. Open the text editor to write the program

$gedit prog.c

2. Type the program, save (alt+f, s) and exit (alt+f, q) from the editor

#define OUT printf
int main()
{
 OUT("Hello\n");
 OUT("World\n");
 return 0;
}

3. Compile the program using “gcc”. Note: -E is a compiler option to generate expanded code -o is a compiler option to generate intermediate file with file name (prog.i)

$gcc -E -o prog.i prog.c

4. Now we can see the expanded or intermediate code using text editor

>gedit Prog.i
# 1 "prog.c"
# 1 ""
# 1 "prog.c"
int main()
{
 printf("Hello\n");
 printf("World");
 return 0;
}

Expanding source in Linux

Previous post:

Next post: