Variables and Data types are the most basic and very important concept to start with programming.

Variables and Datatypes

In C programming, it is very important to declare a variable before using them and each variable is of a particular type.
Let’s see in detail variables and data types in c.

Variables in c

Variables are anything such as a person’s name, address, number, etc.
Variables definition: Variable is a name given to the memory location.
Once we declare a variable our program can manipulate its value.
Any type of data in C is stored into the memory and we can identify that location by its name that is we address that memory location is referred to as Variables.
Usually, in C, We first declared the Variables.
But as we storing the variables, it can be of any type, therefore, we also have to mention the type of that variable which is its data types.

How to declare the variable?

We can declare the variable in c as follows
Syntax to declare a variable
data_type variable_name=value;

Variable example
int a=10;
In the above example, a is a variable of type int

Rules for variable declaration in c

  • Variable name must start with an alphabet or underscore.
  • A variable name cannot start with a number or any special symbol.
  • No space is allowed in between the variable name declaration.
  • No special character is allowed.

Types of variables

There are two types of variable which as follow

  1. Local variable
  2. Global variable
Global variables

The global variables are the variables which are declared outside of the function at the top of the program.
The global variables can be accessed anywhere in the program.
Example
int var1; // global variable declaration
void main() {
var1=10; //accessing the global variable
}

Local variables

The local variables are the variables which are declared inside the functions.
The local variables can not be accessed outside of the function.
Example
void main() {
var1=10; //local variable declaration
}

Data Types in C

In c we have do declare the variable before using it. These variables store a different type of data such as string, number, float, etc. To store such type of data C provided predefined Data types.

What is Data type?
C data types are defined as the data storage format that a variable can store data to perform a specific operation.
A data type specifies the type of which variables holds the data.
In C, there are various types of data type as integer, float, double, character, etc.

Integer data type: This data type holds an integer type of data.
Float data type: This data type holds Floating type of data.
Char data type: This data type holds a character type of data.
Using these data types we declare the variables.
In C, Variables declaration be the first statement of the program.

Example:
int a b; //global variables.
void main() {
int c; //local variable.
}
As shown above we declared 3 variables as a , b, c which holds integer type of data.

Variables and data types in c program

#include <stdio.h>
int var1=10;
int main()
{
int var2;
char ch;
var2=20;
ch='a';
printf("\nGlobal variable with the data type int=:%d",var1);
printf("\nLocal variable with the data type int=:%d",var2);
printf("\nLocal variable with the data type char=%c",ch);

return 0;
}

Output:

variables and data types in c
variables and data types
Explanation

1. Declare global variable.
=> int var1=10;
2. Start with the main method
3. Declare local variables
=> int var2=20;
=> char ch=’a’
4. print variables with with data types values.
printf(“\nGlobal variable with the data type int=:%d”,var1);
printf(“\nLocal variable with the data type int=:%d”,var2);
printf(“\nLocal variable with the data type char=%c”,ch);
5. End with the program

Write A Comment