Data Types in C Language with example Programs

The C language has a rich set of data types to handle different kind of data entered by programmer. Basically a data type informs compiler about how much memory a declared variable required to store input. Data types are used extensively for declaring variables and functions of different types.

Data Types in C
Data Types in C

There are three classes of data types:

  1. Primary Data types
  2. Derived Data types
  3. User Defined Data types

Primary Data Types

Primary data types are those which are already defined in programming languages also known in-built data types. These data types are basic building blocks of any programming language and numerous composite data types are constructed from this basic type.

We can check the memory size of any variable by using sizeof() operator and a format specifier %lu.

Primary data Types include:

  1. Characters
  2. Integers
  3. Floating point
  4. Void

Note: All memory size mentioned are of 32 bit system.

Character Types:

A single character in C is of “char” type data. Characters stored in 8 bits or 1 byte of memory and the character can also be signed and unsigned. Syntax.

There’s no dedicated “character type” in C language. char is same as integer type. char is just a smallest integer type. So, Like any other integer type, it can be signed or unsigned.

char	        1 byte	   −128 to 127        %c
signed char	    1 byte	   −128 to 127        %c
unsigned char	1 byte	      0 to 255        %c
//Example
char ch=’a’;

printf(“size of character %lu”,sizeof(ch));

Signed and unsigned char both used to store single character as per their ASCII values. For example, ‘A’ will be stored as 65. we don’t need to specify keyword ‘signed’ for using signed char.

//Both are same
signed char name = ‘a’;
char name = ‘a’;

but keyword ‘unsigned’ for using unsigned char must be used.

unsigned char name = ‘a’;

ASCII table contains 128 character values from 0 to 127 and the remaining 127 characters is known as extended ASCII character set.

In signed character extended set takes value from -128 to -1 whereas in unsigned char it takes value from 128 to 255. And when you try to give value 128 to signed char, it will take -128 value like clockwise.

Similarly, if you give value 256 to unsigned char, it will take value 0.

// signed ASCII characters set

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;

    printf("ASCII values from -128 to 127\n"); 
    for(i=-128; i<=127; i++)
    {
        printf("%d = %c\n", i, i);
    }

printf("ASCII value of +ve 128 same as -128 character %c = %d\n", i, i);
    return 0;
}

For output of example above and below follow this link.

//unsigned ASCII character set

int main()
{
    int i;

    printf("ASCII values from 0 to 255\n");
    for(i=0; i<=255; i++)
    {
        printf("%d\t%c\n", i, i);
    }

printf("ASCII value of 256 same as -128 character %c = %d\n", i, i);
    return 0;
}

Integer Types:

As the name suggests used to hold only integer values. Generally, an integer stores values in range limited from -2147483648 to 2147483647. A signed integer uses one bit for sign out of 16 bits.

The integers classified into three classes of storage with both signed and unsigned qualifier based on the range of values held by them.

Datatype               size 	  Range limit             format specifier
short int                2 	  -32,768 to 32,767 	               %hd
unsigned short int 	     2 	   0 to 65,535 	                       %hu
unsigned int 	         4 	   0 to 4,294,967,295 	               %u
int 	                 4 	   -2,147,483,648 to 2,147,483,647 	   %d
long int 	             4     -2,147,483,648 to 2,147,483,647 	   %ld
unsigned long int        4 	   0 to 4,294,967,295 	               %lu
long long int 	         8 	   -(2^63) to (2^63)-1 	               %lld
unsigned long long int   8 	   0 to 18,446,744,073,709,551,615 	   %llu
//Check size of data types
int main(){

printf("long int size               = %d\n",sizeof(long int));
printf("short int size              = %d\n",sizeof(short int));
printf("unsigned short int size     = %d\n",sizeof(unsigned short int));
printf("long int size               = %d\n",sizeof(long int));
printf("unsigned long long int size = %d\n",sizeof(unsigned long long int));

return 0;
}

Output:

long int size               = 4
short int size              = 2
unsigned short int size     = 2
long int size               = 4
unsigned long long int size = 8

if you assign more or less that their limit range it follow clockwise rule as mention in above char sets.

int main()
{
short int a = -32769;
unsigned short int a1 = 65536;

printf("%hd %hd",a,a1);

return 0;
}

Output:

32767    0

Floating Point Types:

Data type ‘float’ used to stores real numbers in 32 bits with up to 6 decimal points precision. The double type also similar to float stores in 64 bits with more accurate precision up to 14 decimals and Long double further extends the precision using 80 bits and 19 decimal places.

Datatypes 	     Size   	 Range Limit	           Format Specifier
float 	          4 	  1.2E-38 to 3.4E+38	           %f
double 	          8 	  2.3E-308 to 1.7E+308	           %lf
long double 	  12 	  3.4E-4932 to 1.1E+4932	       %Lf
int main()
{
float a = 1.123456789;
double b = 1.01234567894561234;
long double c = 5.4e-5;

printf("%f %lf \n %lf",a,b,c);
return 0;
}

