Everything that we see around us, has some data associated with it.
We as a humans, store every data in our memory (may be in brain or mind or somewhere). That’s how we remember and we are able to understand the things when we see the next time.
Example 1:
Image if we loose our memory everyday. We have to relearn everything every time. If you have watched the hollywood movie 50 First Dates, you will understand exactly what I am saying.
Example 2:
Let’s say you are going to the market to bring vegetables, groceries etc. Do you need some storage to keep all the items? Yes. You need a carry bag. That’s the storage I am talking about.
In real life, we need the right kind of container to store different things such as a bag can hold solid items but may not hold liquids. A bottle can hold liquid items but not big solid blocks like stones.
Exactly the same way we need containers to hold or store different data that we operate in a C program which are called as data types.
Exactly the same way, the computer deals with a lot of data and the data must be stored somewhere or in some form.
While writing a program we need to store the data that we want to process with. At least till the program is running, it has to memorize the data on which it is operating.
Now, moving forward, understanding data and storage is fine, but are all the data same? Let’s explore different types of data in a new section.
Contents
Types of Data in Real Life Situation
Now let’s understand with the help of another example.
Example 3:
Let’s take the example of buying vegetables and groceries from the market. At least by now you understand that you need a carry bag to keep everything that you buy from the market.
Let’s give the name to this bag as carry_bag.
Is it enough to take one carry bag to keep everything inside?
Let’s say you are buying Sugar and Wheat flour (Atta). Is it okay if we combine both and keep it in the carry_bag? Or do you think we shall have two different individual bags for each?
So, we need 2 different bags, correct?
Let’s say we store sugar in sugar_bag and wheat flour in wheat_flour_bag. Does this make sense here?
The reason we keep different things in different bags is simple, because they are of different types.
Example 4:
Everything that we see around us, has some name or some identity associated with it.
Let’s say a light, fan, room, TV in our house etc.. We can easily define and differentiate every different object by its name.
Can I say the name of an object is a word, or combination of alphabets or characters? These kind of data in C is called as a string. A string is a combination of characters represent as a single entity or variable.
Now let’s say I have 3 rooms, 6 lights, 2 fans in my house. So, the 3, 6 and 2 are presented in number format and represents a count.
Let’s say we have so many trains in our country. How to differentiate or identify any train? They all have different numbers.
Numbers in C are represented as an integer just like a normal integer in math whose value can go from -ve through 0 to +ve.
And so on…
Different types of data in real life are represented using different data types in C or any programming language.
Now let’s look at the different kinds of data types available in C language using which we can represent real life data, such as: name, numbers, decimals etc.
Understanding Different Data Types in C
Now that you have got the very basic idea of how different things are segregated and stored in different bags, in the similar way we have different data types available in C to store different types of data.
There are some basic data types available which are the building block of C through which we can express any data that we use in real life situations.
Understand data type is a kind of data which has a specific type.
Basic (or primitive) Data Types
Let’s list down basic data types available in C language:
- int – to hold an integer value like: 45, 0, -28 etc.
- char – to hold a single alphabet or a character like A, b, @ etc. (Typically this character is as defined in ASCII chart)
- float – to hold single precision floating point number such as: a decimal number like 43.29
- double – holds double precision floating point number like: 23764909.98 (typically a huge floating point number)
- bool – to hold boolean value such as: 0 or 1 (this can be used to represent true or false)
Imagine, why did we have different bags in the previous example, to store some kind of thing.
These data types are like bags or containers which can hold some real life data.
Example to understand how to use these data types
Let’s understand how to use these data types by take an example of subtraction in a real life situation.
Let’s say you go to a shop where he has a sugar sack of 100 KG. Let’s call this 100 kg container as 100kg_sugar_sack.
You have asked for 2 KG of sugar. So, he takes 2 kg of sugar out of the above 100kg_sugar_sack and puts it into another bag let’s call this as a customer_sugar_bag.
So now, the 100kg_sugar_sack contains 98 KG of sugar and the customer_sugar_bag contains 2 KG.
This customer_sugar_bag bag will be returned to you.
Integer Variables
In C programming, more or less similar thing is happening.
Let’s assume that you are writing a subtraction program in C, so, you need two integer type variables as below; just like we had two bags in previous example:
- a – the minuend (from which subtraction will happen)
- b – the subtrahend (how much is to be subtracted)
- c – the final difference (what is the result)
In real life we write this mathematics expression like this: a – b = c
Let’s take a look how do we write in C programming or may be in many other programming language as well.
First, we have to define where do we hold or store the numbers of minuend, subtrahend and the difference.
So, we declare like this:
- int a;
- int b;
- int c;
As the subtraction operation happens on integer values, we use the integer data type to hold these values.
The subtract operation will be like this (a – b), it’s simple, right?
But there is a slight difference the way we store the final difference value in c. In C, the final value will be stored like this: c = (a – b) a little different the way we represent in real life which a – b = c.
IMPORTANT NOTE:
We only use int keyword while declaring the variables. When we use any mathematical operation on these variables we just use the variable names.
This is true for all kinds of data types and not just int data type.
Character Variables
Let’s say you are a teacher and you have to maintain a list of total number of students per class per section. Let’s see what all data you need:
- Class – it may be class 1 to 10
- Section – it can be Section A, Section B etc.
- Number of students – total number of students
It’s simple right?
Let’s see what all variable types you need to create a record where you can store all these details in a C program.
- class – an int type because it is a number
- section – a char data type because it is a character (A, B, C etc.)
- number of students – an int type because this is again a number
For example:
- int class;
- char section;
- int number_students;
Float / double Variables
Float variables are used to hold decimal place values for example you have to maintain the exact temperature in one of the variable.
This is where you have to hold it in a float variable.
You can declare a variable like this: float temperature;
Similarly double data type is used to hold larger decimal values.
I am completely aware that I have covered about float and double in few lines, because this requires very specific examples which I will be covering in detail in its dedicated post.
Bool variables
C has been in widely used close to hardware programming or in embedded systems programming where many a times we just need a variable to decide true or false or enabled or disabled like situation.
In this case, bool is the perfect data type to use. You can define a bool variable like bool is_enabled;
NOTE:
This can only hold a single bit which can have a value of 0 or 1.
Now that I have covered few data types which can hold the below things:
- a number – via int
- a character – via char
- a decimal value – via float/double
- a yes or no kind of value via bool
But by these things we cannot represent all kinds of data we see around us. Let’s see some data that we cannot represent with above data types:
- Name of a place, person or a thing
- This can be done by creating many individual char data type variables and combine them to represent the name we want to show.
- We cannot maintain a list of numbers.
- For now we have understood integer data type so we may need many such integer values to represent a collection of numbers. This not a collection of numbers rather individual numbers.
- How can we deal with data that is a combination of let’s say a number, a name, etc.
- Right now with the above data types it is not possible to represent this kind of combination of data.
C has something called as derived data type which are like advanced data type containers but based on the above basic data types only.
Let’s take a look at those derived data types.
Derived Data Types
Understand derived as constructed in C. This means, every derived data type will have a base of one or more of the above basic data types in it.
As I have said earlier that data type is a kind of data which has a specific type. Above section covers the basic data types and this one covers several derived or constructed data types.
Who has constructed these data types? The C standard.
All these derived data types are used to represent real life data which are combinations of basic ones and are little bit complex than the basic ones.
Let’s see what are the different kinds of derived data types available in C.
- Array / string – a collection of same data type but represented as a single data type (or a container)
- an array can be a collection of numbers, floats, chars etc.
- String – array of chars is called as a string. It is collection of character data types represented as a single variable (or a container)
- Structure / Union – a collection of different data types but treated as a single data type (or a container)
- for example all data of a student. It contains, name (a string), class (an integer), section (a char) etc.
- Let’s say student_data_type is a structure which has some containers as name, class and section. But the student_data_type is treated as a data type by itself.
- I will cover this in more detail in coming section and we will practice some hands-on code which I will cover in a separate post.
- Pointer – pointer is storage which holds memory addresses of other data types
- Explaining this in a single line example would be difficult. I will cover this in a next section in a little more detail
- Function – it is a set of instructions but it has a specific type or identity in C
- You may be surprised to know that I have called function as a data type because many teachers do not teach this as a data type
- But, function is a data type because it has a very specific type or distinction with respect to other data types.
Array in C
An array is a collection of same data types such as collection of 5 integers, or 3 floating point, or 10 bool variables etc.
I took the random numbers of 3, 5 just to give you a presentation and nothing specific to data type here.
So, think of an array like a basket of egg which can hold many eggs but it can ONLY hold eggs and nothing else.
An egg basket can have 6, 12 or 40 as a holding capacity which is fixed. Exactly same way, the capacity of an array is fixed throughout the program.
NOTE:
An array of characters is not array, rather it is called as a string.
A string has several other characteristics in comparison to an array. I will cover in brief about the same in the next section and in detail when we will learn about arrays and strings.
It is represented or declared in c like this: int numbers[5]; This means, numbers is an array which can hold 5 integer type variables.
An array has very special characteristics as below:
- It is treated as a single variable but contains only of same type variables
- The size of an array is predefined at declare time and this cannot be changed later
- All these items are sequentially accessible using the same variable name but with the index
- In the example of numbers [5] the name numbers is used as variable name and in this case the [5] is the index which can have 5 variables to store and the index will be starting from 0 to 4.
- So the individual variables from the array would be accessible like this: numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]
As an array is derived from other basic data types, it is called as a derived data type and the same is true for strings as well.
Right now, I am keeping it simple for you by not telling you anything about the underlying memory layout of an array.
String in C
A string looks exactly like an array by the way it is declared, stored or accessed.
But a string has significant differences which makes it different than an array.
Below are the characteristics of a string:
- It is collection of character data types ONLY
- It always ends with a ‘\0’ character (called as a null character) by the compiler or sometimes it has to be added by the user explicitly (will explain on this in detail).
- Because of the null character, the size of a string is always number of characters in the string + 1.
- Strings has a specific library support available for many operations (will learn more in detail in its dedicated section)
Every string is an array but not every array is a string.
Structure in C
Structures in C is similar to an array in the sense of holding many variables at once but it holds different kinds of variables (int, char, float all at once) in it unlike an array which can only hold a single type of data type (only int, or only float etc.).
Think of it as a modular bag which can hold many kinds of things such as: a pair of show, a laptop, a camera, clothes, etc.
NOTE:
Just like the way array and string looks the same but there is a significant difference between them, exactly same way a structure and a union in C looks the same but they have a significant difference in memory consumption, which we will discuss in detail when we will be covering the struct and union chapter.
Imagine, I am declaring a structure to hold student’s information such as: name, gender, class, height etc.
Because all these are different types of data I have to declare a structure like below:
struct student
{
char name = [50];
int class;
char section;
int height;
enum gender = {BOY, GIRL};
};
struct student one_student;
What I have declared as struct student is just a new type of data I declared just like existing type of int, char etc. But by this, I can not use it now. To use this I have to declare a new variable of type struct student.
I declared a single variable like this: struct student one_student;
As I said above, struct student is of a new type of data that is why we had to use the whole name to declare the variable one_student.
As a structure data type is derived from other basic data types, it is called as a derived data type and the same holds true for a union data type as well.
The C standard calls structure as a derived data type.
Some teachers or text books mention the structure as a user defined data type just to make students understand it in an easy way. But it is a non-standard naming.
Union in C
Union looks exactly same as a structure but the size of the union is always the highest size of all variables declared in it.
Till now I have not yet covered sizes of different data types, hence explaining the same will not be enough here.
For now just understand that a union looks like a structure in C but it is different the way memory is allocated for it.
Pointer in C
Pointer is the first topic where we talk about the address of the containers or data types that we use in our C program.
Imagine that you are going to your bank to deposit money. You don’t know which counter is designated by the bank to deposit money.
So, you go to a front office person who tells you to go to let’s say counter number 10 which only does money deposit.
So, here, the front office person who has stored the counter number 10 which is the address of the deposit counter. When you went to him, he pointed you to go to counter number 10 where you did the actual job which is of depositing money.
In a similar case, a pointer data type in C only stores a memory address of another variable location.
Do you remember the linking stage of a C program compilation where I briefed that this is the stage responsible to link to different dependent libraries to create the final executable?
The linking stage is where every defined variable in the program gets an actual address assigned to it which will be used throughout the program.
If you have used a pointer variable to point to any variable, then in the place of the pointer, the actual address of the variable will be used at the linking phase.
A pointer can be declared like this:
- int *pointer;
- char *char_pointer;
Pointer is a data type. As a pointer points to other data types, it is called as a derived data type.
Here, you just get a basic idea of what a pointer is. I will anyway explain in detail in a separate post.
Function in C
Function in C is the one which keeps C language as a modular language, meaning, you can divide the whole big program into sub programs and wrap under functions.
The way we declare a variable type such as int number; every function in C has a FIXED type which is called as a function prototype and that has to be declared before you can use the function in your C program.
Do you know why do we use #include <some_header_file.h> at the starting of a C program? Ans: simply to declare the function prototype which will be used by the compiler to understand when you use it in your C program.
Take a look at the Hello World C program where I have used #include <stdio.h> and I used the function printf() in my program. The stdio.h header file has the function prototype of printf().
The main function that we use also has a specific prototype which is as below:
int main ( int argc, char *argv[] )
But if you notice I used main in the above hello world program as this: int main () which is similar to int main ( void ) (I have covered the void data type in a later section)
There seems to be a conflict of main function declaration which is int main ( int argc, char *argv[] ) and the way I have used in the actual definition in my program int main ( void ).
In reality there is no conflict. The C compiler is told that the main function has a specific type but because of my usage of void, I am not using those arguments.
If this sounds confusing to you, you can ignore it right here. I will cover this in detail when we will cover the functions chapter.
Below are some other function protoype examples:
- void function_1 ( void ) – this function does neither takes any arguments nor returns anything
- void function_2 ( int ) – this function takes an integer argument but returns anything
- int function_3 ( int ) – this function takes an integer argument and returns an integer argument
These are some of the examples but there are some more complex examples of this as well which we will cover in detail in coming chapters.
In C, a function is also treated as a type of data and as it uses the base data types or other data types, it is also called as a derived data type.
Enumeration Data Type
Enumeration means counting or listing down the number of something. The same meaning is true with the enumeration data type in C.
Enumeration data type automatically assigns a specific value to an entry of enum variable in a list.
An enum can be declared like this: enum class { KG, ONE, TWO, THREE };
To use this enum data type you can declare another enum variable like enum class class_student = TWO; or you can simply assign this to another integer variable like this: int class = THREE;
- By default value of the first entry in the enum data type is 0 and it increases by one.
- Enum data type was specifically designed to group similar attributes or real life things which is meaningful from logical perspective or a human’s perspective.
- Unlike #define values, these enums are understood at compiler level hence using these variables enhance debugging ability.
- Using an enum variable gives a predictable value which is easy to understand and code unlike using hardcoded integer values or using macros via #define.
Void Data Type
Void as the meaning goes, means nothing.
What kind of data type or container in real life where we declare something as nothing type. I don’t know.
But in C, there are several places where void has a specific meaning.
As I have explained in the function data type section that a function may not be taking any arguments nor returning anything. In that case, void is used to convey about the same to the compiler.
In certain device driver entry code, where the kernel level subsystem just passes the driver structure, it uses the void data type as argument.
Later the driver code typecasts the void pointer to driver specific structure.
Right now, this may make no sense at all for you, but just remember that void has a very special place in C.
Data Type Conversion and Type Casting
We understand an integer and a character or a float number as different. But for the computer, everything is a presentation of 1s and 0s.
Because of this, it is possible that the computer can convert one value from one data type and store it in other data type.
It is very much valid. This is the reason we have to clearly understand what does the above two things mean.
Let’s take the below code for consideration:
int a = 65;
char ch = a ; // I am storing the int type variable a into a char type variable
I have declare “a” as an int variable and I am assigning it directly to a char variable “ch”. This is a valid statement from the compiler’s view.
But as the data types are different, the compiler will do a type conversion on its own to fit the int variable into the char variable.
Now take a look at the below example:
int a = 65;
char ch = (char) a ; // I am explicitly type casting the int variable to be copied as a char
In this case I have explicitly made a statement to convert the int variable a to char type. This is called as type casting.
The more experiment programs you do in this area, the more clarity you will eventually get on this concept.
Final Takeaway
Understanding data types with clarity is very important as you gradually move on in learning C programming language.
These are kind of the building blocks of the entire C programming. Of course it take time and a lot of practice to completely understand about all kinds of data types available in C.
I have made extensive attempt to give you real life examples so that you can relate in C programming.
The main aspects of understanding this concept in C is to use the right kind of data type when you are building your C program.
Got a question or have a doubt?
As a student or a beginner in C programming, you may have some doubts or questions.
I invite you to sign up and use the respective forum for data type discussions and create a topic, fill the details for which you seek the answers.
Below are the useful links for you when you need on C: