Skip to main content

The main Function

   
     The main Function

The main is a part of every C program. C permits diferent forms of main statement. Following forms are allowed.

  • main()
  • int main
  • void main()
  • main(void)
  • void main(void)
  • int main(void)

The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main. The keyword void means that the function does not return any information to the operating system and int means that the function retúrns an integer value to the operating system. When int is specified, the last statement in the program must be "return O". For the sake of simplicaty, we use the first form in our program.

Example:

                 
       
       // Programm of Addition
      // Written by Santosh Asole
     
    #include<stdio.h>
     main ()
     {
    int x, y, z;
      x=10;
      y=20;
       z=x+y;
       printf("Addition is = %d",z);
      }

This program when executed produce following output:

         Addition is = 30

The first two line of the program are comment line.Write comments in the program is a symptoms of a good programmer. It is a good practice to use comment line in the beginning to give information such as name of the program, author name, date, etc.

The words number is variable names that are used to store numeric data. The numeric data may be either in integer form or in real form. In C, all variables should be declared to tell the compiler what the variable name are and what type of data they hold. The variables must be declared before they are used. The declarations
                   int number;
tell the compiler that number is an integer (int)  number.Declaration statements must appear the beginning of the functions as shown in above program .All declaration statements end with a semicolon i.e. ";". C supports many other data types and they are discuss in detail in another page.
The words such as int  are called the keywords and cannot be used as variable names. A list of keywords is given in another page.

Data is stored in a variable by assigning a data value to it. This is done in above program. Integer value 10 is assigned to the integer variable x and an integer value 20 is assigned to the integer value y. The result of addition of two integer numbers 10 and 20 is assigned to the integer variable z. 
The statements
      x=10;
      y=20;
      z=x+y;
are called the asaignment statements.Every assignment statements must end with a semicolon.
The next statement is an output statement that print the value of z. 
The print statement 
                  printf("%d", z);
contains two arguments. The first argument "%d" tells compiler that the value of the second argument z should be printed as decimal integer.
Note that these arguments are seperated by comma.

Comments

Popular posts from this blog

Syntax/Format of C program

C Programs A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file. Before we study the basic building blocks of the C programming language, let us look at a bare minimum C program structure so that we can take it as a reference in the upcoming chapters.     Syntax/Format of simple C program main ()  <--------------------- Function name  {  <------------------------------ Start of program         ........         ........  <------------------- Program statement         ........  }  <------------------------------ End of program      Hello World Example A C program basically consists of the following parts − • Preprocessor Commands • Functions • Variables • Sta...

Loop In C Language

Loop In C Language  A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages − C programming language provides the following types of loops to handle looping requirements. Sr.No. Loop Type & Description 1 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 2 for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. 3 do...while loop It is more like a while statement, except that it tests the condition at the end of the loop body. 4 nested loops You can use one or more loops inside any other while, for, or do..while loop. While Loop in C A while loop is the most straightforward looping structure. While loop syntax in C programming language is as follows: Syntax of While Loop in C: while (condition) { st...

Data Types in C

 Data Type in C Programming Type of data is know as Data type. int,float,char are example of data type in c There are two types 1. Primary data type 2. Derived data type. Primary data types Integer(int),floating(float),character(char),void are the primary data type Derived data types  arrays, functions, structures and pointers are Derived data types. Integer type Integers are used to store whole numbers. Floating type Floating types are used to store real numbers. Character type Character types are used to store characters value. void type void type means no value. This is usually used to specify the type of functions.