Variables And Constants
In here we will learn about Variables And Constants . What is variable ? What is Constant ? What are the Variable Declaration rules ? The ranges of variables Why we use constants and types of constant
Variables: A variable is nothing but a name of a memory address that our program can manupulate . You can consider as an empty pot that you can put something or some value . In C programming, it has different data types that we have learnt previous chapter . In a variable which type value you want to place and what is ranges it depends on the data type.In C programming there is some rules to declare and use of the variables.
Variable Declaration:
Data_Type Variable_name;//must give a space between data type and variable name.
Rules for Naming variables:
Variable names must begin with a letter of the alphabet or an underscore( _ ) but for beginner programmer I will suggest you to start with alphabet.
After the first initial letter you can use other letters, numbers etc but you can not have space in a variable name
You can not use same name for different variables.
If you are declare more than one variable of same type must be separate with ","(comma) and must give semicolon(;) end of the line.
You can not use the keywords(reserved words)that we shown on previous chapter as a variable name .
We can not duplicate variable name in the same function.
Let's see an example
#include<stdio.h>
int main()
{
int a; // declare a single variable
int c, d; //declare more than one variable of same data type.
float ad, salary; //declare float type variable
double n; //declare double type variable
char g; // declare a Charater type variable
return 0;
}
Ranges of Data type
DATA TYPE | RANGES |
---|---|
short int | -32,768 to 32,767 |
unsigned short int | 0 to 65,535 |
int | -2,147,483,648 to 2,147,483,647 |
long int | -2,147,483,648 to 2,147,483,647 |
long long int | -263 to 263-1 |
char | 1 Charater |
float | -2,147,483,648 to 2,147,483,647 |
double | -263 to 263-1 |
There is two types of variables-
Local variables:If you decalare variable in between a function like
main()
or any other function that is call local variables . You can not access the Local variables outside the function where it is declared.Global variables:If declare the variable outside of the function that is call global variable.You can access the Global variables from any where of your program.
For an example:
#include<stdio.h>
int a,b,c; //global variables.
int main(){
int x,y,z; //local variable
return 0;
}
Constants: The Constants is a fixed value that you can not change using
any of operators in your program.It is used to get rid of writing same value over and over again.
like value of PI 3.14159265358979323846264338327950288 ....... if you need to write
same value for several times is very much annoying so instead of that you can declare
it as a constant. There are two different ways to declare constant and these are -
1.Using #define
.
2.Using const
key before declare variables.
See the following
example:
#include<stdio.h>
#define PI = 3.14159265358979323 //using define
int main()
{
const int a = 3.14159265358979323; // using const. Must use ; end of the line
return 0;
}
Best way of learing
Self learing is the best learing in the world. Discover yourself first then will get what you are And what you want to do .It will push you for self learing.
Why you need to learn coding?
Coding will play a vital role in one's life . It will help to open a new window of thinking . You can think better way than past . It helps to organise all the thing in better way .