Data Types
Data types are used to define the type of data in the programs. Each language has its set of data types with which it stores different types of data variables. Data types are very important in C programming where they help write reliable and bug-free codes.
Types of data in C:
1. Basic Data Types
2. Derived Data Types
1. Basic Data Types
Basic value types, such as integers, characters, etc., are also available in C.
These data types can be categorized into the following:
a. Integer Data Types
b. Floating-Point Data Types
c. Character Data Type
d. Void Data Type.
Now let’s delve into each of them separately.
a. Integer Data Types:
- Whole number is stored in integer data types. This makes the storage capacity of various integer data types in C variable. Here are some common ones:
• int: Four bytes for signed integers.
• short: Two bytes typically used for small integers.
• long: Used mainly for bigger integers; four or eight bytes.
• long: For very large integers, this uses 8 bytes. - Signed: for signed integers either positive or negative.
- Unsigned: for Unsigned integers, like only positive values.
Example:
int age = 25;
b. Floating-Point Data Types
Decimal numbers are stored in floating-point data types. C provides two primary floating-point data types:
• float: Used for single-precision floating-point numbers; typically 4 bytes.
• double: Used with 8 bytes, each being of a double-precision floating-point number.
Example:
double pi = 3.14159265359;
c. Character Data Type
The storage size for one character using the character data type ‘char’ is one byte. It usually takes up only one byte in memory.
Example:
char grade = ‘A’;
d. Void Data Type
And void does not have a storage value. It is used to show that no value is returned by a function instead.
Example:
void printHello(){
printf(“Hello, World!\n”);
}
C program for finding Size and Range of Each data type using Sizeof() and processor commands from IO.
#include <stdio.h>
#include<conio.h>
#include <limits.h>
void main()
{
short a; long b; long long c; long double d; int e; char f;
printf(“size of short = %lu bytes\n”, sizeof(a));
printf(“Range of signed short int \n%d to %d\n”, SHRT_MIN, SHRT_MAX);
printf(“Range of unsigned short int \n0 to %d\n\n”, USHRT_MAX);
printf(“size of int = %ld bytes\n”, sizeof(e));
printf(“Range of signed int \n%d to %d”, INT_MIN, INT_MAX);
printf(“\nRange of unsigned int \n0 to %u\n\n”, UINT_MAX);
printf(“\n size of long = %ld bytes\n”, sizeof(b));
printf(“Range of signed long int \n%ld to %ld\n”, LONG_MIN, LONG_MAX);
printf(“Range of unsigned long int \n0 to %lu\n\n”, ULONG_MAX);
printf(“\n size of char = %ld bytes\n”, sizeof(f));
printf(“Range of signed char \n%d to %d\n”, SCHAR_MIN, SCHAR_MAX);
printf(“Range of unsigned char \n0 to %d\n\n”, UCHAR_MAX);
printf(“\n size of long long = %ld bytes\n”, sizeof(c));
printf(“\n size of long double= %ld bytes\n”, sizeof(d));
}
Output
size of short = 2 bytes
Range of signed short int -32768 to 32767
Range of unsigned short int 0 to 65535
size of int = 4 bytes
Range of signed int -2147483648 to 2147483647
Range of unsigned int 0 to 4294967295
size of long = 8 bytes
Range of signed long int
-9223372036854775808 to 9223372036854775807
Range of unsigned long int 0 to 18446744073709551615
size of char = 1 bytes
Range of signed char -128 to 127
Range of unsigned char 0 to 255
size of long long = 8 bytes
size of long double= 16 bytes
2. Derived Data Types
Basic data types are combined or modified to make derived data types in C. Some common derived data types include:
a. Array: A group of similar data values stored in sequential memory.
b. Pointer: It is an address of another variable.
c. Structure: A group of different data values stored in sequential memory, grouped into a single user-defined data type.
d. Union: Like structure, but with overlapping memory storage.
e. Enumeration: used to create a user-defined datatype.
struct Student {
char name[50];
int age;
float GPA;
};