Learn Data Types in C Programming With
Examples
Shiksha Online
Updated on Jun 21, 2024 15:48 IST
Data types are the type of data stored in a C program. Data types are used
while defining a variable or functions in C. It’s important for the compiler to
understand the type of predefined data it is going to encounter in the
program. In this article, we will discuss Data type in C programming with
example.
C is a powerful programming language for developing operating systems,
databases, etc. It is an excellent language to learn for beginners. You will need to
have a good understanding of C data types to work with the C programming
language . We need to specify the data type when defining a variable in a C
program. This helps the compiler know what data type to expect and which
operations can be performed. In this blog, we will learn what data types are in c
programming and look at different data types in C programming.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Explore: Free C Programming Courses
What is Data Type in C?
Data types are the type of data stored in a C program. Data types are used while
defining a variable or f unctions in C. The compiler needs to understand the
type of predefined data it will encounter in the program. A data type is an attribute
that tells a computer how to interpret the value.
C provides several built-in data types, such as integer (int), character (char), floating-
point (float), and double-precision floating-point (double), among others. Each data
type has its own set of possible values and operations that can be performed on it.
Let’s say if variables are containers, then the data type is the type of container. The
type of container tells what kind of stuff it should contain. For example, you won’t
put cookies in a bottle. Right! Similarly, you don’t store an integer value in a variable
of data type String.
Crack the code of C/C++ with our comprehensive guide; find details on top
colleges, programmes and online courses.
Example Of Data Types In C
Let’s consider a scenario of a company. A company stores various data of their
employee such as Name, Employee ID, Age, Salary, Address, Phone No, etc.
Now, these data are values containing alphabets, numbers, etc, so to make the
processing of these huge data for programs easy, the information was categorized
into different types:
Name: String
ID: Integer
Salary: Float or Double
Phone No: String
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Pyt hon vs. C++ – What ’s t he Dif f erence?
Pytho n and C++ bo th are general-purpo se pro gramming languages. Ho wever,
their usage and syntax differ widely. In this article, we will highlight the mo st
pro minent differences between the two languages.
Dif f erence bet ween Malloc and Calloc
Mallo c and Callo c are dynamic memo ry allo catio n metho ds in C language. Yo u
will learn the differences between these two . And will also see the applicatio ns
o f them. In this article...re ad m o re
If -else St at ement in C
If-else Statement in C is a very impo rtant to pic in C [Link] article will
teach yo u this co ncept with flo wcharts and implementatio n in C language.
Types Of Data Types In C
There are majorly five main categories of Data Type in C:
Data Type Example of Data Type
Primary Data Type Integer, Floating-point, double, string.
Derived Data Type Union, structure, array, etc.
Enumerated Data Type Enums
Void Data Type Empty Value
Bool Type True or False
Must Check: Top C Program Online Course and Certif icates
Primary Data Types In C
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
The C programming language has five primitive or primary data types.
1. Integer (int): Refers to positive and negative whole numbers (without decimal),
such as 10, 12, 65, 3400, etc.
Example:
Copy code
#include < stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
}
2. Character (char): Refers to all the ASCII character sets within single quotes
such as ‘a’, ‘A’, etc.
Example:
Copy code
#include < stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
3. Floating-point (f loat): Refers to all the real number values or decimal points,
such as 3.14, 10.09, 5.34, etc.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Example:
Copy code
#include < stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
4. Double (double): Used when the range exceeds the numeric values that do
not come under either floating-point or integer data type.
Example:
Copy code
#include < stdio.h>
void main()
{
double d = 71.23574 55;
printf("The double value is: %lf \n", d);
}
Also Read: Tokens in C Programming
Also Read: Understanding Pointers in C
Data Type Modifiers In C
Modifiers are C keywords that modify the meaning of fundamental data types. It
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
indicates how much memory will be allocated to a variable. Modifiers are prefixed
with fundamental data types to adjust the memory allocated for a variable. C
Programming Language has four data type modifiers:
long
short
signed
Unsigned
These modifiers make the memory required for primary data types more precise.
Size Of Data Types In C
The size of each data type is defined in bits or bytes (8 bits). Each data type in C is
associated with a specific range of values defined as below:
Format Size
Data Type Minimal Range
Specif ier in bit
unsigned
%c 0 to 255 8
char
char %c -127 to 127 8
signed char %c -127 to 127 8
16 or
int %d, %i -32,767 to 32,767
32
16 or
unsigned int %u 0 to 65,535
32
16 or
signed int %d, %i -32,767 to 32,767 (same as int)
32
short int %hd -32,767 to 32,767 16
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
unsigned
%hu 0 to 65,535 16
short int
signed short
%hd Same as short int 16
int
long int %ld, %li -2,147,483,647 to 2,147,483,647 32
long long int %lld, %lli -(2^63) to (2^63)-1 64
signed long
%ld, %li Same as long int 32
int
unsigned
%lu 0 to 4,294,967,295 32
long int
unsigned
%llu (2^63)-1 64
longlong int
1E-37 to 1E+37 along with six
f loat %f 32
digits of the precisions
1E-37 to 1E+37 along with six
double %lf digits of the precisions 64
1E-37 to 1E+37 along with six
long double %Lf 80
digits of the precisions
Fun Fact: Double is called double because it can hold double the float values.
NOTE: Format specifiers are used while printing the value of a variable within the
printf() statement.
Must Read: Top 10 Programming Languages to Learn in 2023
Understanding Data Type In C In Terms Of Memory
A data type reserves a chunk of memory to store and represent a value. A single
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
byte consists of 8 bits of memory. Consider the below representation of byte, where
each bit is represented by an underscore (_):
byte: _ _ _ _ _ _ _ _ < – 8 bits
Since we have 8 positions, we can input either a 0 or 1. So we can have a
combination of 2^8 or 256 distinct values, which can be represented from the 8 bits,
which is the overall range of a byte.
byte: 0 0 0 0 0 0 0 0 <- Represents “0”
byte: 0 0 0 0 0 0 0 1 <- Represents “1”
.. so on ..
byte: 1 1 1 1 1 1 1 0 <- Represents “254”
byte: 1 1 1 1 1 1 1 1 <- Represents “255”
Similarly, we have,
int: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ < – 16 bits
long: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ < – 32 bits
Data Type Value Out Of Range
Whenever you try to add value that is outside the range of the data type. The C
compiler would throw an error.
Example:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Copy code
#include < stdio.h>
int main()
{
// the maximum value allowed in the int is 32,767
int x = 4 0000;
printf(x);
return 0;
}
Output:
Segmentation Fault
What is a Segmentation Fault?
A segmentation fault occurs when your program tries to access an area of memory
that is not allowed. In other words, this error is thrown when the program tries to
access memory beyond the allocated space for the specific data type.
Derived Data Types
Derived data types are primary data types that are grouped together. You can group
many elements of similar data types. These data types are defined by the user. The
following are the derived data types in C:
Array
Pointers
Structure
Union
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Array
An array in C is a collection of multiple values of a similar data type and is stored in a
contiguous memory location. An array can consist of chars, integers, doubles, etc.
Declaration of Array in C
data_type array_name[array_size];
C Array Example :
Copy code
#include< stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
clrscr();
marks[0]=50;//initialization of array
marks[1]=60;
marks[2]=75;
marks[3]=4 0;
marks[4 ]=85; //traversal of array
for(i=0;i< 5;i++){
printf("%d \n",marks[i]); }
return 0;
}
Output
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
50
60
75
40
85
Also Read: All About While and Do… While Loop in C
Pointer Data Type
The pointer data type is used to store the address of another variable. A pointer can
store the address of variables of any data type. Pointers allow users to perform
dynamic memory allocation. They also help to pass variables by reference.
A pointer with no address is called a null pointer. A pointer with no data type is a void
Pointer. It is defined by using a ‘*’ operator.
Example – Program to illustrate Pointer
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Copy code
int main(void) {
int *ptr1;
int *ptr2;
int a = 5;
int b = 10;
//address of a is assigned to ptr1
ptr1 = &a;
//address of b is assigned to ptr2
ptr2 = &b;
//display value of a and b
printf("%d", *ptr1); //prints 5
printf("\n%d", *ptr2); //prints 10
//print address of a and b
printf("\n%d", ptr1); // prints address
printf("\n%d", ptr2); // prints address
//pointer subtraction
int minus = ptr2 - ptr1;
printf("\n%d", minus); //prints the difference
return 0; }
Also Read: Introduction to Python Data Types with Examples
Structure
It is a data type that can store variables of similar or different data types. For
example, we can use structures to store information about an employee, such as
the employee’s name, employee ID, salary, and more. Each employee’s record will be
represented by an object of the structure. The size of the structure is the sum of
the storage size required by each variable. The ‘struct’ keyword defines a structure.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Example – Program to illustrate Structure
Copy code
#include < stdio.h>
#include < string.h>
struct Employee { char name[50];
int emp_id;
float salary; } employee1;
int main() {
strcpy([Link], "John");
employee1.emp_id = 1779;
employee1. salary = 3900;
printf("Name: %s\n", [Link]);
printf("Employee ID: %d\n", employee1.emp_id);
printf("Salary: %.2f", [Link]);
return 0; }
Output
Name: John
Employee ID: 1779
Salary: 3900.00
Also Read: Top C Programming Interview Questions and Answers
Union
A union is a group of elements with similar or different data types. In a union, the
memory location is the same for all the elements. Its size will be equal to the memory
required for the largest data type defined. We use the keyword ‘union’ to define a
union. You can declare many variables. However, just one variable can store the
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
value at a time.
Example – Def ining a Union
Copy code
union Student{
int id;
char name[20];
float marks[5];
}
st1, st2;
Enumerated Data Types
Enumerated data types are user-defined data types that consist of integer values.
They are used to define variables that can only assign certain discrete integer values
in the program. They are used to make a program more readable, flexible, and
maintainable. We use the keyword ‘enum’ to declare new enumeration types in the C
programming language.
Enum syntax:
enum f lag {const1, const2, const3………};
A popular example of enumerated data types is the days of the week.
Example – Program to illustrate Enumeration
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Copy code
#include< stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Fri;
printf("%d",day);
return 0;
}
Output
Check out the top Programming Courses
Void
The void is just an empty data type that depicts that no value is available. Typically,
the void is used for functions. When we declare a function as void, it doesn’t have
to return anything.
Void is used in three situations:
Function returns as void – A f unction with no return value will have
the return type as void.
Function arguments as void – A f unction with no parameter can
accept the void.
Pointers to void – It represents the address of an object, but not its
type.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.
Example – The below function will not return any value to the calling function.
void sum (int a, int b);
Also Read: Data Types in Java
Conclusion
In this article, we learned about different data types in C with examples. We
also discussed the examples of each data type. We hope this information about C
data types will help you create efficient programs in C programming.
FAQs
What are data types in the C language?
How many data types are in C programming?
What do signed and unsigned signif y in C programming?
Which are the basic data types in C programming?
What do signed and unsigned signif y in C programming?
What are the dif f erent modif iers in the C language?
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 22-Jun-20 24.