global variables c code example

Example 1: global and local variables in c

#include   //local parameters take  precedance  to global//
 
/* global variable declaration */
int a = 20;
 
int main () {

  /* local variable declaration in main function */
  int a = 10;
  int b = 20;
  int c = 0;

  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);

  return 0;
}

/* function to add two integers */
int sum(int a, int b) {

   printf ("value of a in sum() = %d\n",  a);
   printf ("value of b in sum() = %d\n",  b);

   return a + b;
}

Example 2: how to make a global variable in c#

Make A New Class File And Add This

    internal class Global
    {
        public static int32 Int1 = 1;
        public static string Text1 = "Sample Text";
		public static bool Bool1 = True;
    }

Example 3: variable globlal c

#include 
 
/* global variable declaration */
int g;
 
int main () {

  /* local variable declaration */
  int a, b;
 
  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;
 
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
 
  return 0;
}

Example 4: c how to define a variable

int age;			//int variableName; or int variableName = Value;
			//Integers are whole numbers: 2, 23. They are NOT: 28.4, 8.07.
char first_initial;	//char variableName; or char variableName = 'Character';
			//Characters are single characters on the keyboard. Numbers can 
            //be characters but they are best not stored as such
char name[10]; 		//char stringName[size] or char stringName[size] = "String"
			//Strings are defined as an array of characters that end
            //w/a special character ‘\0’.
float salary;		//float variableName; or float variableName = value;
			//Floats are numbers that contain up to seven digits including decimals.
double micro;		//double variableName; or double variableName = value;
			//Doubles are more precise than floats and can hold more numbers.

Tags:

Misc Example