Introduction to Data Types in C
C is a compact, general-purpose computer programming language that was developed by Dennis Ritchie for Unix operating system at bell laboratories. C is a structured programming language that is machine-independent. C has been used by many organizations for developing operating systems, interpreters, device drivers, also database oracle is written in C and in the modern era, the embedded system designs and IoT development also use C language. C is a compiled language in which the compiler takes responsibility to convert the source code into machine-readable object code. There are various compilers available like – TurboC, Clang, etc.
Types of Data Types in C
- Whenever a variable is defined in C, it has to be associated with a certain data type.
- This gives an indication about the amount of memory to be allocated to that variable and each variable will hold its own unique memory location, except for some cases where the variables point to same memory location only
- C has categorized the data types into:
- a. Primary data types
- b. Derived data types
a. The primary data types are also called as primitive data types and they include the following :
- Int
- Float
- Char
- Void
b. The derived data types can be among the following :
- Array
- Structure
- Union
- Pointer
Lets now Describe all of them with examples
1. The integer data type (int) : If you have to store the whole numbers then int can be used as a data type, it can have a range of numbers based upon size you choose in memory and it can have either all positive or from negative to positive range of numbers based upon user choice of code design.
Int type | Size( in bytes) | Range allowed |
int or signed int | 2 | -32,768 to 32767 |
unsigned int | 2 | 0 to 65535 |
short int or signed short int | 1 | -128 to 127 |
unsigned short int | 1 | 0 to 255 |
long int or signed long int | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 | 0 to 4,294,967,295 |
For example
Code:
#include <stdio.h>
void main()
{
int a = 1;
printf(" %d is the integer value ",a);
unsigned short int x = -3278989;
printf(" %hu is the integer value ",x);
}
Output:
2. Float data type: Any real number can be stored in the float data type and here also we can specify the range, based on data type and size selection, a range of numbers is allowed.
4.5 (5,617 ratings)
View Course
Float Type | Size( in bytes) | Range of Float |
Float | 4 | 3.4E-38 to 3.4E+38 |
double | 8 | 1.7E-308 to 1.7E+308 |
long double | 10 | 3.4E-4932 to 1.1E+4932 |
For example
Code:
#include <stdio.h>
#include <limits.h>
#include <float.h>
void main() {
printf("max float value allowed in positive range : %g\n", (float) FLT_MAX);
printf("max float value allowed in negative range : %g\n", (float) -FLT_MAX);
printf("max double value possible in positive range : %g\n", (double) DBL_MAX);
printf("max double value possible in negative range : %g\n", (double) -DBL_MAX);
}
Output :
3. Char type: This represents the character data type and it can be either signed or unsigned with a constant size of 1 byte for both cases.
Char Type | Size( in bytes) | Range of char |
char or signed char | 1 | -128 to 127 |
unsigned char | 1 | 0 to 255 |
For example
Code:
#include <stdio.h>
void main() {
char c ='a';
char f = 65; // represents ASCII char value, refer to ASCII table
printf("%c %c ", c, f);
}
Output:
4. Void type: If you don’t want to assign any type to a function (i.e. it won’t return anything like you saw the main function prefixed with void type in above snippets), then you can mark it as void type.
The above snippets can be referred to as examples for the same.
5. Arrays: When any homogenous set of data has to be stored in contiguous memory locations then this data type is chosen, use case is that, there may be times when your code would return more than one result and that has to be returned from functions cumulatively, like if we have to find list of all months in a year then they will be 12, hence we can’t place 12 months discretely in a single variable, so we use arrays for the same.
Let’s see a simple snippet to understand the declaration and use of arrays.
For example
Code:
#include <stdio.h>
void main() {
int i;
char arr[] = {'a', 'b', 'c'};
for(i = 0 ; i < 3 ; i++)
{
printf("%c\n",arr[i]);
}
}
Output:
6. Structures: If there is a requirement, where you need to represent any physical world structure into coding world then this type could come handy, like class of students can be defined as a structure and student marks and student roll number can be used as variables inside it, an array can be introduced which could hold data related to such structure for many students.
For example
Code:
#include <stdio.h>
struct class{
int marks;
int rollNo;};
void main() {
struct class c;
c.marks=10;
c.rollNo=1;
printf("%d\n", c.marks);
printf("%d", c.rollNo);
}
Output:
7. Pointer: This is one of the most important data types as we are not into the OOPs world in C language, languages like java do not use it but functional programming languages always use it. The concept of pointers is to allocate the memory to some variable and then refer to that memory location for reading and write operations, that memory location can be the address of a function, can be the address of a variable, etc. Pointers get necessary for Array and structure handling in C language and also provides dynamic memory management.
For example
Code:
#include <stdio.h>
void main() {
int a, *p; // variable and pointer declaration
a = 10;
p = &a;
printf("%d", *p); // print the value of 'a'
printf("%u", &a); //print the address of 'a'
printf("%u", p); // print the address of 'a' in different way
// remember & represents address of variable
}
Output:
Conclusion
Hence we saw various data types in C and how they work along with C language to handle coding scenarios. You can do embedded programming also with C, as utilities for the same have been developed too. So C is a versatile language, but with real-world scenarios, coding gets complex and more involved.
Recommended articles
This is a guide to Data type in C. Here we discuss the basic concept, different types of data with respective examples and code implementation. You can also go through our other suggested articles to learn more –