What is local and global variable in C

Interview Questions
  • What is local variable in C.
  • What is global variable in C.
  • What is the difference between auto variable and register variable in C.

What is local variable in C

  • A 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

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.

Related Topics
What is static variable in C
What are the properties of a register variable in C
What is modifier in C and different types of modifiers.
What is the use of sizeof() function in C
Why is default statement used in switch case in C
What is Address Of(&) Operator and Value Of(*) Operator in C.
What is size of void pointer in C
What are the uses of NULL pointer and void pointer
Can variable names in C have special symbols.
What are different data type in C.