Data Types in C

Data Types in C Language with Examples

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

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

How to Declare a Variable in C?

In the declaration of every variable, it is mandatory to specify its data type. If you look at the variable syntax, you will understand that the variable declaration consists of two parts, i.e., the data type, which is followed by the identifier, 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 must be allocated and what data type can be stored. The data type represents these two 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 that the memory address is a positive integer. So, every variable gets memory allocation, and the integer variable occupies 2 bytes (or 4 bytes) of memory. So, what type of data is allowed once the variable is ready? Suppose I am storing -15, a negative integer number, so it is allowed. Next, if we store 17, which is a positive integer, it is also allowed in the C language. But can we store 12.34? The answer is No. Decimal values are not allowed. For a 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 be stored?
  2. How much memory is required to store the data?
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., comes under user-defined data types.

For a better understanding, please 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, Union, etc. are separate concepts in C Language. In this article, we will 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 following types.

  1. int: Represents integer values. Size can vary based on the system but is typically 4 bytes on modern systems.
  2. float: Used for single-precision floating-point numbers. It typically requires 4 bytes of memory.
  3. double: Used for double-precision floating-point numbers. It typically requires 8 bytes of memory.
  4. char: Represents single characters and requires 1 byte of memory.
Integer Data Type in C Language:

Again, Integer is divided into three types 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. To better understand the integer data types, please look at the image below.

Integer Data Type in C Language

Why is the integer data type 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 numbers are 10, 20, 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 types 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 we store the value 10, and 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 application’s performance.

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 is an unsigned one. 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: float type, double, and the last one is long double. Float is of size 4 bytes; double is of size 8 bytes; long double is of size 10 bytes. For a 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 a better understanding, please look at the image below, 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. Long data types, either signed or unsigned, occupy 4 bytes of memory. For the character data type, signed and unsigned will take 1 byte of memory. The float data type will take 4 bytes of memory, double will take 8 bytes, and long double will occupy 10 bytes of memory.

What is Signed and Unsigned Data Type in C Language?

What is assigned and unsigned in c Language?

We can store positive and negative values using the signed data type. In our program, we are not always working with only positive values. Sometimes, requirements will be there to store negative values. In that situation, we should go for the signed data 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 a signed datatype and an 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 the 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 the 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 defined 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 a 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 derived from the fundamental data types using some declaration operators. The basic derived types that are available in C are:

  1. Pointers: Used to store the address of other variables.
  2. Arrays: Used to store a fixed-size sequential collection of elements of the same type.
  3. Structures (struct): Used to group different types of variables under the same name.
  4. Unions (union): Similar to structures, but members share the same memory location.
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 four elements, then the array will be represented as A[0], A[1], A[2], and A[3]. Here, these subscripts that contain the digit are known as an index. Learn Arrays 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 points 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 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

The 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. The 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 with no value or 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:
  • The function returns as void.
  • Function arguments as void.
  • Pointers to void.

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 to declare 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 should we use which data types in C language?
  • When we require character operations, go for the ‘char’ or ‘unsigned char’ data type.
  • For normal numeric operations, go for the “int” data type. If there is no -ve representation, go for an “unsigned int” data type like employee salary.
  • When we require the numeric values for the range of -128 to 127, then go for the “char” data type instead of creating an int; in this case, we must use the %d format specifier.
  • When we require the numeric values from the “0 to 255 range, ” go for an unsigned char data type instead of creating an unsigned int; in this case, we must 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 types.
  • 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 Short,

The data type determines the storage/value properties like

  1. Type of value
  2. No.of bytes
  3. Range

We should reserve the memory to store anything on our computer. This memory size and other properties are decided by data type. C language provides three basic/fundamental data types.
1. int
2. float
3. Char

In the next article, I will 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, questions, 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 *