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;<br>

e.g. 
int a;<br>

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><br>
#define value 10<br>
void main() {<br>
int data;<br>
data = value*value;<br>
printf("value of data : %d",data);<br>
}<br>

Output
value of data : 100
Get the most out of C and become a better developer through this C Tutorial!

2. Using const keyword

e.g.

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

Output
value of data : 100
 

Related Blogs What’s Inside
PyCharm Installation Explains how to set up PyCharm for Python development tasks.
Spring Boot Interview Questions Offers Spring Boot interview questions for Java programming roles.
Doubly Linked List in C Guides on creating doubly linked lists in C for efficient data handling.
JUnit Interview Questions Details JUnit questions for mastering Java testing interviews.
PyCharm Tutorial Details PyCharm tools for enhancing Python programming productivity.
SOA Interview Questions Lists SOA interview questions for service-oriented architecture roles.
Keywords and Comments in C Details C keywords and comments for writing clear code.
C Installation Provides instructions for installing a C programming environment.
Some Programs in C Features C programs to illustrate basic coding principles.
Mockito Annotations Explains Mockito annotations for effective Java unit testing.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a Software Developer and Technical Research Analyst Lead with around 3 years of experience in full-stack development, Artificial Intelligence, cloud computing, and system design. He specializes in building scalable applications and creating industry-focused technical content on AI, programming, web development, cloud technologies, and modern software engineering.