FIRST C PROGRAM

|

First of all you need a C compiler, I have been using Turbo C++ compiler and gnu compiler for this purpose. 

IN TURBO C++ COMPILER:

Why Turbo C++ compiler why not Turbo C compiler? Because Turbo C++ compiler supports both C & C++ program and if you want to learn C++, you don’t need to change your compiler. One more thing, Turbo C++ compiler provides mouse support which Turbo C compiler doesn’t.

Here is an example of your first C program which has been developed using Turbo C++ compiler.

Example - 1

Using geditor

Just type the above line in the C editor and press Alt + F9 to compile that program. If there will be any mistakes, it will show “Error” message otherwise it will show “Success” message. Now press Ctrl + F9 to run compiled program and you will see similar output. If you cannot see anything then press Alt + F5 to see output window.
Program Output
Turbo C Editor Window. 
                 Turbo C Editor Window. 
Output after executing program.
                 Output after executing program. 
Let’s understand each and every line of this program. The very first line #include tells the compiler to include a header file “stdio.h” in the program. Header file in C is nothing but collection of various functions. Here we are using printf() from stdio.h header file. The next line is void main() which is the beginning of the program. Every program must have a main() because program starts execution from this function. But what does the keyword void means before the main(). This means that main function will not return any value to the compiler. I know this is tough to understand now; these things will be discussed in detailed in function page.
The curly open and close braces { } defines the scope of the “main()” function means “main()” function’s boundaries. The line between curly braces printf("Hello, World!"); is the one of function defined in stdio.h whose job is to print message specified in double quotation. At the end of the line there is a semicolon (;) which is the end of that statement. C statements always end with a semicolon (;).
One thing you should keep in your mind that in C program, instructions declared in “main()”, are executed from top to bottom. To understand it better let’s extend the above program

                                   geditor window
The top two lines include two header files “stdio.h” and “conio.h” respectively. The “stdio.h” header file contains “printf()” and “conio.h” file contains “clrscr()” and “getch()”. The “clrscr()” clears the output screen means any output shown by previously run program will be erased and that why we always put them at the beginning. Next is three “printf()” statements which will print three different messages. The “getch()” waits for a keyboard input and if we press any key on keyboard the program exits. So we don’t need to press Alt + F5 to see the output as we did in above program.
The “getch()” will be discussed later in detail.

IN GNU COMPILER:

In gnu compiler there are different way to compile and execute the programs.First of all we write the source code using any text editor and save the file as file_name.c .


                                         geditor window




                              OUTPUT AFTER EXECUTING PROGRAM
OUTPUT:



1. Open up a terminal window. 
2. Type "gcc --version" into the terminal.
If the command is not found, it is likely that GCC isn't installed. 
3. Install GCC if needed, by referring to the GCC website installation procedures at http://gcc.gnu.org/install/ (This takes some familiarity with GNU/linux which is not covered in this article) 
4. Go to the directory where your .c program files are located. 
5. To compile a single source file, type
              gcc -o outputfile file1.c 
6. To compile multiple files, type
              gcc -o outputfile file1.c file2.c file3.c 
7. To compile with more error checking using the -Wall tag
              gcc -Wall -o outputfile file1.c 
8. To compile files without linking them type
              gcc -c file1.c file2.c file3.c 
9. After the last step, if you want to link them, type:
              gcc -o outputfile file1.o file2.o file3.o 
10. Fix any errors/warnings the compiler reports. 
11. Recompile the code if you had to fix errors. 
12. Repeat step 10-11 until the program builds successfully. 
13. Execute the program.
By using:
                  ./a.out   (by default file name is a.out)
                  ./file_name.out  





Note : gcc is for gnu compiler otherwise in unix based opearating system we can use cc instead of gcc
 
     eg.  cc file_name.c        

                    
OUTPUT:

0 comments:

Post a Comment