Day 2 Write first C program
How to write a first C program
- Step 1. In C program each instruction written as a separate statement.
- Step 2. The statement of a program must be in the sequence by which we want to execute.
- Step 3. Blank space are not allowed in the variable name but you can use blank spaces between two words which increase readability.
- Step 4. Statements should be in small case letters.
- Step 5. Every statement ends with a semicolon (;).
- Step 6. Semicolon is called statement terminator.
- Step 7. You can put the comment within a program by using /* …….*/.
- Step 8. Comments are not necessary to use in a program but it is a good habit to use comments in a program.
Compilation and Execution of a C Program:
- Write a program using editor.
- Save the file with .c extension.
- To compile the program you can select compile from compile menu and press enter or you can press Alt+f9
- It will check the errors if program has any error it will terminate the program then you can find out he error and you can correct it. After correcting the error you can compile the program again. After compiling successfully it will create a object (.obj) file.
- Once the source program has been converted into an object file, it is still not in the form that you can run. The reason behind this is that there may be some references to standard library functions or user-defined functions in other object files, which are compiled separately. Therefore these object files are to be linked together along with standard library functions that are contained in library files. To link our object file, select Link from the Compile menu. After this the .OBJ file will be compiled with the one or more library files. The result of this linking process produces an executable file with .EXE extension. Figure-4 depicts this linking process.
- .exe file is a stand alone executable file. which can direct execute from command prompt
- After pressing Alt+f9 it will complete the whole process creating .OBJ and .EXE.
- To display the output of the program press Alt+f5.
Examples of a simple C program
/* Calculation of simple interest */
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}