Output:

1.123457 1.012346
-27695223254550883000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000.000000

Void Types

Void is considered a data type (for organizational purposes) and usually specifies function return type. Basically a ‘void’ keyword used as a place holder where you would put a data type to represent “NO DATA”.

Void plays an important role of generic type program to achieve run-time polymorphism a bit similar to OOPs languages.

Function with no return value and no arguments.

void function(void);

A normal variable cannot be declared as void directly, Whereas a pointer can be void.

void bad_variable;  //this line generate error
void* vague_pointer;  
// void pointer without specifying which data type it is pointing to

User-Defined Data Types:

Data type that derived from an existing data type are called user-defined data type (UDT). You can create customized data type to extend the built-in types that already available.

typedef and enum used to creates user defined data types. Advantage of user defined data type is that it increases the program readability.

‘typedef’

‘typedef’ allows users to define the identifier which would represent an existing data type. This defined data type can be used to declare variables.

typedef int INTEGER;

int main()
{
INTEGER num1 = 1, num2 = 2; // num1 and num2 are declared as ‘int’

printf("%d %d",num1,num2);


return 0;
}

Output:

1 2

‘enum’

“enum” is the keyword and “day” is the user defined data type that is used to declare the variables within the curly braces, mainly used to assign names to integral parts. We can also explicitly assign the enumeration constants.

enum day {monday, tuesday, wednesday, thursday, friday, saturday};

enum State {Working = 1, Failed = 0}; // explicit assignment

int main()
{
enum day today;
today = friday;

printf("Today is day %d \n",today);

printf("%d, %d, ", Working, Failed);

return 0;
}

Output:

Today is day 4
1, 0,

Derived Data Type:

Data types derived from primitive data types are called derived data types. Derived data types used to add some functionality to the basic data types as per program requirement.

Derived Data Type are formed by a grouping of two or more primary types. DDT have extended the scope of C language.

Following are the Derived data types.

  1. Pointers
  2. Arrays
  3. Union
  4. Structures.

Pointers

Pointer is a powerful feature that C language support. It provide user an ability to access memory and address of variable, functions etc. Pointers can reference any data type (basic or derived).

int main(){
int i=10;
int *j;        // integer pointer
j=&i;      // i address assigned to j

printf("%d %p",i,j);  // %p is a format specifier for pointer
// OUTPUT will be different for you
return 0;
}

Output:

10 0060FF08

Arrays:

An array is a collection of data or finite ordered collection of homogeneous data, stored in contiguous memory locations. Arrays are also referred as structured data types.

It can often be defined as a collection of variables of same type. Array is defined using square brackets which defines array size.

int main(){

int arr1[10] = {1,2,3,4,5,6,7,8,9,10};

printf("Arrays values\t memory Address\n");

for(int i=0;i<10;i++)
     printf("       %d\t %p\n",arr1[i],&arr1[i]);

return 0;
}

Output:

Arrays values    memory Address
       1         0060FEE4
       2         0060FEE8
       3         0060FEEC
       4         0060FEF0
       5         0060FEF4
       6         0060FEF8
       7         0060FEFC
       8         0060FF00
       9         0060FF04
       10        0060FF08

Note that the size of the array cannot be negative.

Structures

Structure datatype in C language allows us to group together relative data of different types together. Keyword “struct” used followed by structure tag (name) to define the structure.

Closing bracket in the structure type declaration must be followed by a semicolon.

struct student{
  	char name[10];
  	int R_no;
  	float percent;
};

int main(){

struct student s1;

printf("Enter name, roll number and percentage of student\n");
scanf("%s %d %f",s1.name,&s1.R_no,&s1.percent);

printf("\n***Student details***\n");

printf("Name= %s\nRoll no.= %d\nPercentage= %f",s1.name,s1.R_no,s1.percent);


return 0;
}

Output:

Enter name, roll number and percentage of student
Externcode 1234 98.9

***Student details***
Name= Externcode
Roll no.= 1234
Percentage= 98.900002

Unions

Union stores different data types in the same memory location with only one member assigned memory and value at any given time. The memory union get assigned is the largest of all members the union contain.

union example{
  	int X;
  	float Y;
  	long double Z;
};

int main(){

union example Data;

printf("size of union is of LONG DOUBLE = %u\n",sizeof(Data));

Data.X=100;  // 'X' got memory and value
Data.Y=98.99;  // 'X' cleared & memory freed for 'Y'

printf("Now 'X contain garbage value = %d \n",Data.X);
printf("Y = %lf",Data.Y);

return 0;
}

Output:

size of union is of LONG DOUBLE = 12
Now 'X contain garbage value = 1120271073
Y = 98.989998
avatar
  Subscribe  
Notify of