• Articles
  • Tutorials
  • Interview Questions

Constants and Variables in C

Variables in C

The variable is used to store the value. As the name indicates its value can be changed or also it can be reused many times. We have to define its Data Types in C as shown below.
Syntax 

Data type variable_name;

e.g. 

int a;

Where ‘a’ is the variables.

Types of Variables in C:

There are many types of variables in c:

  • local variable
  • global variable
  • static variable
  • external variable
  • Automatic variable

1. Local variable – A variable which is declared inside the function is known as local variable. It is used only inside the function in which it is declared.
2. Global variable – A variable which is declared outside the function is known as global variable. It can be used throughout the program.
3. Static variable – It is used to retain its value between multiple function calls. It is declared using static keyword.
4. External variable – You can share a variable in multiple C source files by using external variable. It is declared using extern keyword.
5. Automatic variable – Variable which is declared inside the block is known as automatic variable by default.

Learn the basics of Loops in C through this blog!

Constants in C

Its value is fixed throughout the program that means constants are those variables which value is not changed throughout the program.
C constants can be divided into two major categories:

  • Primary Constants
  • Secondary Constants

C constants
There are two simple ways in C to define constants:

1. Using#define 

e.g.

#include <stdio.h>
#define value 10
void main() {
int data;
data = value*value;
printf("value of data : %d",data);
}

Output
value of data : 100

Certification in Full Stack Web Development

Get the most out of C and become a better developer through this C Tutorial!

2. Using const keyword

e.g.

#include <stdio.h>
void main() {
const int  value = 10;
int data;
data =value*value;
printf("value of data : %d",value);
}

Output
value of data : 100

Course Schedule

Name Date Details
Python Course 30 Nov 2024(Sat-Sun) Weekend Batch View Details
07 Dec 2024(Sat-Sun) Weekend Batch
14 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.