Variables in C

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:03:00

What is a Variable?

In C, a variable is a memory location with a name used to store and retrieve data. We can store several types of data in the variable and reuse it to store other data multiple times.

Rules for Naming Variables

  • The name can include alphabets (both capital and lowercase), numerals, and underscores (_).
  • The first character must be an alphabet or underscore.
  • Variable names are case-sensitive, therefore "num" and "Num" are two distinct variable names.
  • The variable names must not contain white spaces or special characters such as ! or *.
  • A keyword cannot be used as a variable name.
  • After declaring a variable, its type cannot be modified.

Scope of Variables

A scope is a program region, and there are generally three places where variables can be declared: ​

  • Local variables are defined as variables that exist within a function or block.​
  • In the definition of function parameters, also known as formal parameters.​
  • Global variables are defined as variables that exist outside of any function.

Types of Variables

In the C programming language, there are five types of variables:

  1. Local Variable
  2. Global Variable
  3. Static variable
  4. Automatic Variable
  5. External Variable

Local Variable in C

Local variables are declared and initialized at the beginning of a function or block, and memory is allocated inside its execution scope. Only statements within that function can access the local variable. Such variables are removed when the control exits the function.

Global Variable in C

All functions in C can access the global variable because it is defined outside of any function. In C, global variables are initialized at the program start and are kept in memory until the program terminates.

Static Variable in C

Static variables in C hold their value in between function calls and only require initialization once. Although they are local to the block in which they are declared, they hold their value for the duration of the program.

Automatic Variable in C

When a block is entered, an automatic variable is produced, and when it is exited, it is destroyed. They are unavailable outside their designated scope and usually utilized within functions.

External Variable in C

External variables are declared outside of any function so that different functions in the file can access them. If the 'extern' keyword is used to declare them, functions in other files can also access them.

Defining Variable

A variable can be defined in three ways:

  1. Declaration of Variables
  2. Definition of Variable
  3. Initialization of Variables

Declaration of Variables

In C, a variable declaration notifies the compiler that a variable with the specified name and data type exists.The compiler automatically allocates memory for a variable at the time of declaration.

Definition of Variable

A C variable is defined by the compiler, which also allocates some memory and some value to it. Until it is initialized, a specified variable will have some random trash value in it.

Initialization of Variables

The process of giving a variable a meaningful value by the user is known as initialization.

Constant Variable in C

When a variable is defined in C, it is read-only and cannot have its value changed. This is known as a constant variable. The const keyword allows us to declare a constant variable.

Format Specifiers

They inform the compiler about the kind of data value that is kept in a variable. With the printf() function, they are utilised. It is shown as a % sign with a character enclosed in double quotes,"".

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this