0% found this document useful (0 votes)
14 views

Lecture-02 & 03_Data Types, Variables, Constant

Uploaded by

l4420913
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture-02 & 03_Data Types, Variables, Constant

Uploaded by

l4420913
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture – 02 & 03

Data Types, Variables, Constant


(Definition, Description, Example)

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.

There are the following data types in C language:


Basic Data Types:
C has the following basic built-in data types.
 int  id, age,
 float  height, width
 double  salary, price
 char  section, letter

int - data type


int is used to define integer numbers.
{
int count;
count = 1;
}

float - data type


float is used to define floating point numbers.

{
float weight;
weight = 5.6;
}

double - data type


double is used to define BIG floating-point numbers. It reserves twice the storage for the number.
On PCs this is likely to be 8 bytes.

{
double atoms;
atoms = 2500000;
}
char - data type
char defines characters.
{
char letter;
letter = 'a';
}

Data Type Modifiers/ Type Qualifiers:


The data types explained above has the following modifiers.

 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:

Calculating the range of Float Data types:


Calculating the range of Character Data types:

Summary of Data Types ranges:


Size of Data Types Related Program:

More details regarding 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:

Syntax: void function_name


Ex: void main()
Array: An array is a collection of data items, all of the same type, accessed using a common
name. Array works on integer and character values and can be declared as

Syntax: data type array_name [array_size]


Ex: int student[50]

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:

Syntax: data type *var_name;


Ex: int *p;

Enum (Enumeration): Enumeration is a user-defined datatype in C language. It is used to assign


names to the integral constants which make a program easy to read and maintain. The enum is
used for values that are not going to change (e.g., days of the week, colours in a rainbow, number
of cards in a deck, etc.

enum day {Saturday, Sunday, Monday, Tuesday, Thursday, Friday};

Structure (struct): Structure is a user-defined datatype in C language that allows us to combine


data of different types together. Structure helps to construct a complex data type that is more
meaningful. It is somewhat similar to an Array, but an array holds data of a similar type only. But
structure, on the other hand, can store data of any type, which is practical and more useful.

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

Union allocates one common storage space for all


its members.
Structure allocates storage space for
Union finds that which of its member needs high
all its members separately.
storage space over other members and allocates
that much space

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.

Structure example: Union example:


struct student union student
{ {
int mark; int mark;
char name[6]; char name[6];
double average; double average;
}; };
There are three places where variables you can declare variable programming language:

 Inside a function or a block: Local variables


 Outside of all functions: Global variables
 In the definition of function parameters: Formal parameters

How to declare a variable:


1. Choose the "type" you need.
2. Decide upon a name for the variable.
3. Use the following format for a declaration statement:
datatype variable identifier;
4. You may declare more than one variable of the same type by separating the variable names
with commas.
int age, weight, height;
5. You can initialize a variable (place a value into the variable location) in a declaration
statement.
double mass = 3.45;

The Programming language C has two main variable types


 Local Variables
 Global Variables

Local Variable

 Local Variable is defined as a type of variable declared within a programming block or


subroutines. It can only be used inside the subroutine or code block in which it is
declared. The local variable exists until the block of the function is under execution. After
that, it will be destroyed automatically.

Example of Local Variable

int add()
{
int a =4;
int b=5;
return a+b;
}

Here, 'a' and 'b' are local variables


Global Variable

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;
}

Here, 'a' and 'b' are global variables.

Types of Variables in C in details:


There are many types of variables in C:

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() {

int x=10; //local variable

You must have to initialize the local variable before it is used.


Global Variable:

 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:

int value=20; //global variable

void function1() {

int x=10; //local variable

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() {

int x=10; //local variable

static int y=10; //static variable

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(){

int x=10; //local variable (also automatic)

auto int y=20; //automatic variable

External Variable:

 We can share a variable in multiple C source files by using an external variable. To


declare an external variable, you need to use extern keyword.
Ex:

extern int x=10; //external variable (also global)

A Complete ‘C’ program by using local and global variables:

#include <stdio.h>
/*global variables*/
int a,b;

/*function to set values to the global variables*/


void setValues(void)
{
a=100;
b=200;
}
int main()
{
/*local variables*/
int x,y;

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.

Differences between Local and Global Variables


Parameter Local Global
Scope It is declared inside a function. It is declared outside the function.
If it is not initialized, a garbage value is If it is not initialized zero is stored as
Value
stored default.
It is created when the function starts It is created before the program's global
Lifetime execution and lost when the functions execution starts and lost when the
terminate. program terminates.
Data sharing is not possible as data of the Data sharing is possible as multiple
Data sharing local variable can be accessed by only one functions can access the same global
function. variable.
Parameters passing is required for local Parameters passing is not necessary for a
Parameters variables to access the value in other global variable as it is visible throughout
function the program
When the value of the local variable is When the value of the global variable is
Modification of
modified in one function, the changes are modified in one function changes are
variable value
not visible in another function. visible in the rest of the program.
Local variables can be accessed with the
You can access global variables by any
Accessed by help of statements, inside a function in
statement in the program.
which they are declared.
It is stored on a fixed location decided by
Memory storage It is stored on the stack unless specified.
the compiler.
Constant
A constant can be defined as a fixed value, which is used in algebraic expressions and equations.
A constant does not change over time and has a fixed value.
For example, the size of a shoe or cloth or any apparel will not change at any point.
In an algebraic expression, x+y = 8, 8 is a constant value, and it cannot be changed.
Declaration process of Constant in a Program:
Process 01: const <data_type> <var_name> = <value>;
Ex:
const int STUDENT_ID = 01;
const int COURSE_CODE = 1201;

Process 02: #define <VAR_NAME> <VALUE>

Ex:

#define PI 3.1416

#define g 9.83

Difference Between Constants and Variables

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.

You might also like