What is a global variable in c?

0

Can any one explain what is global variable in c with example.

Tags: asked May 9, 2012

1 Answer

1

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 */
}
Avatar Image
chitra
35

Your Answer

Please login to post questions.

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: