You are on page 1of 11

Data Type :

A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

Enumerated Data Types


Enumerated data types are user-defined data types that consist of integer values. They are used to
define variables that can only assign certain discrete integer values in the program. They are used
to make a program more readable, flexible, and maintainable. We use the keyword ‘enum’ to
declare new enumeration types in the C programming language.

Enum syntax:

enum flag {const1, const2, const3………};

A popular example of enumerated data types is the days of the week.

Example – Program to illustrate Enumeration

#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Fri;
printf("%d",day);
return 0;
}

Void
The void is just an empty data type that depicts that no value is available. Typically, the void is
used for functions. When we declare a function as void, it doesn’t have to return anything.

Void is used in three situations:

 Function returns as void – A function with no return value will have the return type as
void.
 Function arguments as void – A function with no parameter can accept the void.
 Pointers to void – It represents the address of an object, but not its type.
Data Types and Their Ranges in C
Data types in C (or in any programming language), are very important to learn and understand before
you start to write a program.They are needed to store different data types like integers, characters,
decimals, strings or even user-defined.

Why we need data types in C


We know that computers store all the data in the form of binary numbers, and it assigns
memory to each of them. Now suppose you want to make a program to store your name, age
and phone number. Without mentioning the data types, your computer would not be able to
distinguish between your name, age and phone number and will treat them equally by
assigning the same memory and keeping them in the same set of variables.

The age consists of the utmost 2 to 3 digits and the phone number consists of at least 10
digits, but computers will assign the same memory to both of them that will lead to a lot of
memory wastage.

To deal with such scenarios, we assign data types to each variable to prevent any confusion
or memory wastage.

What are data types in C

Data type is an attribute of data which tells the C compiler, which type of data a variable is
holding.

It can be of type integer, float( decimal), character , boolean( true/false ) etc. Formally we use
data types to specify the type of data our variables are holding.
Broadly there are two types of data types in C:

a. Primary Data types or Basic Data Types b. Secondary Data Types

A. Primary Data Types or Basic Data types


These are the most basic data types and all the other data typed are derived or made from
them only. It contains integer, floating point and char.

Four main types of primary/basic data types are:

1. Integer
2. Float
3. Char
4. Void

Now, these are further classified as short, long,double, long double, signed and unsigned data
types in C.

Before discussing this, let us first understand what short, long, signed and unsigned data
types in C means.
SHORT AND LONG

These are used to define the amount of memory space that the compiler will allocate. In case
of short int, it is typically 2 bytes and in long int, it is 4 bytes.

SIGNED AND UNSIGNED

This also deals with memory allocation only but in a different way. In case of signed int, it
takes into account both negative and positive numbers. But in unsigned int, we can only
represent positive numbers. Now since the range of values gets decreased in unsigned, thus it
is able to represent higher values in the same memory prospect.

Let us understand it through an example, Integer data type consists of 2-byte memory.

1 Byte= 8 bits 2 Byte= 16 bits

The number of digits = 2^(2 Byte)= 2^(16 bits)= 65536

In the case of unsigned int data type, there are only positive numbers and since zero is also
included in positive numbers therefore unsigned int can take values from 0 to 65535.

Now let us consider the case of signed data type, it has both positive and negative numbers.
Thus the total present memory length gets divided into two equal halves (65536/2= 32768),
one half to represent positive numbers ( 0 to 32767 )and other half to represent negative
numbers (-1 to -32768).

This is applicable to all other types of data types like float char etc. Now similarly, you can
find the range of values of all data types like float, char, double etc.

LONG AND LONG DOUBLE

These are mostly used with decimal numbers. By using these prefixes, we can increase the
range of values represented in float. Float is of 4 bytes, double is of 8 bytes and long double
is of 10 bytes.

By using the relation mentioned above(int data type), we can calculate the length of the
number in float and decimal.

For example, float takes 4 bytes, that is, 32 bits(4*8)

Therefore the length of number will be: 2^(32)= 4,29,49,67,296.

In case of double, it takes 8 bytes, 64 bits(8*8)

Therefore the length of number will be: 2^(64)= 1,84,46,74,40,73,70,95,51,616.


1. CHAR DATA TYPE IN C

It is used to store a single character and requires 1 byte. A character could be any alphabet,
number or special character written inside a pair of single inverted commas, eg ‘1’, ‘a’, ‘#’
etc. Since it requires 1 Byte, which is 8 bits, the number of characters in C language is
256(2^8). Memory space differs with use of prefixes.

