Local and Global variables in C - Local and Global variables



 difference between local and global variables in c

Learn C - C tutorial - Local and Global variables in c - C examples - C programs

Local and Global variables in C

  • GLOBAL VARIABLE
    • A variable that is declared outside all functions, is said to be the global variable.
    • A global variable might be used in all functions.
  • LOCAL VARIABLE
    • A variable which is declared within the function, they were local variables.

Sample Code

#include <stdio.h>
int a,b;
void setValues(void)
{
    a=100;
    b=200;
}
int main()
{
    int x,y;
    x=10;
    y=20;
    setValues();
    printf("a=%d, b=%d\n",a,b);
    printf("x=%d, y=%d\n",x,y);
    return 0;
}

Output

a=100, b=200
x=10, y=20


View More Quick Examples


Related Searches to Local and Global variables in C - Local and Global variables