Showing posts with label Main function. Show all posts
Showing posts with label Main function. Show all posts

Wednesday, 15 August 2012

Lets Program

Let's Discuss a Simple program in C that display "a welcome message".

/*C program to display a welcome message*/

#include<stdio.h>
main()
        {
         printf("Hello sir! welcome to the programming world \n");
         exit(0);
        }
The output of the above program will be:  Hello sir! welcome to the programming world
How to compile and execute the program?
  • First of all you need a C compiler to compile the above bunch of codes.  There's a huge number of free c compilers for windows OS available on net e.g Turbo C/C++ , Dev C++ , Eclips etc. Google any of it and install on your system. I recommend Dev c++ IDE for beginners. In Linux, terminal is enough to start with.
  • Compile the code above with any of the standard compiler and run. Voila you must get the same output as stated above.
Let's discuss the components of the above program
  • main() is a special function in C, which must exist in a C program as the execution of the program from this point.
  • printf is a standard C function called from main, used for displaying ouput.
  • "\n" indicates that the output will be displayed in a newline.
  • 'stdio.h' is a header file responsible for input-output function.
  • exit() is also a standard function that terminates the program after execution. It takes a value as parameter. In the program above it takes 0 as parameter. But it is actually not needed here as it has been used after the last line. Hence without using this function, the program will terminate automatically after displaying the output.

Introduction to C

AAT & T's Bell Labs in the year 1972 Dennis Ritchie, head of the system software research Department developed one of the most efficient languages of all the times Called C. He incorporated some new features of his own along with BCPL ( Basic Combined Programming Language ) and Language B. Day by day C evolved as a compact, coherent and powerful 'one man language'.

Basic Structure of a C Program
  • C is a case - sensitive language. (Statements are generally written in lower case letters).
  • Any C statement ends with a semicolon (;).
  • Blank spaces can be inserted between two distinct words to enhance the readability of the statement. ( Spaces are forbidden while declaring a variable )
  • A program may contain one or more #include statements, which is known as 'Preprocessor Directives'. ( A Preprocessor process the program before compiler do ). 
  • main() function is an essential part of C language, where execution of the program starts. ( You must define the main () function in the program. ) 
  • main() function can be placed anywhere in the program but the execution starts from there only. The group of sentences within the main() function are executed sequentially. 
  • Comments can be inserted in a program to increase the clarity of the motive of the program. ( Comment statements must be enclosed with /*...*/ in a C program.
  • Certain headers files are necessary for compiling a C program. 'stdio.h' is an example of a header file, which controls the functions performing the tasks of input and output. 
  • A program can consists of one or more functions , which is actually a group of C statements that are executed together.
In spite of the above rules for a C program , there is no specification for the position of inserting different parts of a statement. That's why C is also referred as a 'Free form language'.