Priyanka
C PROGRAM
BUILDS
C PROGRAM BUILD PROCESS
Step 1.First we write a code on editor and save it (say hello.c).The saved code goes in secondary memory. The editor used is vim editor .Command to enter in the vim editor is
# vim.
To enter in insert mode we press “i”
To save the code we use “esc” then “: w filename.c”
To exit the editor press “: q”
To compile code #gcc filename.c
Step2.The preprocessor scan at hello.c and work on the line that starts with # and fetch the content of header file that is included and join with the code written.
A file with named hello.i is generated (hello.i would be combination of hello.c+ stdio.h (if this header is included in the code ex #include<stdio.h>)).
Step3.Then it goes to compiler (gcc) and hello.s file is produced .hello.s(when we expand .s extention file it consist of )
→data
→code
→symbol table–>declaration
–>definition
–>link
Step4.This file goes to assembler. Here hello.o file is generated and this goes to linker .
Step5.The linker link the symbol table link section with c libraries.
Step6.Finally the the default executable file(a.out) is generated from linker .
PROGRAM EXECUTION
(To execute code #./a.out)
1.Programs are run on memory(RAM).
2.For that purpose to fetch the program from secondary memory to RAM we need loader .
3.The region of memory occupied in RAM is called Process Content .
4.Logical Address:
Stack segment -this contains local data.
File description-
0 – stdin - standard input -from here we take input .
1 - stdout- standard output -from here we give output
2 – stderr -standard error -from here error is out
Data segment- consist of global data ,static variable, extern variable , register variable ,volatile variable, constants and preprocessor
Code segment – this consist of compiled code
There is the 4 steps
included:-
Preprocessor
Compiler
Assembler
Linker
STEP:
1
PREPROCESSOR
hello.c
file is generated which is scan by preprocessor.
Preprocessor:-
Start with # and it works only on the lines in program. And stdio.h
is header file.
STEP:
2
COMPILER
hello.c
file go to the compiler to compile (#gcc) it and created a new file
hello.s
hello.s
file expand it contains
Data
Section
Code
Section Declaration
Symbol
Table Definition ----> Declaration
---->
Definition
---->
C Libraries
STEP:
3
ASSEMBLER
hello.s
file is go to the assembler it generate the hello.o file.
Assembler
work to convert high language into machine language.
STEP:
4
LINKER
Then
Linker link the symbol table with C Libraries.
STEP:
5
At
last the a.out file is generated from the linker
Explain the complete c program build process in detail.
Answer :-
The C compilation
process converts the source code taken as input into object code .
The compilation
process can be divided into four steps -
a) pre-processor –
It scan the source code file i.e filename.c and generated filename.i
which comprises of filename.c and stdio.h(header files).
b) Compiler – It
takes input as filename.i(which is output of pre-processor) to gcc
compiler for compilation. It generated filename.s as output and when
we expand .s file it consist of 5 stage and 2 pass that is following
-
5 stage – i)
Syntax Analysis
ii) Lexical Analysis
iii) Intermediate
code generation
iv) Code
Optimisation
v) Object code
generation
2 pass – i) It
creates a Symbol table consist of decleration , libraries and their
links.
ii) It also
gnm-glibe pass.
c) Assembler – It
takes filename.s as input and generate filename.o file which goes to
linker .
d) Linker – It
links the symbol table link ,filename.o with c libraries and generate
executable file i.e a.out.
Find 20 compile error from above program.
1.
Removed semicolon
from return statement : -
#include<stdio.h>
int main()
{
printf(“hello
world \n”);
return 0
}
Compiler error -
: In function
‘main’:
hello.c:5:11: error:
expected ‘;’ before ‘}’ token
5 | return 0
| ^
| ;
6 | }
| ~
2.
Remove “” from
printf statement -
#include<stdio.h>
int main()
{
printf(hello
world \n);
return 0 ;
}
Compiler error -
hello.c: In function
‘main’:
hello.c:4:10: error:
‘Hello’ undeclared (first use in this function); did you mean
‘ftello’?
4 |
printf(Hello World\n");
|
^~~~~
|
ftello
hello.c:4:10: note:
each undeclared identifier is reported only once for each function it
appears in
hello.c:4:15: error:
expected ‘)’ before ‘World’
4 |
printf(Hello World\n");
|
^~~~~~
|
)
hello.c:4:21: error:
stray ‘\’ in program
4 |
printf(Hello World\n");
|
^
hello.c:4:23:
warning: missing terminating " character
4 |
printf(Hello World\n");
|
^
hello.c:4:23: error:
missing terminating " character
4 |
printf(Hello World\n");
|
^~~
hello.c:5:12: error:
expected ‘;’ before ‘}’ token
5 | return 0;
| ^
| ;
6 | }
| ~
3).Remove () from main
function -
#include<stdio.h>
int main
}
Compiler error -
hello.c:3:1: error:
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘{’ token
3 | {
| ^
4).
Remove . from
library function -
#include<stdioh>
int main()
{
printf(“hello
world \n”);
return 0;
}
Compiler error -
hello.c:1:9: fatal
error: stdioh: No such file or directory
1 |
#include<stdioh>
|
^~~~~~~~
compilation
terminated.
5).
Remove #from
preprocessor directive -
include<stdio.h>
int main()
{
printf(“hello
world \n”);
return 0;
}
Compiler error -
hello.c:1:8: error:
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘<’ token
1 |
include<stdio.h>
| ^
6).
Remove <> from
library function-
int main()
{
printf(“hello
world \n”);
return 0;
}
Compiler error -
hello.c:1:2: error:
invalid preprocessing directive #includestdio
1 |
#includestdio.h
|
^~~~~~~~~~~~
hello.c: In function
‘main’:
hello.c:4:3:
warning: implicit declaration of function ‘printf’
[-Wimplicit-function-declaration]
4 |
printf("Hello World\n");
| ^~~~~~
hello.c:4:3:
warning: incompatible implicit declaration of built-in function
‘printf’
hello.c:1:1: note:
include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include
<stdio.h>
1 |
#includestdio.h
7).
Remove } in main
function-
#include<stdio.h>
int main()
{
printf(“hello
world \n”);
return 0;
Compiler error -
hello.c: In function
‘main’:
hello.c:5:3: error:
expected declaration or statement at end of input
5 | return 0;
| ^~~~~~
8).
Remove { in main
function-
#include<stdio.h>
int main)
printf(“hello
world \n”);
return 0;
}
Compiler Error :-
hello.c: In function
‘main’:
hello.c:4:3: error:
expected declaration specifiers before ‘printf’
4 |
printf("Hello World\n");
| ^~~~~~
hello.c:5:3: error:
expected declaration specifiers before ‘return’
5 | return 0;
| ^~~~~~
hello.c:6:1: error:
expected declaration specifiers before ‘}’ token
6 | }
| ^
hello.c:6: error:
expected ‘{’ at end of input
6 | }
|
9).
Remove {} in main
function-
#include<stdio.h>
int main()
printf(“hello
world \n”);
return 0;
Compiler Error :-
hello.c: In function
‘main’:
hello.c:4:3: error:
expected declaration specifiers before ‘printf’
4 |
printf("Hello World\n");
| ^~~~~~
hello.c:5:3: error:
expected declaration specifiers before ‘return’
5 | return 0;
| ^~~~~~
hello.c:6: error:
expected ‘{’ at end of input
6 |
|
10).
Remove < from
Library function-
int main()
{
printf(“hello
world \n”);
return 0;
}
Compiler Error :-
hello.c:1:2: error:
invalid preprocessing directive #includestdio
1 |
#includestdio.h>
|
^~~~~~~~~~~~
hello.c: In function
‘main’:
hello.c:4:3:
warning: implicit declaration of function ‘printf’
[-Wimplicit-function-declaration]
4 |
printf("Hello World\n");
| ^~~~~~
hello.c:4:3:
warning: incompatible implicit declaration of built-in function
‘printf’
hello.c:1:1: note:
include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include
<stdio.h>
1 |
#includestdio.h>
11).Remove > from
Library function-
#include<stdio.h
hello.c:1:17: error:
missing terminating > character
1 |
#include<stdio.h
|
^
12).
Remove “ from
printf-
#include<stdio.h>
Compiler Error :-
hello.c: In function
‘main’:
hello.c:4:10:
warning: missing terminating " character
4 |
printf("Hello World\n);
| ^
hello.c:4:10: error:
missing terminating " character
4 |
printf("Hello World\n);
|
^~~~~~~~~~~~~~~~
hello.c:5:3: error:
expected expression before ‘return’
5 | return 0;
| ^~~~~~
hello.c:5:12: error:
expected ‘;’ before ‘}’ token
5 | return 0;
| ^
| ;
6 | }
| ~
13).
Remove “ from
printf-
#include<stdio.h>
int main()
{
printf(hello
world \n”);
return 0;
}
Compiler Error :-
hello.c: In function
‘main’:
hello.c:4:10: error:
‘Hello’ undeclared (first use in this function); did you mean
‘ftello’?
4 |
printf(Hello World\n");
|
^~~~~
|
ftello
hello.c:4:10: note:
each undeclared identifier is reported only once for each function it
appears in
hello.c:4:15: error:
expected ‘)’ before ‘World’
4 |
printf(Hello World\n");
|
^~~~~~
|
)
hello.c:4:21: error:
stray ‘\’ in program
4 |
printf(Hello World\n");
|
^
hello.c:4:23:
warning: missing terminating " character
4 |
printf(Hello World\n");
|
^
hello.c:4:23: error:
missing terminating " character
4 |
printf(Hello World\n");
|
^~~
hello.c:5:12: error:
expected ‘;’ before ‘}’ token
5 | return 0;
| ^
| ;
6 | }
| ~
14).
Remove 0 from return
Statement-
#include<stdio.h>
int main()
{
printf(“hello
world \n”);
return ;
}
Compiler Error :-
hello.c: In function
‘main’:
hello.c:5:3:
warning: ‘return’ with no value, in function returning non-void
5 | return;
| ^~~~~~
hello.c:2:5: note:
declared here
2 | int main()
| ^~~~
16).
Remove ( from main
function -
#include<stdio.h>
int main(
{
printf(“hello
world \n”);
return 0 ;
}
Compiler Error :-
hello.c:2:9: error:
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘)’ token
2 | int main)
| ^
17).
Remove ) from main
function -
#include<stdio.h>
int main)
{
printf(“hello
world \n”);
return 0;
}
Compiler Error :-
hello.c:3:1: error:
expected declaration specifiers or ‘...’ before ‘{’ token
3 | {
| ^
18).
Write \n after “”
in printf -
#include<stdio.h>
int main()
{
printf(“hello
world” \n);
return 0;
}
Complier Error :-
hello.c: In function
‘main’:
hello.c:4:23: error:
stray ‘\’ in program
4 |
printf("Hello World"\n);
|
^
hello.c:4:23: error:
expected ‘)’ before ‘n’
4 |
printf("Hello World"\n);
|
^~
|
)
19).
Remove ; from printf
-
#include<stdio.h>
int main()
{
printf(“hello
world\n” )
return 0;
}
Complier Error :-
hello.c: In function
‘main’:
hello.c:4:26: error:
expected ‘;’ before ‘return’
4 |
printf("Hello World\n")
|
^
|
;
5 | return 0;
| ~~~~~~
20).
Remove #,<>
from header line -
includestdio.h
int main()
{
printf(“hello
world\n” )
return 0;
}
Complier Error :-
hello.c:1:13: error:
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
before ‘.’ token
1 |
includestdio.h
|
^
Explain program loading expansion in memory including process context
and task structure in detail.
Answer :-
Its executable image
is loaded into RAM of computer in an organized manner .
1) Code Segment :-
It contains machine code of the compliled program using loader(It
loads file from secondary storage to memory for execution). The code
segment of an executable object file is often read-only segment that
prevents a program from being accidentally modified.
2)Data Segment:- It
store global variable ,static ,constant,volatile,register,macros and
external variable. It contains heap(dynamic memory
allocation),initialize,uninitialize.
3) Libraries segment
– It is shared , dynamic and static.
4) File Description
-
0 – stdin –
standard input
1- stdout –
standard output
2 – stderr –
standard error
5) Stack Segment –
It store local variable .
How to write and run any program in C using VIM editor.
1. Press CTRL + ALT + T to open terminal.
2. Write “vim hello.c” in terminal and press Enter.
3. Press “i” to enter in write mode.
4. Write the code given below:
#include<stdio.h>
int main(){
printf(“Hello! World”);
return 0;
}
5. Press ”esc” t
o enter in command mode and write “:wq” to save file and exit the VIM editor then press
enter.
6. Write “gcc hello.c” to compile the file.
7. Check the listing in directory by “ls” command.
a.out hello.c
8. Run this command “./a.out” to execute the program.
Build process in C program
There are mainly 4 steps in which the full build proccess occurs in the C program.
1. Pre-processor
2. Compiler
3. Assembly
4. Linking
1. Pre-processor
It include header file code in the source file.
a) It takes the .c file as input and scan the file for preprocess directive (Start with #).
b) It resolve all the macros in the program and remove all the comments given in the
program.
eg. Macros = #define MIN 10
Comments = /* This is my first program */
c) It generate “hello.i” file for the next step of build process.
2. Compiler
a) It takes the pre-processed file ie. “hello.i” as input.
b) it create a high level source code file “hello.s” as assembly code for the next step of build
process.
Compiler contains few process t
o compile the data and convert high level language in to low level language
Lexical Analsys – It breaks the code into tokens. |
Syntex Analsys – It parse the code. |
Semantic Analysis – It checks weather the variable declared or not. |
Intermediate code segment |
Code Optimization |
Target Code Generation |
3. Assembly (Machine code)
Assembly code is not understood by our machine so we need a medium to convert
these codes in to binary language.
a) It takes compiled file ie. “hello.s” as input.
b) It generates the file ie “hello.o” object code to run the program for target processor.
Note: In above process we actually deal with only HEADER files.
4. Linking
a) It links the all static library ( .lib , .h) and objective file to generate single executable file
ie. “a.out”.
eg. Printf, scanf and the assembled binary code or machine code.
NOTE: You can see the all files with command “gcc -Wall -save-temps hello.c”
After linking process if we execute the program it needs a medium so we can bring that
executable file in memory and that medium would be LOADER.
Question: - Roll of Process Context…?
Answer :- Process Context is a program that is a part of operating system. when the .c file give to preprocessor its converts .c file into .i file (contains a .c file and header file) and give to the compiler after that compiler compiles that file and create a .s file (source code file ) and give to the assembler. assembler create a .o file (object file) and give to the linker and linker create a executable file called ./a.out file (elf ) executable linkable format and its store in secondary storage. when executed the object fie loader program load's program from secondary storage to memory ram for execution. after that the process context program is run.
Its also has 4 stages.
1) Code segment
2) data segment
3) File descriptors
4) stack segments
Code Segment - Code Segment Contains Compiled Code.
Data Segment - Data Segment Contains Global Variables, Statics Variables, External Variable, Resistor variable, volatile, Macros , Constants all these things are stored in data segments.
File Segment - File Segment Contains 3 Standard's.
1) stdin (standard input)
2) stdout (Standard output)
3) stderror (Standard Error)
Stack Segment - Stack Segment Contains Load Variables.
It looks like you're new here. If you want to get involved, click one of these buttons!