Circuit Negma

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

Global Variables :: Revisited

Posted by Circuit Negma on March 30, 2009


Created By: Hussein Nosair

Click on the image to download the Global Variables Project.

Also, refer to the following post {Extract Files from Pictures} on how to extract the zip file from the image.

global-variables by you.

Main.c File

   1: /*********************************************************************
   2:  *
   3:  *                  Main Program
   4:  *
   5:  *********************************************************************
   6:  * FileName:        main.c
   7:  * Dependencies:    None
   8:  * Processor:       Intel
   9:  * Compiler:        Mingw
  10:  * Project:         Global Variables
  11:  * Software:        Dev-C++ 4.9.9.2
  12:  *
  13:  *
  14:  * Created By:      Hussein Nosair
  15:  * Date:            03/30/2009
  16:  * 
  17:  *********************************************************************
  18:  * File Description:
  19:  *                     This project will demoenstrate the use of global
  20:                         variables and definitions
  21:  * Change History:
  22:  *  Rev   Date         Description
  23:  *  1.0   03/30/09     Project Created
  24:  ********************************************************************/
  25:  
  26: #include <stdio.h>
  27: #include <stdlib.h>
  28:  
  29: /****** NOTE ::
  30:         Must include the Global variabls header file in the main program
  31: *******/
  32: #include "GlobalVariables.h"     // Include defined variables and functions
  33:  
  34:  
  35: /************** Local Global Functions Definitions *************************
  36: * The following is a local global functions to the main program. This means
  37: * that any part of the code in main.c can call those 2 functions. However, this
  38: * would not work with you if you try to call those 2 functions from GlobalVariables.c
  39: * file or else where.
  40: ****************************************************************************/
  41: void func3(void);               // Define Local function
  42: void func4(void);               // Define Local function
  43:  
  44:  
  45: int main(int argc, char *argv[])
  46: {
  47:     printf("i = %d \n", i);     // Print out initial value of i
  48:     printf("n = %d \n", n);     // Print out initial value of n
  49:  
  50:     i = 8;                      // Set a new value for i
  51:     printf("i = %d \n", i);     // Print out the new value of i
  52:  
  53:     n = 4;                      // Set a new value for n
  54:     printf("n = %d \n", n);     // Print out the new value of n
  55:  
  56:     func1();                    // Call global function = function 1 
  57:     func2();                    // Call global function = function 2
  58:  
  59:     for(i=0; i<6; i++)          // Print out global Array
  60:         printf("%d", Array[i]);
  61:  
  62:     // Print out blank lines
  63:     printf("\n");
  64:     printf("\n");
  65:  
  66:     func3();                   // Call local function = function 3
  67:  
  68:     // Print out blank lines
  69:     printf("\n");
  70:     printf("\n");
  71:  
  72:     func4();                   // Call local function = function 4
  73:  
  74:   system("PAUSE");    
  75:   return 0;
  76: }
  77:  
  78: /****************** Local Global Functions Initialization *****************
  79: * The following is initialization of the local global functions in main.c
  80: * file. Please, note that you can not initialize the local global functions
  81: * somewhere eles other than main.c file.
  82: ***************************************************************************/
  83: // Definition of local global function 3
  84: void func3(void)
  85: {
  86:      printf("This is ... Function 3 \n");
  87:      printf("Please, Enter a new value for variable i = ");
  88:      scanf("%d", &i);
  89:      printf("The new value for variable i = %d \n", i);
  90: }
  91:  
  92: // Definition of local global function 4
  93: void func4(void)
  94: {
  95:      printf("This is ... Function 4 \n");
  96:      for(i=0; i<6; i++)
  97:      {
  98:           printf("Please, Enter a new value for variable Array[%d] = ", i);
  99:           scanf("%d", &Array[i]);
 100:      }
 101:  
 102:     // Print out blank lines
 103:     printf("\n");
 104:     printf("\n");
 105:  
 106:      for(i=0; i<6; i++)
 107:         printf("a new value for variable Array[%d] = %d \n", i, Array[i]);
 108: }

 

