SiteBanner

We 'C' programmers make choices about variables all the time.  We design functions with concern about how & what to pass INTO function code, not to mention what to RETURN to the calling code.  'Global' variables are introduced here.  Their scope has broader range than variables 'contained' in a function's body.

 

(right-click to download)

/* pgm21 source */

#include <stdio.h>
#include <string.h>

// 2 global variables
char *g_strpntr;
int     g_numb;

void function1(void);

int main(void)   {
     char airplane[20];    // these two variables are 'private' to function 'main()'
     int engines;             // function1() has no idea they exist, because it receives NO arguments

     strcpy(airplane, "Airbus A350 XWB");
     g_strpntr = &airplane[0];

     engines = 3;
     g_numb = engines;

     printf("\nThe %s airplane has %d engines, right?\n\n", airplane, engines);
     function1();
     printf("Oops. You're right. It's the Boeing 777 that has %d engines...\n\n", g_numb);

     printf("(Honest, I thought the Airbus had %d engines.)\n", engines);

     printf("\n");
     return(0);
     }

void function1() {
     printf("Sorry, the %s only has 2 engines.\n\n", g_strpntr);
     g_numb = 2;
     }