Variable
It is a memory location to store some type of data.
Since the data that is storing in the memory space can vary during program execution it is called as ‘Variable’.
Variable is referring with an identifier through out the program.
How to create variable?
Variable can be created with Declaration Statement. The declaration statement is used to introduce a variable with its characteristics.
syntax of declaration statement:
datatype identifier;
Eg: int x;
datatype
it is a keyword denotes the type of data that the variable can hold.
There are 4 basic datatypes in C Languge: int, float, double, char
>>>Data Storage into Variable
Variable can be stored with a value with assignment statement or user input.
Assignment Statement:
The statement that has assignment operator is called as ‘Assignment Statement’
The equal to (=) symbol is called as Assignment Operator in C Language.
4 syntax or general format of assignment statement:
var=val;
var=var;
var=expr;
var=func();
>>>
Example:
x=1;
y=x;
z=x+y;
a=test();
>>>Display value of Variable
Value stored in the variable can be displayed with printf() function.
Syntax2 of printf():
printf(“%fsp”,value);
printf(“%fsp”,var);
printf(“%fsp”,expr);
Format Specifier
special characters followed by % sign that determines the type of data to be printed.
Commonly used format specifiers:
%d – integer
%f – float
%lf – double
%c – char
//Example1
#include<stdio.h>
void main()
{
printf(“%d”,5);
int x;
x=15;
printf(“\nx=%d”,x);
printf(“\n%d”,x+5);
}
//Example2
#include<stdio.h>
void main()
{
int x,y;
x=12;
y=24;
printf(“x=%d,y=%d”,x,y);
}
#include<stdio.h>
void main()
{
float temp;
temp=2.34;
printf(“temp=%0.2f”,temp); //0.2 is precision specifier
}
#include<stdio.h>
void main()
{
char choice;
choice=’#’;
printf(“choice=%c”,choice);
}
//Example
#include<stdio.h>
void main()
{
char ch;
ch=’A’;
printf(“ch=%c\n”,ch);
ch=’#’;
printf(“ch=%c\n”,ch);
ch=’9′;
printf(“ch=%c”,ch);
}
Practice Questions)
1. Write C program to create an integer variable with identifier ‘first’, store value 25 into it and display on the monitor.
2. Write C Program to create two variables with your desired values and display their sum
3. Write C program to calculate and display area of rectangle for the given length and breadth
4. program to calculate area of circle for the given radius
5. program to calculate average of any given three numbers
Since the data that is storing in the memory space can vary during program execution it is called as ‘Variable’.
Variable is referring with an identifier through out the program.
How to create variable?
Variable can be created with Declaration Statement. The declaration statement is used to introduce a variable with its characteristics.
syntax of declaration statement:
datatype identifier;
Eg: int x;
datatype
it is a keyword denotes the type of data that the variable can hold.
There are 4 basic datatypes in C Languge: int, float, double, char
>>>Data Storage into Variable
Variable can be stored with a value with assignment statement or user input.
Assignment Statement:
The statement that has assignment operator is called as ‘Assignment Statement’
The equal to (=) symbol is called as Assignment Operator in C Language.
4 syntax or general format of assignment statement:
var=val;
var=var;
var=expr;
var=func();
>>>
Example:
x=1;
y=x;
z=x+y;
a=test();
>>>Display value of Variable
Value stored in the variable can be displayed with printf() function.
Syntax2 of printf():
printf(“%fsp”,value);
printf(“%fsp”,var);
printf(“%fsp”,expr);
Format Specifier
special characters followed by % sign that determines the type of data to be printed.
Commonly used format specifiers:
%d – integer
%f – float
%lf – double
%c – char
//Example1
#include<stdio.h>
void main()
{
printf(“%d”,5);
int x;
x=15;
printf(“\nx=%d”,x);
printf(“\n%d”,x+5);
}
//Example2
#include<stdio.h>
void main()
{
int x,y;
x=12;
y=24;
printf(“x=%d,y=%d”,x,y);
}
#include<stdio.h>
void main()
{
float temp;
temp=2.34;
printf(“temp=%0.2f”,temp); //0.2 is precision specifier
}
#include<stdio.h>
void main()
{
char choice;
choice=’#’;
printf(“choice=%c”,choice);
}
//Example
#include<stdio.h>
void main()
{
char ch;
ch=’A’;
printf(“ch=%c\n”,ch);
ch=’#’;
printf(“ch=%c\n”,ch);
ch=’9′;
printf(“ch=%c”,ch);
}
Practice Questions)
1. Write C program to create an integer variable with identifier ‘first’, store value 25 into it and display on the monitor.
2. Write C Program to create two variables with your desired values and display their sum
3. Write C program to calculate and display area of rectangle for the given length and breadth
4. program to calculate area of circle for the given radius
5. program to calculate average of any given three numbers