Global variable c – What is local and global variable in C

What is local variable in C

  • local variable is declared inside a function.
  • A local variable is visible only inside their function, only statements inside function can access that local variable.
  • Local variables are declared when control enters a function and local variables gets destroyed, when control exits from function.

What is global variable in C

  • Global variables are declared outside of any function.
  • A global variable is visible to any every function and can be used by any piece of code.
  • Unlike local variable, global variables retain their values between function calls and throughout the program execution.

What is the difference between auto variable and register variable in C

Global variable c: A variable which is declared inside a function or block is automatic variable by default. We can declare automatic variables using auto keyword, but it is rarely used because by default every variable is automatic variable.

Declaring a variable with register keyword is a hint to the compiler to store this variable in a register of the computer’s CPU instead of storing it in memory. Storing any variable in CPU register, will reduce the time of performing any operation on register variable. We can declare register variables using register keyword.