C Programming Tutorial for Beginners

What Are Data Types in C Language? Definition, Types, Examples, Range, Size

Introduction

In this topic of our C Tutorial for Beginners, we are going to understand in-depth the concept of data types in C language. 

Here, we will discuss everything related to it, including:

  • Definition of data types

  • Examples

  • Primary data types

  • User-defined data types

  • Derived data types

  • Size and range of all data types

And more…

What Are Data Types in C Language?

Definition

In C programming language, data types are used to define the type of data that a variable can hold. 

Each data type has a specific size and range of values that it can represent.

There are several built-in data types in C, which include:

  • int - used to store integers (whole numbers)

  • float - used to store floating-point numbers (numbers with a decimal point)

  • double - used to store double-precision floating-point numbers

  • char - used to store single characters

  • short - used to store small integers

  • long - used to store large integers

  • long long - used to store very large integers

  • unsigned int - used to store non-negative integers

  • unsigned char - used to store non-negative single characters

  • void - used to indicate an empty data type, usually used with pointers

These data types can be modified using different specifiers like signed, unsigned, short, and long to provide additional variations. 

For example, "short int" is used to store small integers, and "long double" is used to store floating-point numbers with greater precision than the standard "double" type.

Example 1:

int age = 25;

The above example declares a variable named "age" of the "int" data type and assigns it a value of 25.

Example 2:

float price = 9.99;

The above example declares a variable named "price" of the "float" data type and assigns it a value of 9.99.

Classification of Data Types in C

In C programming language, data types can be classified into three categories:

1. Basic Data Types

These are the fundamental data types in C that are directly supported by the programming language. 

They include:

  • int: Used to store whole numbers.

  • char: Used to store a single character.

  • float: Used to store floating-point numbers.

  • double: Used to store double-precision floating-point numbers.

  • void: Used to indicate an empty data type.

2. Derived Data Types

The derived data types in C are derived from the basic data types and can be created using various operators. 

They include:

  • Arrays: A collection of elements of the same data type.

  • Pointers: A variable that stores the memory address of another variable.

  • Structures: A collection of variables of different data types grouped together under a single name.

  • Unions: A special type of structure that can store only one value at a time.

3. User-defined Data Types

The user-defined data types in C are defined by the programmer and are created using the basic and derived data types. 

They include:

  • Enums: A user-defined data type that represents a set of named constants.

  • Typedefs: A way to create a new name for an existing data type to improve code readability and maintainability.

Examples of Data Types in C

Here are some data types in C examples:

  • int data type example

int age = 25;

The "int" data type is used to store whole numbers. In this example, the variable "age" is assigned the value of 25.

  • float data type example

float price = 9.99;

The "float" data type is used to store floating-point numbers (numbers with a decimal point). In this example, the variable "price" is assigned the value of 9.99.

  • char data type example

char grade = 'A';

The "char" data type is used to store single characters. In this example, the variable "grade" is assigned the value of 'A'.

  • double data type example

double salary = 45000.50;

The "double" data type is used to store double-precision floating-point numbers. In this example, the variable "salary" is assigned the value of 45000.50.

  • short data type example

short int count = 100;

The "short" data type is used to store small integers. In this example, the variable "count" is assigned the value of 100.

  • long data type example

long int population = 1000000;

The "long" data type is used to store large integers. In this example, the variable "population" is assigned the value of 1000000.

  • unsigned int data type example

unsigned int quantity = 500;

The "unsigned int" data type is used to store non-negative integers. In this example, the variable "quantity" is assigned the value of 500.

  • void data type example

void* ptr;

The "void" data type is used to indicate an empty data type, usually used with pointers. In this example, the variable "ptr" is declared as a pointer to a data type that is yet to be determined.

What Are Primary Data Types in C Language?

There are five primary data types which are the building blocks of all other data types. 

  • int

The "int" data type is used to store whole numbers. In C, the size of the "int" data type is platform-dependent, but it is typically 2 or 4 bytes.

Example:

int x = 10;
int y = -5;
  • char

The "char" data type is used to store a single character. In C, the size of the "char" data type is always 1 byte.

Example:

char c = 'A';
  • float

The "float" data type is used to store floating-point numbers with single precision. In C, the size of the "float" data type is 4 bytes.

Example:

float f = 3.14;
  • double

The "double" data type is used to store floating-point numbers with double precision. In C, the size of the "double" data type is 8 bytes.

Example:

double d = 3.14159;
  • void

The role of a void data type is to indicate an empty data type. It is often used in function declarations to indicate that the function returns no value.

Example:

void print_hello() {
   printf("Hello, World!");
}

What Are User-Defined Data Types in C Language?

The user-defined data types in C are created by the programmer to make the code more expressive and maintainable. 

The different types of user-defined data types include:

  • Enum or enumeration

  • Structures

  • Typedef

  • Union

  • Enumeration data types in C (enum)

An enum in C is a user-defined data type that represents a set of named constants. Enums or enumerated data types in C can be used to make the code more readable and understandable by using descriptive names instead of numeric or string literals.

Example:

typedef enum {
   MONDAY,
   TUESDAY,
   WEDNESDAY,
   THURSDAY,
   FRIDAY,
   SATURDAY,
   SUNDAY
} DaysOfWeek;
int main() {
   DaysOfWeek today = MONDAY;
   return 0;
}

Here, we have defined an enum named "DaysOfWeek" that represents the days of the week. The enum includes seven named constants, one for each day of the week. We have then defined a variable named "today" of the enum type and assigned it a value of MONDAY.

  • Structure data type in C (struct)

A structure in C is a user-defined data type that can group together variables of different data types under a single name. 

Structures or struct can be used to represent complex data types that can be easily manipulated and passed around in the code.

Example:

typedef struct {
   char name[50];
   int age;
   float height;
} Person;
int main() {
   Person john;
   strcpy(john.name, "John");
   john.age = 30;
   john.height = 5.8;
   return 0;
}

Here, we have defined a structure named "Person" that includes three variables of different data types. 

We have then defined a variable named "john" of the "Person" type and assigned its values for the "name", "age", and "height" variables.

  • Union data type

A union is a composite data type that allows you to store different data types in the same memory location. 

It is similar to a structure in that it can contain members, but unlike a structure, a union can only have one member at a time.

Example:

union myUnion {
  int i;
  float f;
  char c;
};
int main() {
  union myUnion u;
  u.i = 42;
  printf("%d\n", u.i); // Output: 42
  u.f = 3.14;
  printf("%f\n", u.f); // Output: 3.140000
  u.c = 'A';
  printf("%c\n", u.c); // Output: A
  printf("%d\n", u.i); // Output: 65 (the ASCII code for 'A')
  return 0;
}

Here, we defined a union called myUnion with three members: I of type int, f of type float, and c of type char. 

We then declared a variable u of type myUnion and set it I member to 42. We printed out the value of the u.i, which was 42. 

Then, we set the f member to 3.14 and printed it out as a float. Finally, we set the c member to 'A' and printed it out as a character, and then printed out the value of u.i, which was 65 (the ASCII code for 'A').

  • Typedef data type

The typedef is actually a keyword that allows you to create an alias for an existing data type. This can make your code more readable and easier to maintain, especially when dealing with complex data types or function pointers.

Example:

typedef int myInt;
myInt x = 42;
printf("%d\n", x); // Output: 42

Here, we defined a new data type called myInt using typedef. We then declared a variable x of type myInt and set its value to 42. We printed out the value of x, which was 42.

What Are Derived Data Types in C?

The derived data types in C language are derived from the primary data types. These can be created using various operators and language features. 

  • Array

It is a collection of elements of the same data type. Arrays are declared using square brackets [] and can have a fixed or variable size.

Example:

int arr[5] = {1, 2, 3, 4, 5};
  • Pointer

It is a variable used for storing the memory address of another variable. Pointers are declared using an asterisk (*) and can be used to access and manipulate the value of the variable at that memory address.

Example:

int x = 10;
int *ptr = &x;
*ptr = 20;

Here, we have declared a pointer variable named "ptr" and initialized it to the memory address of the "x" variable using the address-of operator (&). We have then assigned the value of 20 to the memory location pointed to by "ptr" using the dereference operator (*).

  • Function

It is a block of code used to perform a specific task. A function can have a return type, which specifies the data type of the value that the function returns, as well as a set of parameters, which are the values that the function takes as input. The return type and parameter types of a function make up its function type.

Size and Range of Data Types in C Language

The size of data types in C depends on the implementation and the system architecture. 

However, the following table provides the standard minimum and maximum sizes and range of each data type as defined by the C standard:

Data Type

Size (in bytes)

Range

char

1

-128 to 127 or 0 to 255 (depending on whether signed or unsigned)

short

2

-32,768 to 32,767 or 0 to 65,535

int

2 or 4

-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

long

4

-2,147,483,648 to 2,147,483,647

long long

8

-(2^63) to (2^63)-1

float

4

3.4e-38 to 3.4e+38

double

8

1.7e-308 to 1.7e+308

long double

12 or 16

3.4e-4932 to 1.1e+4932

bool (C99 or later)

1

true or false

Note that the sizes and ranges of data types in C listed in the table above are minimums and can vary depending on the implementation and architecture. 

Data Types in C PDF (Free Download)

If you want to download the free PDF of data types in C with examples, then click on the link given below.

Download Now

Frequently Asked Questions (FAQs)

Below are some of the commonly asked questions about what is data types in C.

1. What are boolean data types?

C does not have a built-in boolean data type, but it can simulate boolean values using integers. 

The value 0 is considered false, and any non-zero value is considered true. To make the code more readable and self-documenting, it is common practice to use the header file "stdbool.h" and the keywords "bool", "true", and "false" to define boolean values.

2. What is the difference between a char and a string in C?

A char is a single character, whereas a string is a collection of characters. In C, a string is represented as an array of characters terminated by a null character.

3. What is the size of an int data type in C?

The size of an int data type in C is system-dependent, but it is usually either 2 or 4 bytes.

4. What is the size of a char data type in C?

The size of a char data type in C is always 1 byte.

5. What is the difference between a structure and a union in C?

A structure in C is a collection of elements of different data types, whereas a union is a data type that can hold any one of its members at a time.

6. What is the size of a double data type in C?

The size of a double data type in C is system-dependent, but it is usually either 8 or 10 bytes.

7. What are primitive data types in C?

Primitive data types are basic data types that are built into the language and used to define variables.

The following are the primitive data types in C:

  • Integer Types

  • Floating-Point Types

  • Character Types

  • Boolean Type

8. What are non-primitive data types?

Non-primitive data types in C are user-defined data types. They are created by the programmer, and are also known as derived data types.

9. What are built-in data types?

C has several built-in data types, which are classified into two categories: primitive data types and derived data types.

Primary Data Types:

  • char

  • int

  • float

  • double

  • void

Derived Data Types:

  • Arrays

  • Structures

  • Union

  • Pointer

  • Enumeration

Wrapping Up:

So, this was all about what are C data types with examples, types, ranges, sizes, and more. You must practice things on your own using an online C compiler for better understanding. 

For other concepts of this language, make sure to check our full C Programming Tutorial for Beginners, where we are covering everything in detail, including:

And more…

Did you find this article helpful?