Site icon DataFlair

Learn Data Types in C and C++ with Examples and Quizzes 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:

This is just the beginning, at the end of this article, you will be an expert in Data Types in C and C++ with tested knowledge (quiz included at the end).

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-

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:

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:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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:

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 Type Memory (In Bytes) Format Specifiers Range
int 4 %d -2,147,483,648 to 2,147,483,647
short int 2 %hd -32,768 to 32,767
unsigned int 4 %u 0 to 4,294,967,295
unsigned short int 2 %hu 0 to 65,535
long int 4 %ld -2,147,483,648 to 2,147,483,647
unsigned long int 4 %lu 0 to 4,294,967,295
long long int 8 %lld -(263) to (263)-1
unsigned long long int 8 %llu 0 to 18,446,744,073,709,551,615
char 1 %c -128 to 127
Signed char 1 %c -128 to 127
Unsigned char 1 %c 0 to 255
float 4 %f
double 8 %lf
long double 12 %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.

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-

And, the output you will get-

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-

Output-

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:

The basic syntax of declaring an array is:

return_type array_name[size]];

For instance: float marks[5];

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

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 Specifiers Data Type Elucidation
%d, %i int
short
long
unsigned short
Used for integers
%c char
signed char
unsigned char
Used for characters with any type modifier
%f float Used for decimal values
%e, %E, %g, %G float
double
Used for scientific notation of decimal values
%hi short Used for signed short integers
%hu unsigned short Used for unsigned short integers
%l, %ld, %li long Used for signed integers
%lf double Used for floating-point
%Lf long double Used for floating-point
%lu unsigned int
unsigned long
Used for unsigned integers
%lli, %lld long long Used for signed long integers
%llu unsigned long long Used for unsigned long long integers
%s char * Used for a string
%p void * Used when finding the address of the pointer to void *
%o int
short
long
unsigned short
unsigned int
Used for the octal representation of Integer.
%u unsigned int
unsigned long
Used for unsigned integers
%x, %X int
short
long
unsigned short
unsigned int
Used for the hexadecimal representation of Unsigned Integer
%% Used to print % character
%n Used to print nothing

Quiz on Datatypes in C – C++

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    Predict the output of the following C/C++ Program:
    #include
    #include
    using namespace std;
    int main()
    {
    double N = 167.8;
    cout<<fixed<<setprecision(3)<<N;
    return 0;
    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    Predict the output of the following C/C++ Program:
    #include
    #include
    using namespace std;
    int main()
    {
    double N = 167.8;
    cout<<fixed<<setprecision(3)<<N;
    return 0;
    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    Predict the output of the following C/C++ Program:
    #include
    int main()
    { int i = 5437819;
    signed j=0;
    unsigned u=0;
    long l=50000;
    short s;
    j=i; u=j; l=u; s=l; i=s;
    std::cout<<i<<std::endl<<j<<std::endl;
    std::cout<<u<<std::endl<<l<<std::endl<<s<<std::endl<<i;
    return 0;
    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    Predict the output of the following C/C++ Program:
    #include
    int main()
    {
    int i = 786;
    int &j = i;

    std::cout<<i<<std::endl<<j<< std::endl<<&i;
    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    Predict the output of the following C/C++ Program:
    #include
    int main()
    {
    int i = 519;
    int *j = &i;
    if(i==*j)
    std::cout<<"Mango"<<std::endl;
    if(j==&i)
    std::cout<<"Orange"<<std::endl;
    else
    std::cout<<"Papaya"<<std::endl;
    }

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    Predict the output of the following C/C++ Program:
    #include
    int main()
    {
    int i = 84;
    int *j = &i;
    int &k = i;
    k = 100;
    std::cout<<i<<std::endl<<j<<std::endl<<k<<std::endl;
    }

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    Predict the output of the following C/C++ Program:
    #include
    using namespace std;
    int main()
    {
    char arr[2] = “GO”;
    char arr1[4] = “RUN”;
    cout<<arr<<arr[1]<<arr1<<endl;
    }

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    Predict the output of the following C/C++ Program:
    #include
    using namespace std;
    int main()
    {
    char arr[5] = “RUN”;
    char arr1[5] = “FAST”;
    cout<<arr<<arr[5]<<arr1<<endl;
    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    Predict the output of the following C/C++ Program:
    #include
    using namespace std;
    enum colours {black =100, blue};
    int main()
    {
    colours C, D, E;
    C =blue;
    D =black;
    cout<<C<<D;
    return 0;
    }

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    Predict the output of the following C/C++ Program:
    #include
    using namespace std;
    int main()
    {
    typedef char Int;
    Int a=80, b=10;
    a=a+b;
    cout<<a;
    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    Memory Space required by integer Ranges between:

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

    What is true for Structure but not for Union?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    What is the difference between Wide char and char?

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

    What is the main benefit of using wide char?

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    Which of the below points is not a usage of void data type?

    Correct
    Incorrect

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.

Exit mobile version