top

Search

C Tutorial

.

UpGrad

C Tutorial

Data Types in C

Overview

How many data types in C are there? What are the basic data types in C? Are these questions bothering you? Well then, worry no more. 

The following article will give you an in-depth detail of the various data types, such as primitive data types in C and derived data types in C, among others. Explore more about data type modifiers and their functions in the C programming language. 

Introduction to Data Types in C

Simply put, data types in C refer to the multiple categories of data that ultimately determine the multiple functions that can be performed on them and the types of value that can be stored.

Specifying the data type in C is important because it helps the compiler to understand exactly what type of data it must expect from a given program. Before delving into the various types of data in C, let’s first look at its importance. 

Importance of Understanding Data Types in C

Understanding data types in C is crucial for multiple reasons. Such includes, 

Instrumentation

Instrumentation refers to the process of keeping track of data and then routing the same to an internal or external storage system. When determining which events to track, it is essential to specify clearly the data type for each property. This will prevent any errors or unwanted behaviour that might arise during this entire journey. 

Memory Allocation

In C, each data type occupies a specific amount of memory. Understanding these memory requirements is crucial as it allows you to allocate the appropriate amount of memory for variables within a data structure. This, in turn, facilitates the process of efficient memory usage and significantly reduces the chances of insufficient memory allocation, adversely affecting program performance. 

Optimisation

By selecting the right data type, you can use special features and optimisations provided by the C compiler for certain data types. This not only enhances overall performance but also enables efficient code execution. 

Apart from these, understanding the varied data types in C offers several advantages, including code integrity, readability, data validation, and memory management. 

Types Of Data Types In C

There are mainly two broad categories of data types in C. These are, namely,

  • Primary Data Types 

  • Derived Data Types

Primary Data Types

Primary data types in C, also known as fundamental data types, are predefined and inherent to the language. Unlike derived data types, they are already built into C. These primary data types can be categorised into four main categories:

Integer Data Types

Numbers that fall within the range of a -∞ to +∞ are said to be integers. A few examples of the same include 0, 10, -15, and -103, among others. To declare a variable of the integer data type in C, you can use the following syntax:

int variable_name;

Floating-point Data Types

Floating-point data types, also referred to as floating-point numbers, are those that have a fractional part. They can represent a much larger and wider range of digits, in contrast to the integer data types. A few examples of floating point numbers are 3.0, -31.2, and 8.0.. The syntax for floating point data types is: 

float varaible_name;

Character Data Type

The character data type is often considered an integer data type. However, unlike integer data types, the character data type primarily stores characters. The characters stored comprise values similar to its integer code. The syntax for the same is,

char variable_name;

Void Data Type

In C, the void data type refers to the absence of a type. It is typically used as a return type for functions that do not return a value or as a parameter type for functions that do not accept any arguments. 

Derived Data Types

Derived data types in C are typically formed by combining primitive and derived data types to generate complex data structures. By using the same, you can define your own data structures to represent and organise related data. These derived data types can be further categorised into various subcategories, including:

Array Data Type

Arrays in C allow you to store a collection of elements of the same type with a fixed size. These elements can be accessed using an index that represents their respective position in the array. To declare an array, you have to first specify the base type of the elements and the number of elements. It is also important to note that arrays can be one-dimensional or multidimensional. The syntax for the same is

data_type arr_name[size]

Pointer Data Type

Pointers in C store the memory address of another variable, allowing for indirect access and manipulation of variables by referencing their memory location. Furthermore, the pointer data type can also be used for performing crucial tasks such as creating complex data structures and implementing dynamic memory allocation. The syntax for declaring a pointer in C is as follows:

type *pointer_name;

Structure Data Type

Structures are useful in grouping different variable types into one single unit. They are defined using the keyword ‘struct’. Each variable present with the structure is referred to as a member or a field. The syntax for the same goes as follows,

// define a structure
struct structure_name
{
    data_type var1;
    data_type var2;    
};

Union Data Type

Unions in C, like structures, group variables of different types. However, unions can only hold one member at a time, while all the other fields share the same memory location. Thus, changing the value of one member automatically overwrites the values of other members. To define a union in C, we use the keyword ‘union’. The syntax includes,

// define a structure
union structure_name
{
    data_type var1;
    data_type var2;  
};

Enumeration Data Type

The enumeration data type consists of a set of named values referred to as enumerators. They help define a list of named constants by assigning meaningful names to values so the code can be easily readable and maintainable. The syntax for the enumeration data type is 

enum flag {const_name1, const_name2, ..., const_nameN};

User-defined Data Types

Programmers primarily use user-defined data types in C to define new types usually not present in the standard set of data types. They offer enough flexibility and facilitate the process of writing more readable and clean code by assigning meaningful names to values. In the C programming language, various constructs such as structures, unions and typedef are used to define these data types. 

Type Definition Using typedef

The typedef keyword is primarily used to create a customised new name for an existing data type. It enables you to define the custom names, making code readability and maintainability much easier. The syntax is

typedef existing_type new_type_name;

To get a better understanding of how you can use typedef, here is a small example,

typedef unsigned int uint;
typedef struct {
    char name[50];
    int age;
} Person;

Here, we have used ‘uint’ as an alias or a new name for ‘unsigned int’. ‘Person’, on the other hand, is an alias for the structure definition. 

Type Definition Using struct and union

Using the keyword ‘typedef’ with structures and unions helps simplify the syntax required to declare variables of data types throughout the code base. 

Typedef for Structure

typedef struct {
    char name[50];
    int age;
    float salary;
} Employee;
Employee emp1;
emp1.age = 30;

Here, we have used ‘typedef’ to define a structure named ‘Employee’. This allows you to use ‘Employee’ as a new type name instead of writing ‘struct Employee’ each time.

Typedef for Union

typedef union {
    int intValue;
    float floatValue;
} Data;
Data myData;
myData.floatValue = 3.14;

In this example, we have used ‘typedef’ to define a union named ‘Data’. Thus, you can now use ‘Data’ as a new type name for the union. 

Data Type Modifiers In C Programming Language

Data type modifiers are used to modify the behaviour and properties of basic data types. In C programming, data modifiers are used to specify additional information about the storage or behaviour of variables. Such include:

  • ‘signed’ and ‘unsigned’ - They help to accurately indicate whether a variable can represent both positive and negative values or only non-negative values.

  • ‘short’ and ‘long’ - They specify the range of values that a variable can hold. The ‘short’ modifier is used to reduce the range, whereas ‘long’ is used to increase the range of values compared to the base type. You can also combine both these modifiers with the help of ‘int’. 

  • ‘const’ - It helps to declare a constant variable whose value cannot be modified after initialisation.

Range Of Values Of C Data Type

The range of values that can actually be stored in the C data types boils down to the size and signedness of the data types. Some of the most widely used data types in C and their corresponding ranges are mentioned below.

Data Type

Range

Size

unsigned char

0 to 255

1 byte

signed char

-128 to 127

1 byte

unsigned short int

0 to 65535

2 bytes

unsigned long int

0 to +4,294,967,295

4 bytes

signed short int 

-32768 to +32767

2 bytes

signed long int 

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

4 bytes

float

3.4E-38 to 3.4E+38

4 bytes

Use Of C Data Types

Data types in C serve multiple purposes in programming. From memory allocation to defining the behaviour of operations, they help programmers write code more cleanly and efficiently. Some of the main uses of C data types include,

Error Checking 

Data types in C facilitate type checking, which in turn helps detect any programming errors that might arise during the compile time. Once the problem has been identified, you can then take the necessary course of action to eradicate the same. This not only enhances the program's safety but also reduces runtime errors. 

Compatibility 

Programs that follow the standard data types can easily be compiled and executed on different platforms. Thus, this ensures compatibility and makes the code more portable and platform-independent.

Code Readability and Maintainability 

Well-chosen data types help to make the code readability much easier by conveying its actual purpose and the intended usage of variables. This also simplifies the whole process of program maintenance and debugging. 

Practice Problems On Data Types In C

Here are a few practice problems you can try out during your spare time to further improve your knowledge of data types in C.

  • Write a C program that converts a string to an unsigned long integer.

  • Write a C program that can generate any random number

  • Write a C program that can execute a command with the help of the command processor.

  • Write a C program to accurately calculate the sum and average of any three numbers. 

Additionally, exploring online courses, such as upGrad's MS in Machine Learning and AI program from Liverpool John Moores University, can provide valuable learning opportunities and support for your career development in machine learning and artificial intelligence.

FAQs

Q1: What do we mean by keywords in C?

In C programming language, keywords are predefined reserved words that usually carry specific meanings for the compiler. They are a part of the syntax and cannot be used as an identifier.

Q2: What do we mean by void type in C?

In C, the void type is a special type representing the absence of a value or the lack of any type. It is useful when dealing with generic pointers, especially in function definitions and memory management.

Q3: What are the different data types in C?

There are multiple data types in C, such as integer, floating-point, array, string, and character. Each data type in C comes alongside a predetermined set of properties, such as the range of values, memories and operations. 

Leave a Reply

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