Variables and Data types in C :
Variables and Data types in C are most important.we will learn Variables and Data types in C in this tutorial.
Variables in C :
- Variables are anything such as persons name, address, number etc.
- Definition – Variables in C is the name given to the memory location.
- Any data in C are stored into the memory and we can identify that location by its name that is we address that memory location is referred 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 as follow:
Data types in C :
- A datatype specifies the type of which variables holds the data.
- In C , their are various types of data type as integer , float ,double,character etc.
- Integer data type : This data type holds integer type of data.
- Float data type : This data type holds Floating type of data.
- Char data type : This data type holds character type of data.
Using theses data types we declare the variables.
- In C , Variables declaration be the first statement of the program.
Example :
123456789 | 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.
- 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 any where in program.
Example :
123456789 | int var1; // global variable declarationvoid 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 accessed outside of the function.
Example :
1234567 | void main(){var1=10; //local variable declaration} |
Rules for variable declaration :
- Variable name must start with alphabet or underscore.
- Variable name can not start with number or any special symbol.
- No space is allowed in between the variable name declaration.
- No special character is allowed.
C1234567891011121314151617181920//program to illustrate variable and datatype.#include<stdio.h>int var1;void main(){ int var2; char ch; var1=10; var2=20; char=’a’; printf(“global variable with the data type int=:%d”,var1); printf(“local variable with the data type int=:%d”,var2); printf(“local variable with the data type char=:%c”,var3);}Output :global variable with the data type int=:10local variable with the data type int=:20local variable with the data type char=:a