Variable In C Language | Declaration, Definition And Syntax of Variable in C

Today we are going to learn about the variable In C Language. In this article, we will learn what is variable in C language, Basic Syntax of a Variable, and some rules of variable naming.

Let’s first know what is variables in C language?

Variable In C Language
Variable In C

What Is Variable In C Language

Variable is the name of memory location, where we store data or information. we can easily change and update the variable several times, during the execution of the program. Variables are a type of identifier that we use to store data.

Let us understand the variable better by this one example -:

Whenever we want to store any information or data in our computer or laptop, then we store it in the memory of our computer. The operating system provides a complex address, in which data is stored in memory, which is difficult for normal users like us to remember.

In such a situation, the operating system also provides some features to the normal user so that he can decide the name of the place in which he wants to keep that data and if he creates a folder, then he Can keep the data or information inside that folder. so that, if he needs that data or information further, then he can easily find and retrieve it.

Similarly, when we create a program in the C language, and if we want to store any data in that program, then we store that data or information in memory And the place where we have stored that data or information, we give a name of that place. and that place name is called a variable, we give memory location a name so that data can be easily found later.

In simple words, a Variable is the name of the place in memory where we store any data or information, so that, if the information is needed then it can easily find and use it.

Syntax to Declare a Variable

Data_type Variable_name

Example of declaring a variable :

char my_name = "CsTutorialpoint";

In this example, char is a data type and my_name is the name of the variable in which CsTutorialpoint is being stored. Apart from char data type, we can store any kind of data using any other data type. To know more about data type, read from here What is Data types

So friends here, you have understood what is a variable, why variable is used, and what is the basic syntax of a variable. Now let’s learn what is the rule for declaring a variable?

Rules for Naming a Variable

  • Variable names can only use alphabets, numbers, and underscores.
  • The first letter of the variable’s name will be either a letter or an underscore.
  • Variable names cannot begin with a number.
  • The variable name should be Meaningful.
  • Blank and free space cannot be used in the name of a variable.
  • A keyword cannot be used as a variable name.
  • In C language, if you write a variable in lowercase letter and the same variable in uppercase letter, then both of them are different variables in terms of C language because C language is a case sensitive language.

Following are the examples of valid variable names in a ‘C’ program:

Name_is
_nameis
_Nameis14
Name or name

Example -:

int number_is = 410;

Following are the examples of invalid variable names in a ‘C’ program:

Name@Jeetu
Your Name
1Nameis

Declaring, Defining, and Initializing a variable

Variable Declaration -:

When we create a variable to store data in C language, first of all we have to declare that variable and this is called declaration.

Through variable declaration, we tell the compiler that:

  • What is the name of the variable
  • Which type of data is going to be stored in the variable.
  • How much space in memory will be needed to store data to the variable so that the compiler can allocate memory to that variable correctly.
  • By declaring the variable, we tell the compiler that we are going to use this name and type in the variable in our program.

Syntax of Declaring a variable

data_type variable_name;

Example -:

int num;

num = 78;

In this example int is a data type that tells the compiler that the integer type value is going to be stored in this variable and num is the name of the variable in which we are storing 78 .

We can declare the variable using the extern keyword, although it is optional.

Example -:

extern int a, b, c;

Variable Definition -:

When we declare a variable in our program and tell the compiler that this data type and name of variable is going to be used in our program, it is called variable definition. Variable declaration and variable definition are related to each other.

Example -:

int a;

float b, c;

Initializing a variable -:

When we store a value in the declared variable, it is called initialization.

Example -:

int num;

num = 100;

In this example, we store 100 values in a variable named num, by num = 100.

We can also initialize a variable in a single line by a single statement.

Example -:

int num = 96;

Types of Variables in C

In C language, Variables are of the following types:

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

1. Local variable

A variable that we declare inside a function or block is called a local variable.

The scope of the local variable is only up to that block or function, we cannot use such variable in any other function.

Declaration of such variables has to be done at the beginning and the local variable needs to be initialized before use.

The scope of a local variable is only up to the block in which that declare is, we cannot use such variable in any other block or function.

Example -:

#include <stdio.h>
void fun() 
{
  int x = 150;  // local variable
}
int main()
{
  fun();
}

In this example, the scope of x variable is only up to the fun () function, we cannot use it in any other function except the fun () function.

2. Global variable

Global variables are variables that are declared outside the function or block. We can use the global variable in any other block or function. The value of this variable can be used and changed in any function.

It is necessary to declare this variable before using it.

Example -:

#include <stdio.h>
int x = 10;     //global variable
void fun1()
{
  printf("%d \n" , x);
}
void fun2()
{
  printf("%d \n" , x);
}
int main() 
{
  fun1();
  fun2();
  return 0;
}

Output -:

10
10

In the above example, we have declared the x variable outside the function or block and used this x variable with both fun1 () and fun2 () functions because it is a global variable that we can use with any function.

3. Static variable

The Static keyword is used to declare a Static variable. If we declare a Static variable inside a function or block, it is called the local Static variable and if the declaration of the Static variable is outside the function, it is called the global Static variable.

The value of the Static variable is by default zero when declaring the value while the normal variable has a by default garbage value.

Example -:

include <stdio.h>
void fun()
{ 
int x = 10;              //local variable 
static int y = 30;    //static variable 
x = x + 10; 
y = y + 10; 
printf("\n %d, %d",x,y); 
} 
int main() 
{
  fun();
  fun();
  fun();
  return 0;
}

Output -:

20 , 40
20,  50
20,  60

When Static variable is used in a function, the number of times that function is called is used along with the increased value of the static variable, just like in this program we have used the static variable which is used with the increased value. Is happening which you can see in the output.

4. External variable

We declare the external variable using the extern keyword. We can use external variable in various source files.

Example -:

myfile.h

  extern int x=10;   //external variable (also global)  

  program1.c 
  #include "myfile.h"  
  #include <stdio.h>  
  void printValue()
{  
  printf("Global variable: %d", global_variable);  
  }

In this example, x is an external variable that is used in multiple files.

5. Automatic variable

In C language, all the variables we declare inside the block are all automatic variables. It is not very necessary to declare this variable using auto keyword, it is similar to a normal variable or say that all normal variables are by default automatic variables.

Example -:

#include <stdio.h>
void function()
{
  int x=10;//local variable (also automatic) 
  auto int y=20;//automatic variable
}
int main() 
{
    function();
    return 0;
}

In the above example, x and y are both automatic variables but there is only such difference that we have declared the x variable without the auto keyword and declare y with the auto keyword.

Read More -:

Conclusion -:

Friends, I hope you have found the answer to your question and you will not have to search about the variables in c language.

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the variable in C language.

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment