Data Types in C

Data Types in C Language with Examples

In this article, I am going to discuss Data Types in C Language with examples. Please read our previous article, where we discussed the Variables in the C Program. As part of this article, you will learn what are Data types in C, their type, and when and how to use Data Types in C Program with examples.

Data types are used to store data temporarily in the computer through the program. In the real world, we have different types of data like integer, floating-point, character, string, etc. To store all these types of data in the program to perform business required calculations and validations we need the concept data types.

How to Declare a Variable in C?

In the declaration of every variable, it is mandatory to specify its data type. If you just look at the syntax of the variable you will understand, the variable declaration is consisting of two parts i.e. the data type which is important followed by the identifier which is as follows.

Syntax: DataType Identifier;
Example: int a;

In the above variable declaration, a is the identifier and int is the data type.

What is a Data Type in C Language?

It is just a representation of data. That means how much memory is required to be allocated and what type of data is allowed to store. These two are represented by the data type in any programming language. Suppose I am declaring a variable of type integer

int a;

Here ‘a’ gets memory allocation at some location. We have already discussed; the memory address is a positive integer. So, every variable gets memory allocation and the integer variable occupies 2 bytes of memory. So, once the variable is ready, what type of data is allowed? Suppose, here, I am storing -15 which is a negative integer number, so it is allowed. Next, if we store 17 which is a positive integer, then also it is allowed in C language. But can we store 12.34? The answer is No. Decimal values are not allowed. For better understanding, please have a look at the following diagram.

What is a Data Type in C Language?

Simply datatype represents two things about a variable.

  1. What type of data is allowed to store?
  2. How much memory is required to store the data?

These two things are described by a data type.

How many types of data types are available in the C language?

Data types in C Language are classified into three types as follows.

  1. Primitive Data Types: Integer, character, float, void. All these are called primitive data types.
  2. Derived Data Types: Array, String, Pointer, etc. come under derived data types.
  3. User-Defined Data Types: Structure, union, typedef, enum, etc. are comes under user-defined data types.

For better understanding, please have a look at the below diagram which shows a high-level classification of C Language data types.

How many types of data types are available in the C language?

Note: We will discuss Derived and User-defined Data Types later as Array, Function, Pointer, Structure, and Union, etc. are separate concepts in C Language. Here, in this article, we just going to keep the focus on the Primitive Data Types.

Classification of Primitive Data Types in C Language:

As we already discussed the Primitive Data Types are classified into four types are as follows.

  1. Integer
  2. Character
  3. Float
  4. Void
Integer Data Type in C Language:

Again, Integer is divided into three types are as follows.

  1. Short
  2. Int
  3. Long

Again, the short data type is divided into two types i.e. signed short and unsigned short. Same for int and long i.e. signed int, unsigned int, signed long, and unsigned long. So, one integer data type is again subdivided into 6 types. For a better understanding of the integer data types, please have a look at the below image.

Integer Data Type in C Language

Why integer data type is classified into six types?

The basic advantages of classifying these many types of nothing but utilizing the memory more efficiently and increasing the performance. If you want to store one integer value, one integer data type is enough. It depends on the data and also depends on the size of the data. For example, department number is something like 10, 20, and 30, etc. To store such type of data, a very little integer is enough, so we can consider short data type. Consider I am trying to store a phone number or I am trying to store an account number, such type of things we can store with the help of long integer type.

Depending on the size of the data, we choose a particular datatype. Suppose to store the value 10, 1 Byte memory is required. then we should go for a data type in which it occupies only one byte of memory. In this case, if you are trying to choose a data type that occupies 4 bytes, then you are wasting 3 bytes of memory in your application. 3 bytes of memory we are wasting which will reduce the performance of the application.

Character Data Type in C Language:

The Character Data Type in C language is divided into two types. One is a signed character and the second one is an unsigned character. Both are of size 1 byte. For a better understanding of the Character data types, please have a look at the following image.

Character Data Type in C Language

Float Data Type in C Language

The Float Data Type in C language is divided into three types one is float type, the second one is double and the last one is long double. Float is of size 4 bytes; double is of size 8 bytes and long double is of size 10 byte. For better understanding, please have a look at the following diagram.

Float Data Type in C Language

This is called sub-classification of Primitive data types. For better understanding, please have a look at the below image which describes the classification of Primitive Datatypes.

Classification of Primitive Data Types in C Language

Which data Types take how many bytes of Memory in C Language?

If it is a short data type, either it is signed or unsigned, it occupies two bytes of memory. If it is an integer data type, either signed or unsigned, it will occupy two or four bytes of memory depending upon the compiler you are using. For long data type, either signed or unsigned it occupies 4 bytes of memory. For character data type, for signed and unsigned it will take 1 byte of memory. The float data type will take 4 bytes of memory; double will take 8 bytes of memory and long double will occupy 10 bytes of memory.

What is assigned and unsigned in c Language?

What is assigned and unsigned in c Language?

Using signed data type, we can store both positive or negative values. In our program, we are not always working with only positive values. Sometimes requirements will be there to store negative value. In that situation, we should go for the signed type because the signed data type will accept both positive and negative values. But if it is an unsigned type, the unsigned type strictly accepts only positive values. Negative values will not be accepted. This is the difference between signed datatype and unsigned datatype.

Integer Data Type in C

Int is used to define integer numbers. The size of the data type ‘int’ is 2 bytes or 16 bits. The minimum value for the signed ‘int’ data type is -32768. The maximum value for the signed ‘int’ data type is 32767. We can declare int data type as follows:

int c;
c=5;

Float Data Type in C

Float is used to define floating-point numbers. The size of the data type ‘float’ is 4 bytes or 32 bits. The minimum and maximum values for the ‘float’ data type are 3.4E-38 to 3.4E+38. We can declare a float data type as follows:

float Miles;
Miles=5.6;

Double Data Type in C

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. The size of the data type double is 4 bytes or 32 bits. The minimum and maximum values for the ‘double’ data type are 1.7E-308 to 1.7E+308. We can declare a double data type as follows:

double a;
a=2500;

Char Data Type in C

Char defines characters. The size of the data type char is 1 byte or 8 bits. The minimum and maximum values for the ‘char’ data type are -128 to 127. We can declare char data type as follows:

char Name;
Name=’x’;

Example to demonstrate the Built-in Data Types in C Language
#include <stdio.h>
int main()
{
    int a = 4000; 		// positive integer data type
    float b = 5.2324; 		// float data type
    char c = 'Z'; 		// char data type
    long d = 41657; 		// long positive integer data type
    long e = -21556; 		// long -ve integer data type
    int f = -185; 		// -ve integer data type
    short g = 130; 		// short +ve integer data type
    short h = -130; 		// short -ve integer data type
    double i = 4.1234567890; 	// double float data type
    float j = -3.55; 		// float data type
}
Modifiers in C Programming Language:

The amount of memory space to be allocated for a variable is derived by modifiers. Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.

For example storage space for the int data type is 4 bytes for a 32-bit processor. We can increase the range by using long int which is 8 bytes. We can decrease the range by using short int which is 2 bytes.

There are 5 modifiers available in the C Programming language. They are,

  1. Short
  2. Long
  3. Signed
  4. Unsigned
  5. long

Modifiers in C Programming Language

Derived Data Types in C Language:

Derived data types in C Programming Language are those C data types which are derived from the fundamental data types using some declaration operators. The basic derived types that are available in C are:

  1. Array.
  2. Functions
  3. Pointers.
  4. Structures.
  5. Classes.
Array Derived Data Type in C

The arrays can be defined as a set of finite and homogeneous data elements. Each element of an array is referenced using an index.
Example: If the name of an array is A which has 4 elements then the array will be represented as A[0], A[1], A[2], A[3]. Here, these subscripts which are containing the digit are known as an index. Learn Arrays in C with Real-time Examples.

Function Derived Data Type in C

The function can be defined as a part of the program which is declared by the programmer in any part of the program and a function can be of any name depending upon the choice of the programmer. The function declared can be invoked from other parts of the program. Learn Functions in C with Real-time Examples.

Pointer Derived Data Type in C

A pointer is a variable that holds the address of the memory space. If one variable can hold the address of another variable then it is said that the first variable is pointing to the second. Learn Pointers in C with Real-time Examples.

Structure Derived Data Type in C

The structure can be defined as a collection or a group of variables that are referenced under one name. It is used to keep related information together. We use a ‘struct’ keyword to construct a structure. Learn Structure in C with Real-time Examples.

Enumeration Data Type

Enumeration data type consists of named integer constants as a list. It starts with 0 (zero) by default and the value is incremented by 1 for the sequential identifiers in the list. Enum data type is a user-defined data type having a finite set of enumeration constants. The keyword ‘enum’ is used to create an enumerated data type.

Syntax: enum [data type] {const1, const2…., const n};

Example to Understand Enum in C:
#include <stdio.h>
int main()
{
   enum MONTH 
   {
      Jan = 0, Feb, Mar 
   };
   enum MONTH month = Mar;
   if(month == 0)
   printf("Value of Jan");
   else if(month == 1)
   printf("Month is Feb");
   if(month == 2)
   printf("Month is Mar");
}
Output:

Example to Understand Enum in C

Void Data Type in C Language:

The void is an empty data type that has no value and no operations. This can be used in functions and pointers. It’s a data type that represents the lack of a data type. Many programming languages need a data type to define the lack of return value to indicate that nothing is being returned.

Example: void f(void);

void f(); (accepts a constant but unknown number of arguments)

Uses of Void Data type in C:

When used as a function return type: the void keyword specifies that the function does not return a value.

void show()
{
      printf("This function has no return type");
}

When used for a function’s parameter list: void specifies that the function takes no parameters.

int sum(void)
{
      int a,b;
      printf("Enter Two number>> ");
      scanf("%d%d",&a,&b);
      return a+b;
}

When used in the declaration of a pointer: void specifies that the pointer is “universal.”

void main()
{
     void *p;
     int a=10;
     char b='A';
     float c=9.19;
     p=&a;
     printf("\nPrinting Integer data %d",(*(int *)p));
     p=&b;
     printf("\nPrinting character data %c",(*(char *)p));
     p=&c;
     printf("\nPrinting float data %f",(*(float *)p));
}
When to use which Data Types in C Language?
  • In implementation when we are required character operations then go for ‘char’ or ‘unsigned char’ data type.
  • For normal numeric operations go for the “int” data type. If there is no -ve representation then go for “unsigned int” data type like employee salary.
  • In implementation when we required the numeric values for the range of -128 to 127 then go for “char” data type in place of creating an “int” in this case we are required to use the %d format specifier.
  • In implementation when we required the numeric values from the range of “0 to 255” then go for an unsigned char data type in place of creating unsigned int. In this case, we are required to use the ”%u” format-specifier.
  • Signed, unsigned, short and long are called qualifiers and we need to apply these qualifiers to an integral type only i.e. we cannot apply to float, double, and long double type.
  • Signed, unsigned is called signed qualifiers.
  • Short and long are called size qualifiers.
  • By default any integral variable size is short and the sign is signed type.

In the next article, I am going to discuss Integer Data Types in C Language with examples. Here, in this article, I try to explain Data Types in C Language with examples and I hope you enjoy this Data Types in C Language article. I would like to have your feedback. Please post your feedback, question, or comments about these Data Types in the C Language article.

Leave a Reply

Your email address will not be published. Required fields are marked *