Global Variables.h File

   1: /*********************************************************************
   2:  *
   3:  *                  Global Variables Definitions
   4:  *
   5:  *********************************************************************
   6:  * FileName:        GlobalVariables.h
   7:  * Dependencies:    None
   8:  * Processor:       Intel
   9:  * Compiler:        Mingw
  10:  * Project:         Global Variables
  11:  *
  12:  * Created By:      Hussein Nosair
  13:  * Date:            03/30/2009
  14:  * 
  15:  *********************************************************************
  16:  * File Description:
  17:  *                     This project will demoenstrate the use of global
  18:                         variables and definitions
  19:  * Change History:
  20:  *  Rev   Date         Description
  21:  *  1.0   03/30/09     Project Created
  22:  ********************************************************************/
  23:  
  24: #ifndef __GLOBAL_VARIABLES_H_
  25: #define __GLOBAL_VARIABLES_H_
  26:  
  27:  
  28: #ifndef NULL
  29: #define NULL    0
  30: #endif
  31:  
  32:  
  33: typedef unsigned char        BYTE;                // 8-bit unsigned
  34: typedef unsigned short int    WORD;                // 16-bit unsigned
  35:  
  36:  
  37:  
  38: /************** Global Global Variables Definitions *************************
  39: * The following is a global global variables to the Project. This means
  40: * that any part/any file within the project can call these variables.
  41: ****************************************************************************/
  42: extern int i;
  43: extern int n;
  44: extern BYTE Array[];
  45:  
  46:  
  47:  
  48: /************** Global Global Functions Definitions *************************
  49: * The following is a global global functions to the Project. This means
  50: * that any part/any file within the project can call those 2 functions.
  51: ****************************************************************************/
  52: extern void func1();
  53: extern int func2();
  54:  
  55:  
  56: #endif

 

Global Variables.c File

   1: /*********************************************************************
   2:  *
   3:  *                  Global Variables Declaration
   4:  *
   5:  *********************************************************************
   6:  * FileName:        GlobalVariables.c
   7:  * Dependencies:    None
   8:  * Processor:       Intel
   9:  * Compiler:        Mingw
  10:  * Project:         Global Variables
  11:  *
  12:  * Created By:      Hussein Nosair
  13:  * Date:            03/30/2009
  14:  * 
  15:  *********************************************************************
  16:  * File Description:
  17:  *                     This project will demoenstrate the use of global
  18:                         variables and definitions
  19:  * Change History:
  20:  *  Rev   Date         Description
  21:  *  1.0   03/30/09     Project Created
  22:  ********************************************************************/
  23:  
  24:  
  25:  
  26: /************* Functions and Variables Definition Library *************
  27: * This Section of the code associate GlobalVariables.c File with the 
  28: * header file GlobalVariables.h {library}. If you do not include the 
  29: * appropriate header/library file, then either you need to define the 
  30: * functions and variables here in this file or the compiler will complain
  31: * about the functions and variables.
  32: ***********************************************************************/
  33: #include "GlobalVariables.h"
  34:  
  35:  
  36:  
  37:  
  38: /************** Initializing Variables *************************/
  39: int i = 0;                       // Define i and initialize
  40: int n = 0;                       // Define n and initialize
  41: BYTE Array[6] = {1,2,3,4,5,6};   // Define Array and initialize
  42:  
  43:  
  44:  
  45:  
  46: /****************** Global Global Functions Initialization *****************
  47: * The following is initialization of the global global functions in Project
  48: * Please, note that you can not initialize the global global functions
  49: * somewhere eles other than GlobalVariables.h and GlobalVariables.c,; However
  50: * it is recommended that you initialize the functions within the GlobalVariables.c
  51: * file to maintain tidiness and proper documentaion 
  52: ***************************************************************************/
  53: // Initialize func1
  54: void func1(void)
  55: {
  56:     printf("This is ... function 1 \n");
  57: }
  58:  
  59: // Initialize func2
  60: int func2(void)
  61: {
  62:     printf("This is ... function 2 \n");
  63:     return 0;
  64: }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: