Data Types in C Programming

Glad to see all of you, here again, Welcome again folks, in this module, we are going to talk about Data Types in C Programming, till now we have completed a wonderful journey in this series and also are ready to go ahead.

Let’s start this module and look into details about the same.

Data Types in C Programming

Data types in C Programming

We talked about variables and constants in the last module and there we were using data type also while declaring a variable and constant, if you haven’t seen the last module yet, then I Will recommend you to first go through the last module and then come into this module.

Data types in C Programming talk about the type and nature of the value that a particular variable is going to hold. i.e., suppose you buy water from a store, now the water bottle is the container i.e., the variable and the brand of water or the liquidity nature of the water is the data type here. In simple we should know what is nature and the type of things that are being stored in the container or variable in programming.

About Data types in C Programming

Data types in C Programming define the specific type of data that a particular variable can hold, for example, an integer variable can hold integer data, a character type variable can hold character data, a floating-point variable can hold decimal data, etc.

Data Types in C Programming are categorized into mainly 3 groups

  • User-defined Data Types in C Programming
  • Built-in / Primary Data Types in C Programming
  • derived Data Types in C Programming

Primary data types in C programming include Integer, Character, Boolean, Floating-Point i.e., decimal value, Double Floating Point i.e., large values, void i.e., null value, and wide character.

Derived data types include Function, Array, Pointer, and references.

User-defined data types include class, structure, union, enum, and typedef.

In this particular module, we will talk about primary data types in detail and the rest we will cover in the upcoming modules.

Primary Data Types in C programming

Primary data types are also called built-in data types, i.e., they are already built and stored in the C library. Let’s see the function of different built-in data types available:

int data types in C Programming

It is for integers it takes up to 2 or 4 bytes of memory from the compiler (1 byte = 8 bits) and can only store the integer values like 10, 20, and so on.
Example, int num = 20 ;

char data types in C Programming

It is for storing a character and takes up to 1 byte of memory from the compiler.

For example, char ch = ‘A’;

Here, in this example, char is the data type and ch is the variable name, which has stored the character A in it. One thing to be noted in this case is that while assigning a character to any variable, it should be in single quotation marks as given in the above example, else it will throw an error.

bool data types in C Programming

It is for Booleans True or False (0 or 1). It is used for checking various conditions required in the program, will see about this in the later modules.

For example, bool b = true;

float data types in C Programming

it is used for storing a floating-point number, i.e., a decimal number and takes up to 4 bytes of memory from the compiler.

Example, float num = 12.23 ;

Here, in this example, num is the variable name that is assigned with float data type and is holding a 12.23 value in it.

double data types in C Programming

It is used to store a large value decimal number and it takes up to 8 bytes of memory.

Example, double num = 10078.10099 ;

Here, if you have to store a large number then you have to go for the double data type.

Let’s see one sample problem demonstrating the data types.

#include <stdio.h>
int main( ) {
int n1 = 20;
float n2 = 45.3;
char chr = ‘A’;

// Displaying the value of variable n1
printf (“ n1: %d“, n1);

// Displaying the value of variable n2
printf (“ n2: %f”, n2);

// Displaying the value of variable chr
printf (“chr: %c“, chr);

return 0;
}

The output of the above Program:

n1: 20
n2: 45.3
chr: A

Here, in the above program, we have declared 3 types of data types int, float, and char with the n1, n2, and chr variables respectively and thus printing the same with printf() function. Different data types use different format specifiers, we have discussed format specifiers in the last module refer to that if we haven’t.

User-defined Data types in C Programming

There were some data types that were not defined in the C library, but there was a need for that, so in this, we the user can define our own data type, which is also known as secondary or non-primitive data type. Users can define this as per their requirements. There are many types of user-defined data types such as arrays, pointers, structures, unions, functions, etc. We will talk about this in more detail in the upcoming modules. so stay tuned.

In the Data Types in C Programming module, we have discussed one of the most important concepts of the C programming series i.e., data types. Hope you all are enjoying the programming and are excited about the upcoming modules.

Until then, Stay connected, keep learning, and have Happy coding!