Monday, 21 March 2016

First C Program

Before starting the abcd of C language, you need to learn how to write, compile and run the first c program.
To write the first c program, open the C console and write the following code:
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main(){  
  4. printf("Hello C Language");  
  5.   
  6. getch();  
  7. }  
#include <stdio.h> includes the standard input output library functions. The printf() function is defined in stdio.h .
#include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.
void main() The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks the screen.
c program

How to compile and run the c program

There are 2 ways to compile and run the c program, by menu and by shortcut.

By menu

Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.

By shortcut

Or, press ctrl+f9 keys compile and run the program directly.
You will see the following output on user screen.
c program output
You can view the user screen any time by pressing the alt+f5 keys.
Now press Esc to return to the turbo c++ console.

clear screen by clrscr() function

If you run the c program many times, it will append the output in previous output. But, you can call clrscr() function to clear the screen. So it will be better for you to call clrscr() function after the main method as given below:
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main(){  
  4. clrscr();  
  5. printf("Hello C Language");  
  6.   
  7. getch();  
  8. }  

Flow of C Program

The C program follows many steps in execution. To understand the flow of C program well, let us see a simple program first.
File: simple.c
  1. #include <stdio.h>  
  2. void main(){  
  3. printf("Hello C Language");  
  4. }  
Let's try to understand the flow of above program by the figure given below.
C program flow
1) C program (source code) is sent to preprocessor first. The preprocessor is responsible to convert preprocessor directives into their respective values. The preprocessor generates an expanded source code.
2) Expanded source code is sent to compiler which compiles the code and converts it into assembly code.
3) The assembly code is sent to assembler which assembles the code and converts it into object code. Now a simple.obj file is generated.
4) The object code is sent to linker which links it to the library such as header files. Then it is converted into executable code. A simple.exe file is generated.
5) The executable code is sent to loader which loads it into memory and then it is executed. After execution, output is sent to console.



No comments:

Post a Comment