Sie sind auf Seite 1von 19

Data Types in C

Er. Sunali Sharma


Assistant Professor
Hindu Kanya College
Kapurthala
Meaning
C data types are defined as the data storage format that a
variable can store a data to perform a specific operation. Data
types are used to define a variable before to use in a
program.

Uses
• Identify the type of a variable when it declared.
• Identify the type of the return value of a function.
• Identify the type of a parameter expected by a function.
Why Data Types?
1.Every variable must have a type

2. Every variable should get some space in


the memory

3. Help the compiler decide how many bits


should be reserved for a variable
Data Types In C
Data Types in C

Primary Derived User Defined

1. Integer 1. Structure
2. Real 1. Arrays 2. Union
3. Character 2. Pointers 3. Enumeration
4. Void 4. Typedef
A. Primary Data Types
These are fundamental or basic data types in C namely

1. Integer

2. Real

3. Character

4. void
1. Integer Data type
Integers are used to store whole numbers

Type Size(bytes) Range Format Specifier


int or signed int 2 -32,768 to +32767 %d
unsigned int 2 0 to 65535 %d
long int or signed 4 -2,147,483,648 to %ld
long int +2,147,483,647
unsigned long int 4 0 to 4,294,967,295 %lu

• Unsigned -> means positive number if no sign i.e. +


• Signed -> means positive or negative number with sign i.e. +/-
2. Real Data Type
Floating types are used to store real numbers.

Type Size(bytes) Range Format Specifiers


float 4 3.4X10 -38 to 3.4X10+38 %f
double 8 1.7X10-308 to %lf
1.7X10+308
long double 10 3.4X10-4932 to %Lf
3.4X10+4932
3. Character Data Type
Character types are used to store characters value.

Type Size(bytes) Range Format


Specifiers

char or signed char 1 -128 to 127 %c


unsigned char 1 0 to 255 %c
4. Void Data Type
void type means no value. This is usually used to specify the type of
functions which returns nothing by defining in front of main() or any other
function and as blank arguments within functional brackets.

For example –

1. void main()

2. void main(void)
B. Derived Data Types
Derived data types are nothing but primary data types –

1. Arrays

2. Pointers
1. Array Data Type
An array is a collection of items stored at continuous memory
locations.

e.g. int a[4]={10,20,30,40};

Index/Position a[0] a[1] a[2] a[3]


Values 10 20 30 40

1000 1002 1004 1006


Address in memory
2. Pointer Data Type
Pointers are symbolic representation of addresses.

e.g. int a=10, *ptr;


ptr=&a;
address 1000 1002
a = 10 ptr = 1000

Variables and values


C. User-Defined Data Types
A user-defined data type (UDT) is a data type that
derived from an existing data type. But they have their
own keywords too along with the use of basic data types.
1. structure
2. union
3. enum
4. typedef
1. Structure Data Type
• It is a package of variables of different types under a single
name.
• "struct" keyword is used to define a structure.
• The total bytes of data types occupy the memory.

e.g. struct student


{
int rollno,
char name[10];
float per;
}std;
Structure student will occupy 2+1+4=7bytes in memory.
2. Union Data Type
• It is a package of variables of different types under a single
name.
• “union" keyword is used to define a structure.
• The highest bytes of data types will occupy the memory.

e.g. union student


{
int rollno,
char name[10];
float per;
}std;
Structure student will occupy 4 bytes in memory as float is the
highest data type among int and char.
3. Enumeration Data Type
Enumeration is a special data type that consists of integral constants,
and each of them is assigned with a specific name. "enum" keyword is
used to define the enumerated data type.

State 0 State 1 State 2

1. Declaration - enum week{Mon, Tue, Wed};


2. Instantiation - enum week day; Object of enum week
or
Declaration and Instantiation - enum week{Mon, Tue, Wed}day;
3. Operation – day=Tue;
output will be 1.
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
void main()
{
enum week day;
day = Tue;
printf("%d",day);
}

output - 1
4. Typedef Data Type
You can define explicitly new data type names by using the
keyword typedef. Using typedef does not actually create a new
data class, rather it defines a name for an existing type.

void main()
{
typedef int suman;
suman a,b; //a and b are of int data type
-----
}
Now, suman will act as int data type.
Thank You

Das könnte Ihnen auch gefallen