The greatest mistake you can make in life is to be continually fearing you will make one

Sunday 19 December 2010

Multifile Program in C

1.What is a Multifile program?
  • If a program contains many functions then it is difficult to read and maintain because when the number of functions increases the size of the program/file will also increase. To avoid this some functions can be written in separate file 
  • The individual files will be compiled separetely and then linked together to form one executable object program.
  • This will help to editing and debugging because each file can be maintained at a manageable size.

2.How functions are defined in Multifile Program?
  • Within a multi file program a function definition may be either external (or) static
  • An external function will be recognized throughout the entire program(ie all the files)
  • Static function will be recognized only with in the file in which it is defined.
  • The default storage class for the function is external
  • Syntax for Function definition
    Storage class datatype name(type1 arg1......typen,argn)
    {
    }
  • Example:
    extern int calculate(int x)
    {
    }

3.How functions are declared in Multifile Program?
  • When a function is defined in one file and accessed in another one file, the 2nd file must include a function declaration.
  • This declaration identifies the function as an external function whose definition appears else where. 
  • The function declarations are usually placed at the beginning of the file before any function definition.
  • Syntax for external function declaration is same as function definition with additional one semicolon at the end
    Storage class datatype name(type1 arg1...typen,argn);
  • Example:
    extern int calculate(int x);
  • To execute a multifile program, each individual file must be compiled separetely then the main program file is compiled and executed.

4.A simple Multifile program in c

/****************File1*********/
#include<stdio.h>
//Include file test.c
#include "test.c"
//function declaration
extern void result(void);

int main()
{

    printf("In file1 main function\n");
    result();
    return;
}

/*****************File2*********/
extern void result(void)
{
    printf("Welcome to file2\n");
    return;
}
output:
./a.out
In file1 main function
Welcome to file2

