Lecture-02 & 03_Data Types, Variables, Constant
Lecture-02 & 03_Data Types, Variables, Constant
Data Types
C has a concept of 'data types' which are used to define a variable before its use. The definition
of a variable will assign storage for the variable and define the type of data that will be held in
the location. The value of a variable can be changed any time.
A Data Type is a type of data.
Data Type is a data storage format that can contain a specific type or range of values.
{
float weight;
weight = 5.6;
}
{
double atoms;
atoms = 2500000;
}
char - data type
char defines characters.
{
char letter;
letter = 'a';
}
short
long
signed
unsigned
The modifiers define the amount of storage allocated to the variable. The amount of storage
allocated is not cast in stone. ANSI has the following rules:
short int<= int<= long int
float <= double <= long double
What this means is that a 'short int' should assign less than or the same amount of storage as an
'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real
world is:
Type Bytes Range
---------------------------------------------------------------------
short int 2 -32,768 -> +32,767 (32KB)
unsigned short int 2 0 -> +65,535 (64KB)
unsigned int 4 0 -> +4,294,967,295 (4GB)
int 4 -2,147,483,648 -> +2,147,483,647 (2GB)
long int 4 -2,147,483,648 -> +2,147,483,647 (2GB)
signed char 1 -128 -> +127
unsigned char 1 0 -> +255
float 4
double 8
long double 12
These figures only apply to today’s generation of PCs. Mainframes and midrange machines could
use different figures, but would still comply with the rule above. You can find out how much
storage is allocated to a data type by using the sizeof operator discussed in Operator
Types Session.
Calculating the range of Integer Data types:
void type: void type means no value. This is usually used to specify the type of functions which
returns nothing. The void can be as follows:
Pointers: A pointer is a variable whose value is the address of another variable, i.e., direct
address of the memory location. Like any variable or constant, you must declare a pointer before
using it to store any variable address. The general form of a pointer variable declaration is as
follows:
struct Student
{
char name[25];
int age;
char branch[10];
char gender;
};
Union: A union is a data type that has all values under it stored at a single address. It is a special
data type available in C that allows storing different data types in the same memory location. You
can define a union with many members, but only one member can contain a value at any given
time. Unions provide an efficient way of using the same memory location for multiple-purpose.
union car
{
char name[50];
int price;
}
Difference Between Structure and Union In C:
C Structure C Union
Structure occupies higher memory Union occupies lower memory space over
space. structure.
We can access all members of We can access only one member of union at a
structure at a time. time.
Local Variable
int add()
{
int a =4;
int b=5;
return a+b;
}
A Global Variable in the program is a variable defined outside the subroutine or function. It has
a global scope means it holds its value throughout the lifetime of the program. Hence, it can be
accessed throughout the program by any function defined within the program, unless it is
shadowed.
Example:
int a =4;
int b=5;
int add()
{
return a+b;
}
1. Local variable
2. Global variable
3. Static variable
4. Automatic variable
5. External variable
Local Variable:
A variable that is declared inside the function or block is called a local variable.
It must be declared at the start of the block.
Ex:
void function1() {
A variable that is declared outside the function or block is called a global variable. Any
function can change the value of the global variable. It is available to all functions.
It must be declared at the start of the block.
Ex:
void function1() {
Static Variable:
A variable that is declared with the static keyword is called a static variable.
It retains its value between multiple function calls.
Ex:
void function1() {
x=x+1;
y=y+1;
printf("%d, %d",x,y);
N.B: If you call this function many times, the local variable will print the same value for each
function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in
each function call, e.g. 11, 12, 13 and so on.
Automatic Variable:
All variables in C that are declared inside the block, are automatic variables by default.
We can explicitly declare an automatic variable using the auto keyword.
Ex:
void main(){
External Variable:
#include <stdio.h>
/*global variables*/
int a,b;
x=10;
y=20;
setValues();
printf("a=%d, b=%d\n",a,b);
printf("x=%d, y=%d\n",x,y);
return 0;
}
Output:
a=100, b=200
x=10, y=20
Explanation:
In this program,
Global variables are: a and b
Local variables are: x and y
Here, a and b are using within the function set Values () and main() function because they are
global and can be accessed anywhere. While x and y are used within the main() function only
because they are local and declared in main() function, so they are accessible only for the main()
function i.e. their scope is local for main() function only.
Ex:
#define PI 3.1416
#define g 9.83
Constant Variables
A constant does not change its value over time. A variable, on the other hand, changes
its value dependent on the equation.
Constants are usually written in numbers. Variables are specially written in letters
or symbols.
Constants usually represent the known values in an Variables, on the other hand,
equation, expression or in line of programming. represent the unknown values.
Constants are used in computer programming. Variables also have its uses in computer
programming and applications.