What are Global Variables - C Function

C examples for Function:Global Variable

Introduction

The global variables are known throughout the program.

You create global variables by declaring them outside of any function.

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

The global variable is usually declared at the top of the program.

Demo Code

#include <stdio.h>
int count;  /* count is global  */

void func1(void);
void func2(void);

int main(void)
{
    count = 100;//w  w w.j  a v a 2  s  .com
    func1();

    return 0;
}

void func1(void)
{
    int temp;

    temp = count;
    func2();
    printf("count is %  d", count); /* will print 100 */
}

void func2(void)
{
    int count;

    for (count = 1; count<10; count++)
        putchar('.');
}

Related Tutorials