Understanding how C program works


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Understanding how C program works

  1. 1. www.mindbridgetech.comdhanashri@mindbridgtech.com pushpa@mindbridgetech.com
  2. 2.  Compilation stages of C program How C program works Object file format How to run hello world program in Linux environment
  3. 3.  C created -1972 by Dennis Ritchie Efficient Portable Flexible
  4. 4.  Three things are necessary for creating programs • Text editor (vi editor) • Compiler (gcc) • Standard libraries and headers (glibc)
  5. 5.  Assembly Machine Language • MOV R1, 0x1000 ------ 10101010 • MOV R2, 0x1004 ------ 11001100 • ADD R1, R2 ------ 11100011 • STA 0x1008 ------ 10001000 HLL (C program) • C = a+b
  6. 6.  Each and every statement of C program need to convert in such a format so that processor can understand it. Ex: Processor does not understand what is “printf” A program that translate HLL language to machine level language is known as compiler
  7. 7.  The four stages for a C program to become an executable • Pre-processing • Compilation • Assembly • Linking
  8. 8.  Begins with „#‟ Carried out prior to compilation Macro substitution Comments are removed Expansion of the included files Saved in .i file/* hello.c */#include<stdio.h> /*Preprocessor directives*/int main(){ printf(“Hello Worldn”); return 0;}
  9. 9.  “hello.i” is the input to Compiler Compiled into intermediate stage (assembly) i.e. „hello.s”/* hello.c */#include<stdio.h> /*Preprocessor directives*/int main(){ printf(“Hello Worldn”); return 0;}
  10. 10.  “hello.s” is the input to Assembler Compiled into object file “hello.o”/* hello.c */#include<stdio.h> /*Preprocessor directives*/int main(){ printf(“Hello Worldn”); return 0;}
  11. 11.  Final stage “hello.o” is the input to linker Form executable a.out/* hello.c */#include<stdio.h> /*Preprocessor directives*/int main(){ printf(“Hello Worldn”); return 0;}
  12. 12.  Input Program Output src code >Preprocessor >expanded src code expanded src code >Compiler >assembly src code assembly code >Assembler >object code object code >Linker >executable code executable code >Loader >execution
  13. 13.  Part of Operating system responsible for loading the program Allocates program address space Copies a program into main memory
  14. 14.  Stands for Executable and Linkable Format Object file Executables Shared libraries
  15. 15.  Each object file contains • code section • data section • BSS (Block started by Symbol) section • symbol table Symbol table contains information of • global variable, • external functions like printf, scanf • Functions defined in other object file
  16. 16.  not specific to particular processor or architecture adopted to many operating systems on many different platforms
  17. 17.  Combines code and data sections Resolves symbols for global variable Resolves symbols for external function and library function such as printf, scanf
  18. 18.  What is Library?It is a collection of pre-compiled functions andreusable code. In Linux, it is called glibc (GNU C library)
  19. 19. LibraryStatic Library Shared Library Dynamic Dynamic Loading Linking
  20. 20.  Link at the compilation stage into the final executable Size of the executable becomes large compared to shared library Need more RAM Fast Suitable for small program
  21. 21.  It just verifies the link Actual symbol is added at the time of running Size of executable become less as compared to Static Becomes slower than static library Used for bigger application
  22. 22.  C language is • Efficient • Powerful • Flexible Its being 40 years it is used extensively and will be used for many more years to come. Encourage yourself to be a good programmer to sustain in software industry