Search & Find out

Monday, November 26, 2012

Basic of C Program

Basics of C

  Format :
                  
     #include  "stdio.h"                           \* Header files*/
    #include  "conio.h "
   
     void main()                \* Main function, the execution of the program       
    {                       starts from here */
      
                     --------------------
        --------------------                 \*  statements or body of the programe */
        --------------------
                 
                 }

   
    The body of the programe include declaration of variables, statements &....many more..

      Declaration of  variable :
                              Declaring the variable means declaring the datatype for that variable,
       in which the variable to be stored.
       (A variable is an identifier that is used to represent some specified type of information with in a
         designated portion of the program.) 

    int a;     -->   Interger value, a is a variable which is used to store the integer value.

    char b;    -->   Character , b is a variable which is used to store the character.

    float c;     -->   Real constant value, c is a variable which is used to store the real constant
               value or float value.

     Here we can initialise the values to the variables
    i.e
        int a=10;   
        char b={z};
        float c=10.205;

      Statements of the program :
            Statements or body of the program include lot of functions, like
       printf(), scanf(), arrays[], user defined functions, pointers, structures, ... and many more..


     printf() :
      This function is used to printing the statements, values, etc..
             E.g :
             printf("Hello, world");

    O/p:  Hello, world

             E.g :
             printf("The value is %d",a);

    O/p:  10                   /* Here the value of the "a" is printed */


    \n     -->     uses to break the line
    \t      -->     uses to give a tab space i.e. space of 8 characters

     scanf() :
      This function is used to accepting the values , etc..
             E.g :
             scanf("%d",&a);

    O/p:  _                                  /* The program is waiting for accepting a integer value */

   
    %d    -->   It accepts only integer values.

    %c    -->   It accepts only a single character.

    %f            -->   It accepts only real constant values.

    We can accept one or more values at a samr time.

     E.g :
             scanf("%d%c%d",&a,&b,&c);

    O/p:  _       (enter)        /* The program is waiting for accepting a integer value */
             _    (enter)        /* The program is waiting for accepting a character */
             _    (enter)           /* The program is waiting for accepting a real constant value */
   
  
      CONDITIONAL STATEMENT:

        This statements are depend upon the conditions
      There are two types:
http://www.brothersoft.com/d.php?soft_id=182798&url=http%3A%2F%2Flfiles3.brothersoft.com%2Fdevelopment%2Fc_c%2Ftc3.zip
       1) if else:
       
    syntax:        
        if(condition)
        {
            ----------------
            ----------------
        }
        else
        {
            ----------------
            ----------------
        }
   
    Here if condition is true then the ' if ' part is execute. if the condition is false then the ' else '
      part is execute.

         2)  switch :
   
    syntax :
        switch(expression \ variable)
        {
              case 1:           
            ----------------
            break;

              case 2:
            ----------------
            break;   

              default :
            ----------------
            break;
        }

        Here if the expresion or condition is declared.
        If condition is true the the case 1 : be execute,
        If condition is false the the case 2 : be execute.

        Here if the variable is declared, then the matching case would be executed.


           LOOP STATEMENTS :
            Loop means continue.... It means the execution of the loop
    statements are stop if and only if the condition in false.
           
    C has provided three types of iteration statements:

    1) for loop.
    2) while loop.
    3) do...while loop.

    1) for loop :
        For loop is a loop statement, In this first the condition is checked, and then the execution will be start.
        If the condition is false then the loop does not execute
        In which the statements are executes till the condition is false.
        In this loop, we can initialise & increment the variables.

        syntax :
            for(initialize; condition ; increment )
            {
                 ----------------------
            }
    E.g :  
       
    #include
    #include
   
    void main()
    {
            int i;

        for(i=0;i<100;i++)
        {
              printf("The value of i is %d",i);   
        }

        getch();
    }

     
         2 ) while loop :
        It is same as for loop, but the formate is different, means
     For loop is a loop statement, In this first the condition is checked, and then the execution will be start.
     If the condition is false then the loop does not execute
     In which the statements are executes till the condition is false.
   
        syntax :
            while (condition)
            {
                  -----------------
                  -----------------
            }

        E.g :
       
                #include
                #include
   
              void main()
             {
                 int i;

        while(i<100 )
        {                                  
                    printf("The value of i is %d",i);   
          }

        getch();
        }

        ...
   
          3) do .. while loop :
        In this loop, first the statements are executed before checking the condition.
    Means if the condition is false then only it will execute atleast one time.

    syntax :
        do
        {   
            ------------
        }
        while(condition);

    E.g :        
   
        do
    {
        printf("The value of i is %d",i);
    }
    while (i<100);
    ...
    ...


 Arrays:
                    An array is data type that uses subscripted variables to make the representation of a large number of homogeneous values possible.

    E.g: 
              int a[3];
              float c[5];

     Types of Arrays:

       1)  Single Dimensional Array:
               It uses only single dimensional ..
    E.g:   a[3]
             b[100]
             c[10]
       
       2) Multi Dimensional Array :
              It uses two or more dimensional i.e it uses two or morw seperate square
          brackets.

    E.g :  
        a[10][10]              -->    It is two dimensional Array.
        b[10][5][14]       -->    It is three dimensional Array.

       3)  Character Array or String :
               A group of characters can be stored in a character array.
           Character array are known as String.
          
    E.g:
                        char a[3]
           char b[100]
           char c[10]   
   
    In this String '\0' is used to indicate the last position of a word or sentence.

    i.e               a[0]  a[1]  a[2]  a[3]  a[4]
            T      E       J       A     \0

      POINTERS:
                Pointers is a variable which contains the address of the another variable.
         Basically it is used to store the memory location.
         Pointer itself might be another pionter.

    E.g:
              int *p;    /*p is declared as a pointer */


    E.g:
               int *p;   
               int a=10;
               p=&a;    /* It is a address operator, here p is stored the addres of the variable a */

                            int **k;            /* k is a pinters pointer , it points address of the pointer which points the address of a variable*/
               int *p;       
              int a=10;
              p=&a;
              k=&p;

           i.e
          a     p      k     /* the variable name */
                                  10           35084          48451        /* the value of the variable contains */
                  35084        48451          52101        /* the location of the variable */
             lets see how it works

    printf("%d",a);    -->    10
    printf("%d",*p);    -->    10
    printf("%d",p);    -->    35084
    printf("%d",*k);    -->    35084
    printf("%d",**k);    -->    10
    printf("%d",k);    -->    48451


          STRUCTURE :
        A structure is a user defined datatype. Structure is a data structure,
            whose individual elements can differ in type.
            It is a combination of several different previously data types, including othere structures.

    E.g:
           #include
           #include

          struct demo    /* "demo" is a name of the structure, we can take any name */
          {
              int a;    /* Here we can only declare & that would be only one variables in one line statement */
              int b;
             float c;
          };   
       
      void main()
     {
          struct demo s,s1;          /* s & s1 are variables, we uses this variable with the variable which are
                      declared in the struct demo */
        s.a=1;
        s.b=15;
        s.c=12.25;
   
        printf("%d\n%d\n%f",s.a,s.b,s.c);
    o/p:  1
            15
            12.25           

  

0 comments:

Post a Comment

 

My Blog List

Term of Use