Learn Data Types in C and C++ with Example in Just 4 mins.

A programming language cannot work without a wide range of data types as each type has its own significance and utility to perform various tasks. Here, reasons are mentioned why we require different data types in C and C++ Programming:

  • At the time of the variable declaration, it becomes convenient for the user to distinguish which type of data variable stores.
  • It makes it clear for the user to identify the return value of the function based on the data type.
  • If parameters are passed to the function, it becomes easy for the user to give input according to the given format.

This is just the beginning, at the end of this article, you will be an expert in Data Types in C and C++.

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. What is Data Type in C/C++?

Data types in C and C++ refer to the characteristics of data stored into a variable. For instance, while working with mathematical problems, in order to simplify things for us, we look for a specific type of data, let’s say, we want to find the factorial of a number. We know that only for whole numbers, the factorial of that number exists which is also a whole number. In order to eliminate all scopes of errors and reduce run-time of the program, we would preferably assign such a data type to the input and output such that it only covers the range of whole numbers. Voila! This is how you begin to think to increase your program efficiency and well utilize the features offered by C.

Clearly, from the above discussion, you might have inferred that memory occupied by different data types would be different. Therefore, a different amount of space in the computer memory would be allocated for them and hence the run time of the program would be reduced, increasing the efficiency of the program.

2. Types of Data Types in C and C++

According to the conventional classification, these are data types in C language-

Data Types in C and C++

2.1 Primary Data Types in C and C++

Primary (Fundamental) data types in C programming includes the 4 most basic data types, that is:

  • int: It is responsible for storing integers. The memory it occupies depends on the compiler (32 or 64 bit). In general, int data type occupies 4 bytes of memory when working with a 32-bit compiler.
  • float: It is responsible for storing fractions or digits up to 7 decimal places. It is usually referred to as a single-precision floating-point type. It occupies 4 bytes of memory
  • char: It can be used to store a set of all characters which may include alphabets, numbers and special characters. It occupies 1 byte of memory being the smallest addressable unit of a machine containing a fundamental character set.
  • double: It is responsible for storing fractions or digits up to 15-16 decimal places. It is usually referred to as a double-precision floating-point type.
  • void (Null) data type: It indicates zero or no return value. It is generally used to assign the null value while declaring a function.

Learn the Samurai Technique to implement Arrays in C

In C++, in addition to the primary data types available in C, there are few more data types available in the C++ programming language.

They are:

  • bool: It refers to a boolean/logical value. It can either be true or false.
  • wchar_t: It refers to a wide character whose size is either 2 or 4 bytes. It is similar to the char data type but the only difference is the space occupied in the computer memory.
  • string: Instead of declaring an array of characters to enter a string data type, C++ gives you the provision to declare the “string” data type.

Along with data types, comes modifiers. Modifiers basically alter the function of a data type and make it more specific by the inclusion of additional provisions. These include:

  • Signed: It is used to store zero, positive or negative values.
  • Unsigned: It can store only zero or positive values.
  • Long:  It is used to store large integer numbers. The size of the long type is 8 bytes.
  • Short: It is used to store small integer numbers. Its size is of 2 Bytes.

Primary Data Types in C

Here is a table that would specify the range of various data primary data types along with their modifiers. It is in accordance with a 32-bit compiler.

Data TypeMemory (In Bytes)Format SpecifiersRange
int4%d-2,147,483,648 to 2,147,483,647
short int2%hd-32,768 to 32,767
unsigned int4%u0 to 4,294,967,295
unsigned short int2%hu0 to 65,535
long int4%ld-2,147,483,648 to 2,147,483,647
unsigned long int4%lu0 to 4,294,967,295
long long int8%lld-(263) to (263)-1
unsigned long long int8%llu0 to 18,446,744,073,709,551,615
char1%c-128 to 127
Signed char1%c-128 to 127
Unsigned char1%c0 to 255
float4%f
double8%lf
long double12%Lf

In order to compute the size of a variable, we can use the function sizeof() operator
Its output is of the form of an unsigned integer.

Take a break for a while and learn Variables in C with examples

Example of Primary Data Types in C

Let’s understand with an example of Data types in C-

#include <stdio.h>
int main()
{
int number1 = 400;
short int number2 = 500;
unsigned short int number3 = 600;
long int number4 = 700;
unsigned long int number5 = 800;
unsigned long long int number6 = 900;
char character1 ='A';
signed char character2 ='B';
unsigned char character3 ='C';
float digit1 =20.00;
double digit2 = 3.14159;
long double digit3 = 1.414213;

printf("Welcome to DataFlair tutorials!\n\n");
 
//Print statements to show the size of various data types

printf("The size of int data type %d is: %lu bytes.\n", number1,sizeof(number1));
printf("The size of short int data type %d is: %lu bytes.\n", number2,sizeof(number2));
printf("The size of unsigned short int data type %d is: %lu bytes.\n", number3,sizeof(number3));
printf("The size of long int data type %ld is: %lu bytes.\n", number4,sizeof(number4));
printf("The size of unsigned long int data type %ld is: %lu bytes.\n", number5,sizeof(number5));
printf("The size of unsigned long long int data type %lld is: %lu bytes.\n", number6,sizeof(number6));
printf("The size of char %c is: %lu byte.\n", character1,sizeof(character1));
printf("The size of signed char %c is: %lu byte.\n", character2,sizeof(character2));
printf("The size of unsigned char %c is: %lu byte.\n", character3,sizeof(character3));
printf("The size of float data type %f is: %ld bytes.\n", digit1,sizeof(digit1));
printf("The size of double data type %lf is: %ld bytes.\n", digit2,sizeof(digit2));
printf("The size of long double data type %Lf is: %ld bytes.\n", digit3,sizeof(digit3));
return 0;    
}

