Learn Data Types in C Programming With Examples

Learn Data Types in C Programming With Examples

10 mins read1.9L Views Comment
clickHere
Updated on Dec 28, 2023 18:05 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.

2021_10_Copy-of-What-is-7.jpg

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.

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 functions 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:

  1. Name: String
  2. ID: Integer
  3. Salary: Float or Double
  4. Phone No: String 
Python vs. C++ – What’s the Difference?
Python vs. C++ – What’s the Difference?
Python and C++ both are general-purpose programming languages. However, their usage and syntax differ widely. In this article, we will highlight the most prominent differences between the two languages.
Difference between Malloc and Calloc
Difference between Malloc and Calloc
Malloc and Calloc are dynamic memory allocation methods in C language. You will learn the differences between these two. And will also see the applications of them. In this article...read more
If-else Statement in C
If-else Statement in C
If-else Statement in C is a very important topic in C language.This article will teach you this concept with flowcharts and implementation 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 Certificates

Primary Data Types In C

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:


 
#include <stdio.h>
void main()
{
int i = 5;
printf("The integer value is: %d \n", i);
}
Copy code

2. Character (char): Refers to all the ASCII character sets within single quotes such as ‘a’, ‘A’, etc.

Example:


 
#include <stdio.h>
void main()
{
char c = 'b';
printf("The character value is: %c \n", c);
}
Copy code

3. Floating-point (float): Refers to all the real number values or decimal points, such as 3.14, 10.09, 5.34, etc.

Example:


 
#include <stdio.h>
void main()
{
float f = 7.2357;
printf("The float value is: %f \n", f);
}
Copy code

4. Double (double): Used when the range exceeds the numeric values that do not come under either floating-point or integer data type. 

Example:


 
#include <stdio.h>
void main()
{
double d = 71.2357455;
printf("The double value is: %lf \n", d);
}
Copy code

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 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: 

Data Type Format Specifier Minimal Range Size in bit
unsigned char %c 0 to 255 8
char %c -127 to 127 8
signed char %c -127 to 127 8
int %d, %i -32,767 to 32,767 16 or 32
unsigned int %u 0 to 65,535 16 or 32
signed int %d, %i -32,767 to 32,767 (same as int) 16 or 32
short int %hd -32,767 to 32,767 16
unsigned short int %hu 0 to 65,535 16
signed short int %hd Same as short int 16
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 int %ld, %li Same as long int 32
unsigned long int %lu 0 to 4,294,967,295 32
unsigned longlong int %llu (2^63)-1 64
float %f 1E-37 to 1E+37 along with six digits of the precisions  32
double %lf 1E-37 to 1E+37 along with six digits of the precisions  64
long double %Lf 1E-37 to 1E+37 along with six digits of the precisions  80

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 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:


 
#include <stdio.h>
int main()
{
// the maximum value allowed in the int is 32,767
int x = 40000;
printf(x);
return 0;
}
Copy code

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

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:


 
#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]=40;
marks[4]=85; //traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]); }
return 0;
}
Copy code

Output

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


 
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; }
Copy code

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.

Example – Program to illustrate Structure


 
#include <stdio.h>
#include <string.h>
struct Employee { char name[50];
int emp_id;
float salary; } employee1;
int main() {
strcpy(employee1.name, "John");
employee1.emp_id = 1779;
employee1. salary = 3900;
printf("Name: %s\n", employee1.name);
printf("Employee ID: %d\n", employee1.emp_id);
printf("Salary: %.2f", employee1.salary);
return 0; }
Copy code

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 value at a time.

Example – Defining a Union


 
union Student{
int id;
char name[20];
float marks[5];
}
st1, st2;
Copy code

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 flag {const1, const2, const3………};

A popular example of enumerated data types is the days of the week.

Example – Program to illustrate Enumeration


 
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Fri;
printf("%d",day);
return 0;
}
Copy code

Output

4

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 function with no return value will have the return type as void.
  • Function arguments as void – A function with no parameter can accept the void.
  • Pointers to void – It represents the address of an object, but not its type.

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?

Data types are the basic building blocks of the C programming language. They store the value of variables and tell the computer system how to interpret its value. A data type determines the type and size of data associated with variables.

How many data types are in C programming?

There are four data types in C programming, namely Basic, Derived, Enumeration, and Void.

What do signed and unsigned signify in C programming?

In the C programming language, the signed modifier represents both positive and negative values while the unsigned modifier means all positive values.

Which are the basic data types in C programming?

There are four basic data types in C programming, namely Char, Int, Float, and Double.

What do signed and unsigned signify in C programming?

In the C programming language, the signed modifier represents both positive and negative values while the unsigned modifier means all positive values.

What are the different modifiers in the C language?

In C programming, there are four types of datatype modifiers, namelyu00a0signed,u00a0unsigned,u00a0long, andu00a0short. We can use data type modifiers with the basic data types to categorize them further. The signed represents + (positive) and - (negative) values while unsigned signifies only + (positive) values for any data type. Long and short modify the range of the values for any datatype. Long increases the current size of the data type while short is used to save memory space.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio