the portability of the compiler.
ready access to the hardware when needed
the standard library concept
Low Level Language Support
#include<stdio.h>
int main()
{
char ch;
int c;
c=open("filename",O_RDONLY);
ch = read(c,&ch,1);
while(ch!=EOF)
{
printf("%c\n", ch);
ch=read(c,&ch,1);
}
return 0;
} i am not able to get that why on providing input program is not terminating it is kepp on asking more inputs (went into infinite loop).
Normally the size of an array can be calculated as :
int i=sizeof(arr)/sizeof(arr[i]); //where i can be any index of the array.
There is another way by which we can calculate the size of an array , lets take an example :
int arr[5]={0}; //arr has 5 elements, all initialized to 0.
now try to print arr and &arr, you will see both will contain same value that is pointing to same array. But when you try to print arr and &arr+1, the &arr+1 will print the memory location after the array arr.
so for example. if arr print 1008 then &arr+1 will print 1028( as size of arr is 5*int ,means 20).
So by this another method we can calculate the size as:
int i=*(&arr+1)-arr;
COMPILATION PROCESS
In
this block diagram the process of compilation is explained. The first
process or step that takes place when we give command for compilation
i.e. gcc hello.c where gcc
is command to compile the following filename.c which is hello.c here,
#include<stdio.h> is
processed in which # is preprocessor directive then stdio.h header
file is included from path /usr/include
after
preprocessor file hello.i is created which combines of stdio.h and
hello.c. stdio.h contains declarations such as printf, scanf etc.
Then
this hello.i is forwarded to gcc compiler. GCC compiler starts its
compilation from main()
It
is also known as two pass compilation.
Steps
included in compilation-:
1.
Syntax analysis
2.
Lexical analysis
3.
Intermediate code generation
4.
code optimization
5.
object code generation
In first pass of compilation three steps are processed syntax analysis lexical analysis intermediate code generation.
In second pass of compilation all five steps are processed and in 5th step object code is generated
which is passed to source code hello.s. It comprises of header code and link. From this symbol table is filled in which keywords are passed to table.
Now, file is converted into assembly language in assemble which then produce hello.o file. It is then passed to linker which produces a.out file which is in executable and linkable format. There are three segments here ELF Header ,code segment & data segment.
Now for user interaction, file is loaded to process context in memory. It also comprises of four segments:-
1. Stack Segment : In this segment local variables are stored
2. File desription : In this there are three connections which are connected through connectors to input and output
- 0 it is connected to stdin
- 1 it is connected to stdout
- 2 it is connected to stderr
3. Code segment : It contains compiled code
4. Data segment : This segment contains - Global variable
Static variable
External variables
Volatile variables
Register variables
Constants
Qualifiers
Macros
Preprocessor directories
Build process in
C program
There are mainly 4
steps in which the full build process occurs in the C program.
1. Pr-processor
2. Compiler
3. Assembly
4. Linking
1. Pr-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 preprocessed 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 to compile the data and convert high level
language in to low level language
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 , .a) 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.
It has few stages to execute the program.
1. Code segment
2. Data segment
3. Heap segment
4. Stack segment
a) It holds the compiled program instruction that will be
executed as the program runs.
b) It is the read only section that can not change while
execution.
c) It present in the bottom of the memory so other segments
cant overwrite in code segments.
Data segment is divided in to 2 segments:
a) Initialized data
b) UN-initialized data
a) Initialized data: It contains only global variable that are explicitly initialized.
The value of variable can be changed later.
It is possible it can contain some read only data. Eg
“const”
#include<stdio.h>
const char str = “Hello world”
int a;
int main(){
int b = 3;
int x;
}
The value “str” is already defined in the program so
initialized data contains such kind of variables.
b) UN-initialized data: It
contains the global variable
which the value of those variable are not defined in the program.
#include<stdio.h>
const char str = “Hello world”
int a;
int main(){
int b = 3;
int x;
}
The value of global variable “a” is not defined in the program
so UN-initialzed data contains such kind of variables.
3. Stack segment
It contains only local variables present in the program.
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
It looks like you're new here. If you want to get involved, click one of these buttons!