Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

Posts Tagged ‘Programming language’

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

Posted by Circuit Negma on July 16, 2008


Created By: Hussein Nosair

What is the best way to declare and define global variables and functions?

Declaration vs. Definition:

1. Declaration is a statement of a programmatic entity (ex. variable, function, union, structure) to be referenced in other parts of a program/code, which has its definition somewhere else.

2. Definition is the actually description, allocated space and initialization of a declared programmatic entity (ex. variable, function, union, structure).

Purpose:

a global variable or function is a variable or function that need to be shared or accessed across several source files, keeping in mind that the definition and declaration is consistent with each other.

Right                                                             Wrong

Definition:

int a;                                                             int a;

Declaration:

int a = 0;                                                       char a = 0;

OR

a global variable or function is a variable or function that need to be shared or accessed within a single source file, keeping in mind that the definition and declaration is consistent with each other.

*** The best way to declare and define a global variable is to place the declaration in a header file (.h) and the definition is a source file (.c).

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.

Posted in C Programming | Tagged: , , , | 16 Comments »