5.Accessing external variables in Multifile function:
  • Within a multifile program, external variables defined in one file and accessed in another file.
  • An external variable definition can appear in only one file. 
  • Its location within the file must be external to any function definition. Usually it will appear at the beginning of the file.
  • External variable may include initial values.
  • If no initial value is assigned then it will automatically initialized to zero.
  • To access an external variable in another file,the variable must first be declared within that file .This declaration may appear anywhere within the file usually at the beginning of the file.
  • The declaration must begin with the storage class specifier extern. Initial values cannot be included in external variable declaration
  • Example:
     /*********file1**********/
    #include<stdio.h>
    //Include file test.c
    #include "test.c"
    //External variable definition
    int x=1,y=2,z=3;
    //function declaration
    extern void result(void);
    int main()
    {
        printf("In file1 main function\n");
        x=y+z;
        printf("x=%d y=%d z=%d\n",x,y,z);
        result();
        return;
    }
    /*****************File2*********/
    extern int x,y,z;/*external var declaration*/
    extern void result(void)
    {
        printf("Welcome to file2\n");
        printf("x=%d y=%d z=%d\n",x,y,z);
        return;
    }

    Output:./a.out
    In file1 main function
    x=5 y=2 z=3
    Welcome to file2
    x=5 y=2 z=3
  • The value assigned to an external variable can be altered within any file in which the variable is recognized
  • The changes will be recognized in all other files. Thus external variables provide a convenient means of transferring information between files.
  • Example:
     /*********file1**********/
    #include<stdio.h>
    //Include file test.c
    #include "test.c"
    //External variable 
    definition
    int x=10,y=20,z=30;
    //function declaration
    extern void result(void);
    int main()
    {
        printf("In file1 main 
    function\n");
        x=y+z;
        printf("x=%d y=%d z=%d\n",x,y,z);
        result();
        return;
    }
    /*****************File2*********/
    extern int x,y,z;/*external 
    var declaration*/
    extern void result(void)
    {
        printf("Welcome to file2\n");
    
        y=y+10;
        z=z+10;
        printf("x=%d y=%d z=%d\n",x,y,z);
        return;
    }

    Output:./a.out
    In file1 main function
    x=50 y=20 z=30
    Welcome to file2
    x=50 y=30 z=40

Sunday 12 December 2010

Static Variables in C

  • Static variable use the keyword static.
  • In a single file program static variable is similar to auto variables.
  • Like auto variables, static variables are also local to the block in which they are declared.It cannot be accessed outside of their defining function.
  • The difference between auto and static variable is that static variables dont disappear when the function is no longer active. their values persist.
  • If the control comes back to the same function again the static variables have the same values they had last time.
  • Example
    #include<stdio.h>
    void fun1();
    
    int main()
    {
        fun1();
        fun1();
        fun1();
        return;
    }
    
    void fun1()
    {
        int x=1;
        printf("X=%d\n",x);
        x=x+1;    
    }
    The output of the above program is ./a.out
    X=1
    X=1
    X=1
    If the variable is static then
    #include<stdio.h>
    void fun1();
    
    int main()
    {
        fun1();
        fun1();
        fun1();
        return;
    }
    
    void fun1()
    {
        static int x=1;
        printf("X=%d\n",x);
        x=x+1;    
    }
    The output of the above program is ./a.out
    X=1
    X=2
    X=3
  • If the automatic or static variables having the same names as external variables then the local variables will take precedence over the external variable and the changes done to the local variable will not affect the external variable.
    #include<stdio.h>void fun1();
    int x=1,y=2,z=3;
    int main()
    {
        static float x=5.0;
    
        printf("float x=%f\n",x);
        x=x+1;
        printf("float x=%f\n",x);
        fun1();
        printf("x=%d\n",x); //try to print external var
        printf("x=%f y=%d z=%d\n",x,y,z);    
        return;
    }
    void fun1()
    {
        char z;
        z='A';
        printf("int z=%d char z=%c\n",z,z);
        x=x+10;
        printf("x=%d\n",x);    
    }
    Output
    float x=5.000000
    float x=6.000000
    int z=65 char z=A
    x=11
    x=-1  //This will show an warning
    x=6.000000 y=2 z=3
    Here x,y,z are external integer variable. But x is redefined as a static floating point variable within main. so in main the external variable x will not be recognized. In function fun1() ,z is redefined as a char variable so the external variable z will not be recognized in fun1().
    Example 2
    #include< stdio.h>
    int x=1;
    int main()
    {
        static int x=5;
        printf("x=%d\n",x);
        x=x+1;
        printf("x=%d\n",x);
        return;
    }
    Output:
    x=5
    x=6
Initializing Static Variable:
  • Initial values can be included in the static variable declaration. 
  • Example: static int x=10;
  • The initial values must be constants not expressions.
    Example
    #include<stdio.h>
    int main()
    {
        static int x=5,y=10;
        static int a=x;
        static int z=x+y;
        printf("a=%d\n",a);
        printf("z=%d\n",z);
       return;
    }

    While compiling it shows error as " initializer is not a constant"

  • The initial values are assigned at the beginning of program execution. The variables retain these values throughout the life of the program unless different values are assigned.
  • The default initial value is zero.

Thursday 9 December 2010

Some Questions about Arrays

  1. How will you define an array?
  2. Is it necessary to specify the storage class in array definition? what is the default storage class for arrays?
  3. If the array elements are not initialized then what is the default initial value?for example: int x[5]; what is the value of x[1]?
  4. Consider we have a 10 element array what happen if we try to access the 12th element?
  5. Can I declare the array without specifying the size? int a[]; is it allowed?
  6. int a[]={1,2,3,4,5}; Is it allowed?
  7. Consider int a[5]={1,2,3,4,5}; printf("a=%d",*a); what is the output?
  8. Consider int a[5]={10,20,30,40,50}; printf("a=%d",a); what is the output?
  9. The last character in a array must be a NULL(\0) character what happen if I missed the '\0' character? char vowels[6]={'a','e','i','o','u'}; Is it correct?
  10. What happen If I am not giving space for '\0' character? char flag[4]={'T','R','U','E'}; is it correct?
  11. What happen If I am not giving space for '\0' character in string? char str[5]="Welco"; is it correct?
  12. x is a array pointer. what is the value for *x++; 
  13. Is there any way to find the size of the array?
  14. Can I print the characters given in single quotes as string?
  15. What is the output for the following program?
    #include<stdio.h>
    int main()
    
    {
    
        char x[]={'a','e','i','o','u'};
    
        printf("ans=%s",x);
        return;
    }
  16. What is the output for the following program?
    #include <stdio.h>
    int main()
    {     int x[5]={10,20,30,40,50};
        printf("ans=%d",(*x)++);
        return;
    }
  17. What is the output for the following program?
    #include <stdio.h>
    int main()
    {
        int x[5]={10,20,30,40,50};
        printf("ans=%d",++*x);
        return;
    }
  18. Do we have any minimum or maximum size restrictions in array index?
  19. What is the maximum index(sixe) number an array can have?
  20. Why array index is starting from zero? (or) Why arrays not store values from 1?
  21. Can I declare an array with 0 size? int a[0]; is it allowed?
  22. Can I declare an array with negative size? int a[-5]; is it allowed?
  23. Can I compare 2 arrays with single operator?
  24. Can I have comma(,) alone in the array list? int x[5]={,,,,}; Is it allowed?
  25. What happen if the array size is less than the number of initialized values?int a[5]={1,2,3,4,5,6,7,8,9,}; is it allowed?
  26. How to pass and access the array values to the function using call by value method?
  27. How to pass and access the array values to the function using call by reference method?
  28. Array name is a pointer. If an array is passed to a function and several of its elements are altered with in the function, are these changes recognized in the calling portion of the program?
  29. Can an array can be passed from a function to the calling portion of the program via return statement?
  30. Is there is any difference in storing 1 dimensional array and multi dimensional array?
  31. How the elements of a 2 dimensional array is stored? row major form (or) column major form?
  32. what is the size of a  2 dimensional array, 3 dimensional array?
  33. In 2 dimensional array number of rows specified first or number of columns specified first?
  34. Is it necessary to specify both  number of rows and number of columns in 2 dimensional array?
  35. Can I leave the number of columns in 2 dimensional array? int a[5][]; is it allowed?
  36. Can I initialize a[][]={1,2,3,4,5,6,7,8,9}; (or) a[5][]={1,2,3,4,5,6,7,8,9}; 
For Answers, Click here

      Thursday 2 December 2010

      Storage Classes in C(Extern)

      External Variables (or) Global Variables

      • The scope of a external variables remains from the point of the definition through the remainder of the program.
      • External variables are recognized globally, that can be accessed from any function 
      • Using external variables we can transfer information in to a function without using arguments.
      • The key word for external variable is extern.
      External Variable definition:
      • External variable definition is written in the same manner as an ordinary variable declaration.
      • It must appear outside of ,and usually before the functions that access the external variables
      • Example:
        int x;     //External variable definitions
        fun1(){}
        fun2(){}
      • An external variable definition will automatically allocate the required storage space for the external variables with in the computer memory.
      • The assignment of initial values can be included.
        Example:
        int x=10;     //initializing external variable in definition
        fun1(){}
        fun2(){}
      • The keyword extern is not required in external variable definition.
      • If a function requires an external variable, that has been defined earlier in the program, then the function may access the external variable freely.
        Example:
        #include<stdio.h>
        int x=10;
        fun1
        {
        }
        fun2
        {
            int a,b=10;
            a=b+x; //no need to declare x here
        }
      External variable Declaration:
      • If the program is a multi file program  then the external variable must be declared where ever its needed.
      • An external variable declaration begins with the keyword extern.
      • The name and the datatype of the external variable must agree with the corresponding external variable definition that appear outside of the function.
      • Example:
        file1
        #include<stdio.h>
        int x=10; //external var   defined here
        fun1()
        {
        }
        fun2()
        {
        }
        file2
        #include<stdio.h>


        fun3()
        {
            extern int x; //declaration               
        }
        In file 2,x should be of type integer because in file 1, x is defined as integer.
      • The storage space for external variables will not be allocated because the value is already defined.
      • External variable declaration cannot include the assignment of initial value. 

      Initialization of External variable:
      • External variables can be assigned initial value during variable definition.
      • The initial values will be assigned only once, at the beginning of the program.
      • The initial values must be expressed as constants not expressions or statements
        Example:
        #include<stdio.h>
        #define z 10
        /*definition of external variable x and Y
        int x=z;
        int y=z+x;       //error
        fun1()
        {
        }
        fun2()
        {
            int a,b=10;
            a=b+x;
            printf("x=%d y=%d\n",x,y);
        }
        int main()
        {
        fun1();
        fun2();
        return;
        }
        while compiling this program It shows an error   "error: initializer element is not constant"
                  
      • The external variables will then retain these values, unless they are altered during the execution of the program.
      • If the value of the external variable is changed with in a function, then the changed value will be carried over in to other parts of the program.
        Example
        #include<stdio.h>
        #define z 10
        int x=z;

        fun1()
        {
           x=30;
           printf("In fun1, x=%d \n",x);
        }
        fun2()
        {
            x=20;
            printf("In fun2, x=%d \n",x);
        }
        int main()
        {
            printf("In main, x=%d \n",x);
            fun1();
            fun2();
            return;
        }
        The output of the above program is
        In main, x=10
        In fun1, x=30
        In fun2, x=20
      • If the initial value is not assigned , the variable will automatically be assigned a value of zero.
        Example
        #include<stdio.h>

        int x; //Not initialized

        fun1()
        {
           x=30;
           printf("In fun1, x=%d \n",x);
        }
        fun2()
        {
            x=20;
            printf("In fun2, x=%d \n",x);
        }
        int main()
        {
            printf("In main, x=%d \n",x);
            fun1();
            fun2();
            return;
        }

        The output of the above program is
        In main, x=0
        In fun1, x=30
        In fun2, x=20