Get it on Google Play
Hey check out this LuckyKhel App at:
Get it on Google Play Get it on Google Play
LuckyKhel

Saturday, 2 June 2012

How to use a variable in multiple/different source files?

How-to:
To define a global variable or function; you need to do the following:
1. create a header file with the declaration of variable or function.
Ex:
Common.h
#ifndef _COMMON_H_
#define _COMMON_H_
extern int i;
extern int n;
extern unsigned char Array[];
extern void func1();
extern int func2();
#endif
*** Note: The keyword extern is optional in function declarations
2. create a source file with the definition of variable or function.
Common.c
#include "common.h"
int i;      // Define i and initialize
int n;      // Define n and initialize
unsigned char Array[6] = {1,2,3,4,5,6};   // Define Array and initialize
// Define func1 and initialize
void func1(void)
{
    printf("This is ... function 1 \n");
}
// Define func2 and initialize
int func2(void)
{
    printf("This is ... function 2 \n");
    return 0;
}
*** Note: The declaration source file should include (#include) the definition header file.
3. Main program
#include <stdio.h>              // Include general I/O library
#include "common.h"             // Include defined variables and functions
void func3(void);               // Define Local function
void func4(void);               // Define Local function
int main(void)                  // Main Program
{
    printf("i = %d \n", i);     // Print out initial value of i
    printf("n = %d \n", n);     // Print out initial value of n
    i = 8;                      // Set a new value for i
    printf("i = %d \n", i);     // Print out the new value of i
    n = 4;                      // Set a new value for n
    printf("n = %d \n", n);     // Print out the new value of n
    func1();                    // Call global function = function 1 
    func2();                    // Call global function = function 2
    for(i=0; i<6; i++)          // Print out global Array
        printf("%d", Array[i]);
    // Print out blank lines
    printf("\n");
    printf("\n");
    func3();                   // Call local function = function 3
    // Print out blank lines
    printf("\n");
    printf("\n");
    func4();                   // Call local function = function 4
    return 0;                  // End main program
 }
// Definition of local function 3
void func3(void)
{
     printf("This is ... Function 3 \n");
     printf("Please, Enter a new value for variable i = ");
     scanf("%d", &i);
     printf("The new value for variable i = %d \n", i);
}
// Definition of local function 4
void func4(void)
{
     printf("This is ... Function 4 \n");
     for(i=0; i<6; i++)
     {
          printf("Please, Enter a new value for variable Array[%d] = ", i);
          scanf("%d", &Array[i]);
     }
     for(i=0; i<6; i++)
        printf("a new value for variable Array[%d] = %d \n", i, Array[i]);
}
*** Note: Need to include (#include) the definition header file wherever the global variables and functions  are needed in the program code.

 See More http://teliumapplication.blogspot.in/

1 comment:

  1. any query...contact me with mail-iet.devrajsingh@gmail.com

    ReplyDelete



Hey check out this LuckyKhel App at:
Get it on Google Play Get it on Google Play
LuckyKhel