#include <stdio.h>
void main() {
char c;
c = 'a' ;
printf("%c",c);
}
Output
a
2. INT DATA TYPE IN C
It is used to store integer values and requires memory according to the value of the integer we
want to store. The size of int is compiler dependent. For example, 32-bit compilers have int
as 4 bytes but 64 bits compilers(which we are using now) have int as 8 bytes. And since the
size varies the range of values integers can store also varies from compiler to compiler.
For 2 bytes, it’s 0 to 65,535 for an unsigned integer.
For 4 bytes, it’s 0 to 4,29,49,67,296 for an unsigned integer.
#include <stdio.h>
void main() {
int i;
i = 10;

printf("%d",i);
}

Output
10
3. FLOAT

Floating point numbers are used to store decimal numbers. The range of values it can store
also varies from compiler to compiler. For 32-bit and 64-bit compilers, it is the same as 4
bytes. That is 2^(4*8) length of value, which is 4,29,49,67,296 i.e. 0 to 4,29,49,67,296
numbers can be represented using float.

#include <stdio.h>
void main() {
float f;
f = 1.20 ;
printf("%f",f);
}
Output
1.200000

Size of various data types in my computer(64-bit). Using a similar program, you can find the
size in your own compiler. (You do this exercise to clear all your doubts regarding size!!)

#include <stdio.h>
int main() {
printf("short int is %2d bytes \n", sizeof(short int));
printf("int is %2d bytes \n", sizeof(int));
printf("int * is %2d bytes \n", sizeof(int *));
printf("long int is %2d bytes \n", sizeof(long int));
printf("long int * is %2d bytes \n", sizeof(long int *));
printf("signed int is %2d bytes \n", sizeof(signed int));
printf("unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n");
printf("float is %2d bytes \n", sizeof(float));
printf("float * is %2d bytes \n", sizeof(float *));
printf("double is %2d bytes \n", sizeof(double));
printf("double * is %2d bytes \n", sizeof(double *));
printf("long double is %2d bytes \n", sizeof(long double));
printf("\n");
printf("signed char is %2d bytes \n", sizeof(signed char));
printf("char is %2d bytes \n", sizeof(char));
printf("char * is %2d bytes \n", sizeof(char *));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
return 0;
}
Output:
short int is 2 bytes
int is 4 bytes
int * is 8 bytes
long int is 8 bytes
long int * is 8 bytes
signed int is 4 bytes
unsigned int is 4 bytes

float is 4 bytes
float * is 8 bytes
double is 8 bytes
double * is 8 bytes
long double is 16 bytes

signed char is 1 bytes


char is 1 bytes
char * is 8 bytes
unsigned char is 1 bytes
4. VOID

It is a special type known as empty data type that is used to state that a given variable does
not have any type. This is mainly used in defining functions where we do not want to return
any value.

Following is the data of data type size based on a 32-bit compiler and 64-bit compiler

B. Secondary Data Types


Secondary data types are formed by combining two or more primary data types in C.

They are mainly of two types:

1. USER-DEFINED DATA TYPES


2. DERIVED DATA TYPE
1. USER-DEFINED DATA TYPES IN C

These data types are defined by the user as per their convenience. If a user feels a need of
having a data type which is not predefined in C library, then they make their own.

1. STRUCTURE

Structure is a user-defined data type in C, where we can store values of multiple data types.

For example, if you want to store details of students of a college, where each student will be
having a name, roll no and marks. But managing the data of one student together (string, int
and float variable) for a student is not possible with any of the data types we have discussed
so far. Now for this we use another data type known as Structure.

Let's say I create a structure of students with the fields such as name, roll no and marks. Now
for each student I can create a variable of structure which will store data of that particular
student. And I can access it whenever I want.

#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int roll_no;
float marks;
};

int main() {
struct Student student1;
strcpy(student1.name, "Ankush");
student1.roll_no = 20;
student1.marks = 99.99;

return 0;
}

2. UNION

Union is quite similar to structure as it is also used to store values of multiple data types.

#include <stdio.h>
#include <string.h>
union Emp {
float F;
char X;
}e;

int main() {
union Emp e;
e.F = 2.0;
e.X = 'a';

return 0;
}
The only difference between structure and union is that, in union the space allocation is equal
to the highest memory required by the data type.

