Skip to main content

Posts

Showing posts from September, 2013

Steps involved in compiling a c program using gcc

Steps involved in converting the C source code to executable: Now, we are going to see how to convert a C source code to executable and also about the steps involved in compiling a C program.  C program compilation involves 4 steps which are as follows. Converting C source code to Preprocessed code Converting preprocessed code to assembly code Converting assembly code to object code Converting object code to binary Steps involved in compiling a C Program Here, we have two c files main.c and add.c. Below is the source code for main.c #define NUM1 10 #define NUM2 20 int main() { int a, b; a = NUM1; b = NUM2; add(a, b); return 0; } Below is the source code for add.c #include <stdio.h> #define STRING "Result:" void add(int a, int b) { int c; c = a + b; printf("%s %d\n", STRING, c); return; } As I mentioned earlier, compiling a C program involves 4 phases.  They are preprossesin...

How to convert c source code to assembly language code?

Covert c program to assembly language program Compiler uses -S option to generate assembly language code for the given c source code. Below is the c source code(program.c).   #include <stdio.h>   int main() {         int a, b, c;         a = 20, b = 30;         c = a + b;         printf("Sum of two numbers is %d\n", c);         return 0;   } C source code can be converted to assembly code using the following command.   jp@jp-VirtualBox:~/$ gcc -S program.c    jp@jp-VirtualBox:~/$ ls   program.c  program.s Below is the assembly code for the above given c source code.         .file   "program.c"         .section        .rodata   .LC0:         .string "Sum of two numbers is %d\n"         .text   .globl main       ...

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

_graphgetmem & _graphfreemem examples in c

Header file:     graphics.h Synopsis:        void far * far _graphgetmem(unsigned size);      void far _graphfreemem(void far *ptr, unsigned size);       Description:       User hooks which can be used for memory allocation and deallocation. _graphgetmem & graphfreemem functions in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   #include <alloc.h>   /* called by the graphics kernel to allocate memory */   void far * far _graphgetmem(unsigned sz) {         printf("Inside _graphgetmem to allocate %d bytes\n", sz);         delay(500);         /* allocate memory from far heap */         return farmalloc(size);   }   /* called by the graphics kernel to free memory */   void far _graphfreemem(void far *ptr...

setwritemode example in c

Header file:     graphics.h Synopsis:        void setwritemode(int mode);       Description:       setwritemode() sets the current writing mode for line drawing.  Mode can be COPY_PUT or XOR_PUT. COPY_PUT - overwrites with the line on screen XOR_PUT  - uses XOR command to mix line with the screen setwritemode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main() {         /* request auto detection */         int graphicDriver = DETECT, graphicMode, err;         /* initialize graphics and local variables */         initgraph(&graphicDriver, &graphicMode,                                         "C:/TURBOC3/BGI"); ...

setviewport example in c

Header file:     graphics.h Synopsis:        void setviewport(int left, int top, int right, int bottom, int clip);      (left, top) - top left corner      (right, bottom) - bottom right corner      clip - If the clip value is non-zero, all drawings will be truncated to current view port       Description:       setviewport() sets a new view port for graphics output. setviewport function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int graphicDriver = DETECT, graphicMode;         int err, midx, midy;         /* initialize graphics and local variables */         initgraph(&graphicDriver, &graphicMode, "C:/TURBOC3...

installuserfont example in c

Header file:     graphics.h Synopsis:        int installuserfont(char *name);       Description:       installuserfont() installs a user defined font file and returns the font ID which can be used as the font style parameter for settextstyle(). installuserfont function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int graphicDriver = DETECT, graphicMode;         int font, err, midx, midy;         /* initialize graphics and local variables */         initgraph(&graphicDriver, &graphicMode,                                 "C:/TURBOC3/BGI");         /* read res...

setusercharsize example in c

Header file:     graphics.h Synopsis:        void setusercharsize(int multx, int divx, int multy, int divy);      multx : divx - width magnification factor      multy  : divy  - height magnification factor Description:       setusercharsize() sets user defined character magnification factor for stroked fonts.  To make the text twice as wide as default text, set 2 to multx and 1 to divx.      multx : divx <=> 2 : 1 To make the text half the height of default, set 1 to multy and 2 to divy.      multy : divy  <=> 1 : 2 setusercharsize function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request autodetection */         int graphicDriver = DETECT, graphicMode, err;     ...

setgraphbufsize example in c

Header file:     graphics.h Synopsis:        unsigned setgraphbufsize(unsigned bufsize);       Description:       setgraphbufsize() returns size of the previous buffer and set the given size for the internal graphic buffer. setgraphbufsize function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   #define BUFSZ 1024 /* internal graphics buffer size */   int main(void) {         /* request auto detection */         int graphicDriver = DETECT, graphicMode;         int err, midx, midy, prevsz;         char str[64];         /* setting the size of the interal graphic buffer */         prevsz = setgraphbufsize(BUFSZ);         /* initialize graphics and local variables */     ...

setgraphmode example in c

Header file:     graphics.h Synopsis:        void setgraphmode(int mode);       Description:       setgraphmode() sets the system to graphic mode. setgraphmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

getgraphmode example in c

Header file:     graphics.h Synopsis:        int getgraphmode (void);       Description:       getgraphmode() returns the current graphic mode number. getgraphmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy, mode;         char str[64];         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         /* an error occurred */         if (err != grOk) {         ...