Data Types in C: The Complete Guide in 2022

Every programming languages have their data type. C has integer, float, array, character. Python has an integer, string, float, dictionary, list, set, tuple, etc. Golang has struct, bytes, map, string, integer, etc. JavaScript has an array, object, string, number, weakmap, map,  etc.

In C Language, there are two types of main data types. Primitive Datatypes or Primary Datatypes and Non Primitive datatypes or Derived Datatypes.

Data types specify how we enter the data into our programs and what kind of data we enter. These data types have different storage capacities. In the C programming language, the variables or memory locations should be declared first before being used.

Similarly, the function also needs to be declared before being used. Data types in the C language refer to an extensive system used for declaring variables or functions of the different types. Any variable determines how much space it will occupy in the storage and how the bit pattern stored is interpreted.

Data Types in C

There are mainly two data types in C.

  1. Primitive data types
  2. Non-primitve data types

Data types refer to the type and size of data associated with variables and functions.

Primitive Datatypes in C

Primitive Datatypes are fundamental data types in C language, namely integer(int), floating-point(float), character(char), and void. The primary data types are integer-based and floating-point-based. In addition, a c programming language supports both signed and unsigned literals. The memory size of the primary data type may change according to the 32 or 64-bit operating system.

Let us talk one by one about Primitive Datatype in Detail.

int – Integer data types

An int variable is used to store the integer. Integers are the numbers that can have both positive or negative values except decimal or floating-point values. Example: 0, -5, 10 but not 1.5, 10.5. In C programming, the keyword int is used for declaring integer variables. The storage size of the int data type is 2 or 4 or 8 bytes. 

It varies depending on the CPU processor that we use. Using the 16-bit processor, 2 bytes (16 bit) of memory will be allocated for the int data type. If you want to use an integer value that crosses above the limit, you can go for a long int for which the limits are very high. 

The amount of memory space to be allocated for the variable is derived by the modifiers. Modifiers are prefixed with the essential data types to modify (either increase or decrease) the amount of storage space allocated to the variable. i.e., The storage space for int data type is 4 bytes for the 32-bit processor. 

We can increase this range by using long int, the 8 bytes. We can decrease that range by using the short int, the 2 bytes.

 These are the following various modifiers for C Programming.

Type Storage size Value range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

You can determine the size of an int using the sizeof() function provided by C Language. Let us take an example to determine the sizeof an integer.

#include <stdio.h>

int main()
{
   printf("The size of an integer is: %zu \n", sizeof(int));
   return 0;
}

Now, save the above file as the datatype.c and compile the file using the following command.

gcc datatype.c

It will generate the compiled version called a.out and now run that file using the following command.

./a.out

Now, it will generate the output like this.

The size of an integer is: 4

We can check for other integer types as well.

 

Data Types in C

We can define the integers in the C program using the following syntax.

#include <stdio.h>

int main()
{
  int a;
  printf("The size of an integer is: %zu \n", sizeof(a));
  return 0;
}

We can also define the multiple variables like the following. Here, we have described and declared the variable b. 

#include <stdio.h>

int main()
{
  int a, b=10;
  printf("The size of an integer is: %zu \n", sizeof(a));
  return 0;
}

float – Floating types

It is used to store the decimal numbers (the numbers with floating-point values) with single precision. Floating types are used to store real numbers. The storing capacity of the float data type is 4. This also varies depending upon the processor in the CPU as “int” data type.

We can use up to 6 digits after decimal using float data type. You can declare a floating-point variable in a C language using either float or double keyword. The double is used to store decimal numbers (numbers with floating-point value) with double precision.

Size and range of Integer type on the 16-bit machine.

Type Size(bytes) Range
Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
long double 10 3.4E-4932 to 1.1E+4932
#include <stdio.h>

int main()
{
  int a;
  float b;
  double c;
  printf("Storage size for int data type:%zu \n",sizeof(a));
  printf("Storage size for float data type:%zu \n",sizeof(b));
  printf("Storage size for double data type:%zu\n",sizeof(c));
  return 0;
}

 

C Datatypes Tutorial Example

The main difference between the float and double is that the size of float (single precision float data type) is 4 bytes, and the size of double(double precision float data type) is 8 bytes.

Floating-point variables have the precision of 6 digits, whereas the accuracy of double is 14 digits.

char – Character types

The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. Character types are used to store the value of the character. The storage size of the character data type is 1.

We can store only one character using the character data type. The “char” keyword refers to the character data type. For example, character ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.

#include <stdio.h>

int main()
{
  char appdividend = 'k';
  printf("Storage size for character data type:%zu\n",sizeof(appdividend));
  return 0;
}

Here we have declared the variable appdividend, which has a datatype of char and value k. Now, we want to display the sizeof the character. In theory, it should be 1. So let’s check on the terminal and see the output.

Primitive Data types in C

Size and range of Character type on the 16-bit machine.

Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255

void data type

The void type means no value. It is usually used to specify the type of functions that return nothing. We will get acquainted with this datatype as we start learning more advanced topics in the C language, like functions, pointers, etc.

Derived Datatypes or Non-primitive Datatypes

Derived data types are nothing but the primary datatypes with a little twisted or grouped like the arraystructureunion, and Pointer. The derived type is formed by using one or more basic types in combination.

Using derived types, an infinite variety of new types can be developed. An array and structure types are collectively called the aggregate types. Note that the aggregate types do not include the union types, but a union may contain the aggregate member.

Pointer Datatype in C

The pointer type describes the value representing the address of an object of the stated type. The Pointer is stored as an integral value that references the address of the target object. Pointer types are derived from other data types, called the referenced type of the Pointer. Pointers in C language are the variable that stores/points to the address of another variable. The Pointer is used to allocate memory dynamically. 

Pointer Syntax:

int *p;  char *p;

Where * denotes that “p” is the pointer variable and not the regular variable.

The standard variable stores the value, whereas the pointer variable stores the address of the variable. The content of the C pointer always is the whole number, i.e., address. Always C pointer is initialized to null, i.e., int *p = null.

The null pointer value is 0. The & symbol is used to get the address of the variable. The * symbol is used to get the variable’s value that the Pointer is pointing to.

If the Pointer in C is assigned to NULL, it is pointing to nothing. The size of any pointer is 2 bytes (for the 16-bit compiler).

#include <stdio.h>
int main()
{
  int *ptr, k;
  k = 21;
  ptr = &k;
  printf("%d \n", *ptr);
  return 0;
}

So, here first, we have defined the two variables.

One is an integer pointer, and the second is an integer. Now, we have assigned the k int to 21, and currently, we have attached the k’s address to the Pointer because the pointer store the address.

 

Pointers in C

Array Datatype in C

Arrays are the kind of data structure that can store a fixed-size sequential collection of elements of the same type. The specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element starts with 0 indexes, and the highest address to the last element.

Declaring Arrays

To declare an array in C programming, you need to specify the type of the elements and the number of elements required by an array. The syntax is following.

type arrayName [ arraySize ];

Let us take the example of Arrays in C.

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

  int k[2] = {3,5};
  int i;
       
  for ( i = 0; i < 2; i++ ) {
    printf("Element is %d \n", k[i]);
  }
  return 0;
}

Here,  we have declared the array, which has size 2. We have a loop through each item in an array and display it.

 

Arrays in C

Structure Datatype in C

The structure in C is a collection of items of different data types. You can think of a structure as the “record” is in Pascal or the class in Java without methods. The structures, or structs, are instrumental in creating data structures for larger and more complex projects.

The structure is a user-defined data type in C/C++. The structure creates the data type that can group items of possibly different types into a single type.

You can create the structure using the ‘struct‘ keyword.

struct address 
{ 
   char name[20]; 
   char street[80]; 
   char city[20]; 
   char state[20]; 
   int pin; 
};

Union Datatype in C

Like the Structure datatype, the union is a user-defined data type. In union, all members share the very same memory location. 

#include <stdio.h> 
  
union appdividend 
{ 
  int x, y; 
}; 
  
int main() 
{ 
  union appdividend a; 

  a.x = 2;
  printf ("After making x = 2:\n x = %d, y = %d\n\n", a.x, a.y); 

  a.y = 10;
  printf ("After making y = 10:\n x = %d, y = %d\n\n", a.x, a.y); 
  
  return 0;
}

Here, the union datatype shares the exact location. So if we change the value of x, it reflects in the value y and vice-versa.

Union in C

That’s it for data types in C tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.