Suppose we have two variables, one of type float(4 bytes) and one of type char(1 byte). Now
struct will require memory of 5 bytes (4 bytes of float in 1 byte of char) but union will require
4 bytes (4 bytes of float which is maximum among all variables).

3. TYPEDEF

It is a keyword present in C, which is used to give a new name to any data type.

For example, sometimes typing whole data type name can be tiring, now you can give a short
name using typedef i.e, if you want to write unsigned long int in short form as INT, then you
can use typedef as:

typedef unsigned long int ul; ul i1, i2;

4. ENUM

Enum is a user-defined data type, which is used to make a program more readable and
understandable. It is used to assign text values to integer values.It basically uses an indexing
method and assigns text values to the concerned index value.

In the below given figure 0 will be assigned to Jan, 1 to Feb, 3 to Apr and so on.

#include<stdio.h>
enum Months{Jan,Feb,Mar,April,May, June, July};

int main() {
enum Months month;
month = April;
printf("%d",month);
return 0;
}
OUTPUT:
3
2. DERIVED DATA TYPES

Derived data types are data types which are formed by combining one or more primitive data
types or basic data types in C. For example, there might be some cases where primitive data
types are not sufficient for us. Like if we want to store a phone number we can use an integer
but what if we want to store phone numbers of all the students in a college. Making variables
for each of them is not the optimal way.

To deal with such situations optimally, C has some derived data types, which we can use as
per our convenience.

1. ARRAY
2. STRING
3. POINTER
4. FUNCTIONS
1. ARRAY

Array is a derived data type which is used to store the data of the same type in a contiguous
memory location. We can store any type of data types ranging from int , float, double, char to
pointer, string, and structure. That is we can make an array of primitive, user-defined or of
derived data types.

Array provides random access that is, we can get the value of any element in the array using
its index value. In the image shown above, you can see how the elements are stored in an
array( in a contiguous manner) and how the indexing is done( starts with 0 not 1). That is, the
9th element is accessed using index 8.

For example, if you want to store all of your marks. You can use an integer array. And then,
you can get access to your marks by simply using its index. Let us understand it through
code. (If you will carefully observe the code, the element at ith index is accessed using arr[i])

#include <stdio.h>
int main() {

int x = 0, num[4];

for (x = 0; x < 4; x++) {


num[x] = (x + 1) * x;
}

for (x = 0; x < 4; x++) {


printf("%d \n", num[x]);
}

return 0;
}
Output
0
2
6
12
We can also make an array of structures.
#include <stdio.h>
#include <string.h>
struct student {
int rollno;
char name[10];
};

int main() {
int i;
struct student st[2];
printf("Enter Records of 2 students");

for(i = 0; i < 2; i++) {


printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i = 0; i < 2; i++) {
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}

return 0;
}
Input
Enter Records of 2 students
Enter Rollno:1
Enter Name:Ankush
Enter Rollno:2
Enter Name:Shreyas
Output
Student Information List:
Rollno:1, Name:Ankush
Rollno:2, Name:Shreyas
2. STRING

String is an array of characters. The difference between a normal character array and a string
is that string contains ‘\0’ value at the end, indicating that this is the end of the string, while a
simple character array does not need to have this.

‘\0’ is a special character, which is known as the NULL character and it is used to indicate
the termination of a string.

In the image shown above, you can see how the characters are stored internally in a string.
Yes, it follows the indexing property of an array, therefore, string is also known as an array of
characters. Observe the ‘\0’ (NULL) at the end of the string. It signifies the end of a string.

#include <stdio.h>
int main() {
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};\
char str2[] = "Hello";
printf("Say: %s\n", str1 );
printf("Say: %s\n", str2 );
return 0;
}
Output
Say: Hello
Say: Hello

We can traverse each character of a string using its length or by using ‘\0’. Let us see how:

#include <stdio.h>

void main() {
char hey[6] = "hello";
int i = 0;

while (i < 5) {
printf("The %dth character is %c\n" ,i+1,hey[i]);
i++;
}
printf("\n");
char bye[7] = "GoodBye";
int j = 0;
while (bye[j]!=NULL) {
/* This will not work if there is no ‘\0’ at the end of string */
printf("The %dth character is %c\n", j+1,bye[j]);
j++;
}

Output
The 1th character is h
The 2th character is e
The 3th character is l
The 4th character is l
The 5th character is o

The 1th character is G


The 2th character is o
The 3th character is o
The 4th character is d
The 5th character is B
The 6th character is y
The 7th character is e

You might also like