What is a global variable?

variables that are known throughout the program and may be used by any piece of code is called a global variable. Also, they will hold their value throughout the program’s execution. Global variables can be created by declaring them outside of any function.

Global variables are helpful when many functions in the program use the same data. Storage for global variables is in a fixed region of memory set aside for this purpose by the compiler.

In the following program, the variable count has been declared outside of all functions.

Example:

#include "stdio.h"
int count;  /* count is global  */
void func1(void);
int main(void)
{
  count = 100;
  func1();
  return 0;
}
void func1(void)
{
  printf("count is %
d", count); /* will print 100 */
}
Chitra
Chitra

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in