The above example on your screen looks like-

Example of Primary Data Type in C

And, the output you will get-

Output of primary data types in C

Still, can’t get the example, learn the Syntax of C Language.

Example of Primary Data Types in C++

Here is an illustrated C++ code which will help you to find the memory occupied by various data types:

#include <iostream>
using namespace std;

int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int number1 = 400;
short int number2 = 500;
unsigned short int number3 = 600;
long int number4 = 700;
unsigned long int number5 = 800;
unsigned long long int number6 = 900;
char character1 ='A';
signed char character2 ='B';
unsigned char character3 ='C';
float digit1 =20.00;
double digit2 = 3.14159;
long double digit3 = 1.414213;
string word = "DataFlair";// 9 characters
bool flag = 0;
 
//Print statements to show the size of various data types

cout<<"The size of int data type "<< number1 << " is: " << sizeof(number1) << " bytes."<<endl;
cout<<"The size of short int data type "<< number2 << " is: " << sizeof(number2) << " bytes."<<endl;
cout<<"The size of unsigned short data type "<< number3 << " is: " << sizeof(number3) << " bytes."<<endl;
cout<<"The size of long int data type "<< number4 << " is: " << sizeof(number4) << " bytes."<<endl;
cout<<"The size of unsigned long int data type "<< number5 << " is: " << sizeof(number5) << " bytes."<<endl;
cout<<"The size of long long int data type "<< number6 << " is: " << sizeof(number6) << " bytes."<<endl;
cout<<"The size of char data type "<< character1 << " is: " << sizeof(character1) << " bytes."<<endl;
cout<<"The size of signed char data type "<< character2 << " is: " << sizeof(character2) << " bytes."<<endl;
cout<<"The size of unsigned char data type "<< character3 << " is: " << sizeof(character3) << " bytes."<<endl;
cout<<"The size of float data type "<< digit1 << " is: " << sizeof(digit1) << " bytes."<<endl;
cout<<"The size of double data type "<< digit2 << " is: " << sizeof(digit2) << " bytes."<<endl;
cout<<"The size of long double data type "<< digit3 << " is: " << sizeof(digit3) << " bytes."<<endl;
cout<<"The size of string data type "<< word << " is: " << sizeof(word) << "bytes. "<<endl;
cout<<"The size of bool data type "<< word << " is: " << sizeof(word) << "bytes. "<<endl;

return 0;    
}

Code-

Example of Primary Data Types in C++

Output-

Output of Primary Data Types in C++

2.2 Secondary (Derived) Data Types in C and C++

As the name itself suggests, they are derived from the fundamental data types in the form of a group to collect a cluster of data used as a single unit. These include:

  • Arrays: A collection of data items of similar data types, which is accessed using a common name.

The basic syntax of declaring an array is:

return_type array_name[size]];

For instance: float marks[5];

  • Pointers: Pointers in C/C++ are basically variables that are used to store the memory address of another variable.
  • Functions: It is a group of statements that are written to perform a specific task. Functions are either user-defined or built-in library functions.

2.3 User-defined Data Types in C and C++

  • Structures – It is a user-defined data type in which a collection of different data types can be made and accessed through an object.
  • Union– A special kind of data type which gives us the provision to store different data types in the same memory location.
  • Enumeration – It refers to the arithmetic data types used to define variables to specify only certain discrete integer values throughout the entire program.

Key takeaway: Arrays and structures in C are collectively called aggregates.

Format Specifiers C

It is important to note that format specifiers is an exclusive feature only available in C, not in C++.

Often referred to as format string, it is an exclusive feature of the C language. It is associated with data types as it defines the type of data to be taken as input or printed when using the I/O statements.

Here is a table that would help you to explore a wide range of format specifiers used for various purposes.

Format SpecifiersData TypeElucidation
%d, %iint
short
long
unsigned short
Used for integers
%cchar
signed char
unsigned char
Used for characters with any type modifier
%ffloatUsed for decimal values
%e, %E, %g, %Gfloat
double
Used for scientific notation of decimal values
%hishortUsed for signed short integers
%huunsigned shortUsed for unsigned short integers
%l, %ld, %lilongUsed for signed integers
%lfdoubleUsed for floating-point
%Lflong doubleUsed for floating-point
%luunsigned int
unsigned long
Used for unsigned integers
%lli, %lldlong longUsed for signed long integers
%lluunsigned long longUsed for unsigned long long integers
%schar *Used for a string
%pvoid *Used when finding the address of the pointer to void *
%oint
short
long
unsigned short
unsigned int
Used for the octal representation of Integer.
%uunsigned int
unsigned long
Used for unsigned integers
%x, %Xint
short
long
unsigned short
unsigned int
Used for the hexadecimal representation of Unsigned Integer
%%Used to print % character
%nUsed to print nothing

Summary

Data types provide a backbone to the programming language, as it helps the user to differentiate between the type of data variable stores at the time of the variable declaration itself. Additionally, while writing the logic of the program, it becomes clear for the user to identify the return value of the function based on the data type. Not only this, by specifying the data type of the parameters passed to the function, it becomes easy for the user to give input according to the given format.

Now, that you have mastered the Data Types in C and C++, let’s move towards the next topic – Operators in C with examples. All the feedback and suggestions are welcome.

2 Responses

  1. Clim jark says:

    That example is incorrect, the C standard states the main function MUST return an integer. Section 5.1.2.2.1 any compiler doing otherwise is not writing c code at all

    • DataFlair Team says:

      Hello Clim,
      Thank you for the information, we changed all the examples of our C tutorials. Now, readers can learn and execute all the code hassle free.
      Keep learning and keep visiting DataFlair

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.