Data Types in C

Data is the primary need for any programming language. This data can be of various types, including integers, fractional numbers, characters, and symbols, or a group of entities. To handle these different types of data, the concept of data types has been introduced in C. Data types in C, signify the characteristics of the data stored in a variable. Every type of data has its own data type that cannot be used to store other types of data.

Moving forward, let us start with various data types in C.

Various Data Types

Data_Types_in_C_1.

Primary Data Types in C

Primary data types are also known as the fundamental data types because they are pre-defined or they already exist in the C language. All the other types of data types (derived and user-defined data types) are derived from these data types. Primary data types in C are of 4 types: int, char, float, and double. In this section, we are going to discuss all these data types in detail.

The following table represents the memory consumed or size of each primary data types in C:

DATA TYPE

MEMORY (bytes)

RANGE

FORMAT SPECIFIER

int

4

-231 to 231 - 1  

%d 

char

1

-128 to 127

%c

float

4

1.2E-38 to 3.4E+38

%f

double

8

2.3E-308 to 1.7E+308

%lf

  • Int Data Type

The int data type is used to store the integer values. Any number that falls in the range of -∞ to +∞ is said to be an integer. For example 0, 10, 105, -15, -103, etc are all integers. However, integers can be either negative or positive. And that’s where we use data type modifiers, which we will be going to discuss later in this article.

In C language, the integers cannot be represented as floating or decimal point numbers. They can only be operated by/on integers themselves. For example, 4/3 is fine but 4/3.0 is not fine.  However, if the operation of two integers results in a fraction number, the integer part will be stored as the final result.

Syntax

int variable_name;

Example

The following example illustrates the int data types in C.

#include <stdio.h>

int main()

{

    //variables num, ans and i

    //are initialised as int

    int num = 5, ans = 1;

    int i;

    printf("The multiples of num are: \n");

    for (i = 1; i <= num; i++)

    {

        //int variable is getting multiplied

        //with int and the result is

        //stored in another int variable

        ans = i * num; 

        //print the result

        printf("5 * %d", i);

        printf(" is %d", ans);

        printf("\n");

    }

}

 Data_Types_in_C_2.

The above example declares and initializes an integer 5 and prints its multiples up to 5. Here, we can easily observe that all the operations that are being conducted include the variables of the same data type ie., int data type. And the final result is also being stored in another variable of the int data type.

  • Char Data Type

The char data type is used to store the characters. The characters stored in a variable of char data type have a value equivalent to its integer code. These codes are often called American Standard Code For Information Interchange or ASCII codes. The ASCII code for upper case characters A to Z starts from 65 to 90 and for lower case characters a to z, it starts from 97 to 122.

The general size of a character is 1 byte. However, just like the integers the size of a char can vary according to the data type modifiers. Since all characters and symbols can be represented in their numerical ASCII value, the char type is often considered as an int type.

Syntax

char variable_name;

Example

The following example illustrates the char data types in C.

#include <stdio.h>

int main()

{

    //variable ch is initialised as char

    char ch = 'A';

    //storing same ASCII code in int

    int num = ch;

    //print the ASCII value of ch

    printf("The ASCII code for %d", ch);

    printf(" is %d", num);

    printf("\n");

    printf("Adding 1 to the character code \n"); 

    //Adding 1 to the char variable ch

    ch += 1;

    //num stores the corresponding

    //ASCII value of ch

    num = ch;

    //print the ASCII value of ch+1

    printf("The ASCII code for %d", ch);

    printf(" is %d", num);

}

 Data_Types_in_C_3. 

In the above example, we have initialized a variable ch with character A. Then, we have initialized an int variable with ch. Now, this variable is holding the ASCII value of character A. This has proven when we added 1 in the num and printed it, we got 66 as output. 

  • Float Data Type

The float data type is used to store the floating-point numbers. The numbers that have a fractional part are called floating-point numbers. For example, 3.0, 5.57, -31.2, -3.12, etc are all floating-point numbers. Moreover, it should be noticed carefully that 8 is an integer but 8.0 is a floating-point number. Although the value of both the digits is the same, 8.0 has a decimal number which differentiates it with 8.

The major advantage of floating point numbers over integers is that they can represent a much larger and wider range of digits as compared to int data type.

Syntax

float varaible_name;

Example

The following example illustrates the float data types in C.

#include <stdio.h>

int main()

{

    //variable radius is initialised as float

    float radius = 10.0; 

    //print the radius

    printf("Radius of the circle is: %f", radius);

    printf("\n");

    //variable area is initialised as float

    float area;

    area = 3.14 * radius * radius; 

    //print the area of the circle

    printf("Area of the circle is :%f", area);

}

 Data_Types_in_C_4

In the above example, we are calculating the area of a circle. Note that, we are multiplying only the variables having float data type with each other and storing the final result in another float type variable.

  • Double Data Type

The double data type is also used to store floating-point numbers. But still, it is considered as a distinct data type because of two reasons. The first one is that it occupies twice as much memory as type float. The second reason is that it is used to store a much larger range of floating-point numbers than a float data type.

The double stores almost double the number of significant digits after the decimal point or precision of the float. However, the double data type consumes more memory and it  is also slower as compared to float. So, it is advisable to use a smaller data type as much as possible for the sake of optimization of code.

Syntax

double variable_name;

Example

The following example illustrates the double data type in C.

#include <stdio.h>

// function to calculate surface area and volume

// of a sphere

void sphere(int radius)

{

    // variables to hold double values

    double surfaceArea, sphereVolume;

    //Calculating the surface area 

    //and surface volume of the sphere

    surfaceArea = 4 * 3.14 * radius * radius;

    sphereVolume = (surfaceArea * radius) / 3; 

    printf("The surface area of the sphere is: %lf", surfaceArea);

    printf("\n\nThe volume of the sphere is:  %lf", sphereVolume);

int main()

{

    int radius = 6;

    sphere(radius); 

    printf("\n\n");

    return 0;

}

 Data_Types_in_C_5.

In the above example, we are calculating the area and volume of a sphere. Here, we have used the double-type variables. One thing to note here is that after the final answer, the remaining significant digits have automatically set to 0.

Those were the primitive data types in C.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Derived Data Types in C

Derived data types are derived from the primitive or fundamental data types. There are mainly 3 types of derived data types in C that we are going to discuss in this section.

  • Arrays

An array is a group of similar kinds of finite entities of the same type. These entities or elements can be referred to by their indices respectively. The indexing starts from 0 to (array_size-1) conventionally.  An array can be one-dimensional, two-dimensional, or multidimensional.

Syntax

data_type arr_name[size];

Description of the syntax

  • data_type: This is the data type that specifies the type of elements to be stored in the array. It can be int, float, double, and char.
  • array_name: This is the name of the array. To specify the name of an array, you must follow the same rules which are applicable while declaring a usual variable in C.
  • size: The size specifies the number of elements held by the array. If the size is n then the number of array elements will be n-1.

Example

The following example illustrates the array data types in C.

#include <stdio.h>

int main()

{

    int idx, element;

    // initialize an array.

    int my_array[10] = {10, 0, 29, 8, 52, 14, 16, 100, 2, 27};

    printf("Enter element to be searched:\n");

    // input element to be searched.

    scanf("%d", &element);

    // traverse the array to search the element.

    for (idx = 0; idx <= 9; idx++)

    {

        if (my_array[idx] == element)

        {

            // print the index at which

            // the element is found.

            printf("Element found at idxex %d", idx);

            break;

        }

    } 

    // if the element is not found.

    if (idx == 10)

    {

        printf("\nElement not found!");

    }

     return 0;

}

 Data_Types_in_C_6

The above example is to check if the input element exists in the array or not. Here, we have declared an integer type array for size 10. We are iterating over the array and checking if the given element is found in the array. 

  • Functions

A function is a piece of code that performs some specific task when invoked in the program. It can be called from anywhere and any number of times in the program. The return value i.e., what type of value it will return depends upon the return type of the function.

In C, a function can be called by types: call by value and call by reference. When the function is called by value, a copy of the variable is passed as the argument whereas when the function is called by the reference, the address or reference of variable itself is passed to the function.

Syntax

return_type function_name(parameters);

Description of the Syntax

  • return_type: This is the data type that specifies the type of value to be returned by the function. If the return type is void, then it is not mandatory for the function to return a value.
  • function_name: This is the name of the function. To specify the name of a function, you must follow the same rules which are applicable while declaring a usual variable in C.
  • parameters: The parameters are optional. They are passed according to the type of the function call.

Example

The following example illustrates functions in C.

#include <stdio.h>

// function returning the maximum

// numbers between two integers.

int max_num(int num1, int num2)

{

    // local variable declaration

    int res; 

    if (num1 > num2)

        res = num1;

    else

        res = num2; 

    return res;

int main()

{

    // local variable definition.

    int num1 = 225;

    int num2 = 250;

    int res;

     // function call.

    res = max_num(num1, num2); 

    // print the result.

    printf("Maximum number is : %d\n", res);

    return 0;

}

Data_Types_in_C_7

The above example is comparing two numbers and finding the greatest among them. Here, we have passed two numbers that need to be compared, to the function by value. The function has an int return type so, it is returning the greater number after making a comparison.

  • Pointers

A pointer can be defined as a variable that stores the address of other variables. This address signifies where that variable is located in the memory. If a is storing the address of b, then a is pointing to b. The data type of a pointer must be the same as the variable whose address it is storing. 

Syntax

type *pointer_name;

Description of the syntax

  • type: This is the data type that specifies the type of value to which the pointer is pointing.
  • pointer_name: This is the name of the pointer. To specify the name of a pointer, you must follow the same rules which are applicable while declaring a usual variable in C. Apart from these rules, a pointer must always be preceded by an asterisk(*).

Example

The following example illustrates pointers in C.

#include <stdio.h>

int main()

{

    // array declaration and initialization.

    int arr[4] = {50, 100, 150, 200}; 

    // int type pointer variable declaration.

    int *ptr; 

    // Assign the address of arr[0] to ptr.

    ptr = arr;

    for (int i = 0; i < 4; i++)

    {

        printf("Value of *ptr = %d\n", *ptr);

        printf("Value of ptr = %p\n\n", ptr); 

        // increment pointer ptr by 1.

        ptr++;

    }

}

 Data_Types_in_C_8.

In the above example, we have declared a pointer ptr that is holding the address of the array arr. We have looped through the array and printed the value at each index along with the address of the element.

Those were the derived data types in C. Moving forward, let us understand the user-defined data types in C.

User-Defined Data Types in C

  • Structure

A structure is a user-defined data type in C that allows you to combine members of different types under a single name (or the struct type). The reason why it is called a user-defined data type is that the variables of different types are clubbed together under a single structure, which can be defined according to the user’s choice. 

Consider a situation where you want to store a record of a book. The book will have properties like name, author, and genre. You can create three variables to store this information. But what if you need to store records of 10 books. Then creating 3 separate variables for each book would not be a practical solution. This is where a structure can be a great solution. A single structure book can be created having three members: name, author, and genre. This structure can be used for all 10 books. 

Syntax 

// define a structure.

struct structure_name 

{

    data_type var1;

    data_type var2;    

};

Description of the Syntax

    • struct: The definition of a structure includes the keyword struct followed by its name. All the items inside it are called its members and after being declared inside a structure. 

  • data_type: Each variable can have a different data type. Variables of any data type can be declared inside a structure.

  • The definition of a structure ends with a semicolon at the end.

Example

The following example illustrates structure in C.

#include<stdio.h>  

#include <string.h>  

// define a "user-defined" structure.

struct book      

{  

    // declare members of the structure.

    int id;      

    char name[25];

    char author[50];

    char genre[20];      

};  

int main( )    

{    

   // declare a variable of the book type.

   struct book b1; 

 

   //store the information of the books.    

   b1.id = 10; 

   strcpy(b1.name, "Dummy");  

   strcpy(b1.author, "Dummy Author"); 

   strcpy(b1.genre, "Science Fiction");

   // print the information.    

   printf( "The id is: %d\n", b1.id);    

   printf( "The name of the book is: %s\n", b1.name);    

   printf( "The author of the book is: %s\n", b1.author);    

   printf( "The genre of the book is: %s\n", b1.genre);     

   return 0;  

}  

Data_Types_in_C_9

In the above example, a structured book is defined with 4 member variables: id, name, author, and genre. Now a separate copy of these 4 members will be allocated to any variable of the book type. So, the variable b1 also has its own copy of these 4 variables which are used to store the information about this book.

  • Union

A union is also a user-defined data type. It also holds members of different data types under a single name. A union sounds similar to a structure and they are similar in conceptual terms. But there are some major differences between the two. While a structure allocates sufficient memory for all its members, a union only allocates memory equal to its largest member. 

Syntax 

// define a structure.

union structure_name 

{

    data_type var1;

    data_type var2;   

};

Description of the Syntax

  • union: The union keyword is written at the beginning of the definition of a union in C. After it,  the name of the union is specified.
  • data_type: It is the data type of the member variable of the union. Members of different types can be defined inside a union.

To understand the major difference between a structure and a union, consider the following definitions of a structure and a union:

Structure definition

struct book      

{  

    int price;      // 4 bytes

    char name[10];  // 1*10 = 10 bytes    

};  

Union definition

union book      

{  

    int price;      // 4 bytes

    char name[10];  // 1*10 = 10 bytes    

};

An object of the structure book would be allocated 14 bytes for both the int and char members. However, an object of the union book would only be allocated 10 bytes (equal to the memory required by the char member) which is the maximum size.

Example

The following example illustrates union in C.

#include<stdio.h>

#include<string.h>

// define the union. 

union city

{

    int pinCode;

    char name[20];

}; 

int main( )

{  

   // object of the type "city".

   union city c1; 

   c1.pinCode = 110090;

   strcpy( c1.name, "Delhi");

   // print the information.

   printf("The pin code of the city: %d\n", c1.pinCode);   

   printf("The name of the city is: %s\n", c1.name); 

}

Data_Types_in_C_10

In the above example, the char array name is printed correctly whereas the pinCode gives a corrupted value. This happened because the name occupied the space allocated for object c1. 

  • Enumeration

Enumeration or simply enum is one of the user-defined data types in C which provides a special type of flexibility of defining variables. An enum consists of a set of integer constants that can be replaced by user-defined names. 

Syntax

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

Description of the Syntax

  • enum: The keyword enum is written at the beginning of the definition.

  • flag: This is the default name of the enumeration set. It can be replaced by another name or can be used as it is. 
  • const_name: It is the integral identifier inside the enum set. The default values of this set are- {0, 1, 2, ….}.

Example

The following example illustrates enumeration in C.

#include <stdio.h>

// define enum flags.

enum color { red, blue, green };         // set is {0, 1, 2}.

enum fruit { mango = 10, apple, grapes }; // set is {10, 11, 12}. 

int main()

{

    enum color c1;

    enum fruit f1;

    c1 = blue;  // default value i.e. 1 will be assigned.

    f1 = apple; // 11 will be assigned. 

    printf("The second value in enum set is: %d\n",c1);

    printf("The second value in enum set is: %d\n",f1); 

    return 0;

}

Data_Types_in_C_11. 

In the above example, two enum flags are defined. The first enum flag color{red, blue, green} has a constant with default values i.e. {0, 1, 2}. So, when the second constant blue is printed, 1 will be displayed in the output. Whereas, in the second enum flag fruit{mango, apple, grapes}, the first constant i.e. mango is initialized with 10. So, the values of the following constants will automatically get updated. Hence, when the second constant of the enum fruit is printed, 11 is displayed in the output.

Those were the user defined data types in C.

Next, we will be looking at the data type modifiers in data types in C.

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

Data Type Modifiers

DATA TYPE

MEMORY (bytes)

RANGE

FORMAT SPECIFIER

short int

2

-32,768 to 32,767 

%hd 

unsigned int

4

0 to 4,294,967,295 

%u

long int

4

-231 to 231 - 1  

%ld

long long int

8

-(263) to (263)-1

%lld 

unsigned long int

4

0 to 4,294,967,295 

%lu

unsigned long long int

8

0 to 2^64-1 

%llu

signed char

1

-128 to 127 

%c

unsigned char

1

0 to 255 

%c

long double

16

3.4E-4932 to 1.1E+4932

%Lf

  • Signed

When a signed variable is declared, it indicates that it can have both negative and positive values. This modifier can be applied to data types like int, char, short, and long. In C, the value stored in a variable is signed by default. For example in the declaration, int var = 10, the int variable is a signed int by default and the variable var can hold both negative and positive values.

Syntax

signed type var_name;

Examples

signed int var1;

signed short int var2;

signed long int var3;

signed long long int var4;

signed char var5;

Note: Since all data types are signed by default, the best practice is to declare variables without using the suffix “signed”.

An n-bit variable can contain 2n  possible values. So, an 8-bit signed integer can store 28 i.e. 256 possible values.

Bits

Range

8 bit signed

-128 to 127

16 bit signed

-32,768 to 32,767

32 bit signed

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

64 bit signed

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

The following example illustrates the size occupied by signed variables.

#include <stdio.h>

int main()

{

    // declare signed variables.

    signed int var1;

    signed short int var2;

    signed long int var3;

    signed long long int var4;

    signed char var5;

    // print the size occupied by the signed variables.

    printf("The size occupied by signed int is: %d\n", sizeof(signed int));

    printf("The size occupied by signed short int is: %d\n", sizeof(signed short int));

    printf("The size occupied by signed long int is: %d\n", sizeof(signed long int));

    printf("The size occupied by signed long long int is: %d\n", sizeof(signed long long int));

    printf("The size occupied by signed char is: %d\n", sizeof(signed char));

    printf("\n\n");

}

Data_Types_in_C_12.

  • Unsigned

An unsigned variable can contain only 0 and positive values (non-negative values). This data type modifier can be used with the types- int, char, short, and long. To declare an unsigned variable the suffix “unsigned” is used before the type of the variable. 

The range of possible values of an unsigned variable is nearly double the range of a signed variable. This data type modifier is useful for positive entities like age, salary, etc.

Syntax 

unsigned type var_name;

Examples

unsigned int var1;

unsigned short int var2;

unsigned long int var3;

unsigned long long int var4;

unsigned char var5;

In a 32-bit system, an unsigned integer can hold values up to 232-1 i.e. 0 to 4,294,967,295 values.

Bits

Range

8 bit unsigned

0 to 255

16 bit unsigned

0 to 65535

32 bit unsigned

0 to 4294967295

64 bit unsigned

0 to 18446744073709551615

The following example illustrates the size occupied by unsigned variables.

#include <stdio.h>

int main()

{

    // declare unsigned variables.

    unsigned int var1;

    unsigned short int var2;

    unsigned long int var3;

    unsigned long long int var4;

    unsigned char var5; 

    // print the size occupied by the unsigned variables.

    printf("The size occupied by unsigned int is: %d\n", sizeof(unsigned int));

    printf("The size occupied by unsigned short int is: %d\n", sizeof(unsigned short int));

    printf("The size occupied by unsigned long int is: %d\n", sizeof(unsigned long int));

    printf("The size occupied by unsigned long long int is: %d\n", sizeof(unsigned long long int));

    printf("The size occupied by unsigned char is: %d\n", sizeof(unsigned char)); 

    printf("\n\n"); 

}

 Data_Types_in_C_13

  • Long

The long data type modifier is used when the value to be stored is large and exceeds the usual integer range. It can be applied to int and double data types in C. A long integer variable takes 4 bytes of memory space in a 32-bit system. 

Syntax

long type var_name;

Examples

long int var1;       // or long var1

long long int var2;  // or long long var2

long double var3;

In C, “long int” is equivalent to simply “long” and similarly instead of “long long int” you can simply write “long long”. This is commonly used in competitive programming as the inputs there can be very large.

The following example illustrates the size occupied by long variables.

#include <stdio.h>

int main()

{

    // declare long variables.

    long int var1;      

    long long int var2;  

    long double var3;

    // print the size occupied by the long variables.

    printf("The size occupied by long int is: %d\n", sizeof(long int));

    printf("The size occupied by long long int is: %d\n", sizeof(long long int));

    printf("The size occupied by long double is: %d\n", sizeof(long double));

    printf("\n\n");

}

Data_Types_in_C_14.

  • Short

The short data type modifier is used for saving memory consumption. It can contain whole numbers lying in the range from -32768 to 32767. It can only be applied with an int data type and it’s also said to be a 16-bit signed two's complement integer.

Syntax

short int var_name;

// or

short var_name;

Note: This data type modifier takes 2 times less memory than the usual integer variable. 

The following example illustrates the size occupied by long variables.

#include <stdio.h>

int main()

{

    // declare a short variable.

    short int var_name; 

    // print the size occupied by the long variables.

    printf("The size occupied by long int is: %d\n", sizeof(short int));

    printf("\n\n");   

    return 0;

}

Data_Types_in_C_15.

That was all about Data types in C.

dvance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Wrapping Up!

In this article, we have learned the different data types in C programming language. You started with a basic introduction of Data types in general. You then saw the three main categories of data types in C - primary, derived, and user-defined data types. Next, you went through the different data types in C in each of those categories with the syntax to initialize them, their range, and a few examples. In the end, you learned about the data type modifiers in C language. 

Why stop here? You can check out our popular Full Stack Web Development certification training course that will allow you to learn the ins and outs of web development along with topics such as agile methodology, DevOps, java spring, hibernate, JS, HTML, CSS, etc. It is a 9-month long self paced training course and the instructors will be available throughout the duration to solve your queries. If you want to learn more such interesting technologies for free, you can check out Simplilearn’s free online courses.

If you have any queries in this “Data Types in C” article or suggestions for us, please mention them in the comment box and our experts answer them for you as soon as possible.

Happy Learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.