Working with Different Types of Integers - C++ Data Type

C++ examples for Data Type:int

Introduction

The C++ integer types

Type Name Number of Bytes Minimum Value Maximum Value
char1-128 127
short 2-32,76832,767
int4-2,147,483,648 2,147,483,647
long 4-2,147,483,648 2,147,483,647
long long 8-9,223,372,036,854,775,808 9,223,372,036,854,775,807

The cinttypes integers

Type Name Number of Bytes Minimum Value Maximum Value
int8_t 1-128 127
int16_t 2 -32,768 32,767
int32_t 4-2,147,483,648 2,147,483,647
int64_t 8 -9,223,372,036,854,775,8089,223,372,036,854,775,807

Using the int32_t type with operators

Demo Code

#include <iostream>
#include <cinttypes>

using namespace std;

int main(int argc, char* argv[])
{
    int32_t whole32BitNumber1{ 64 };
    cout << "whole32BitNumber1 equals " << whole32BitNumber1 << endl;

    int32_t whole32BitNumber2{ whole32BitNumber1 + 32 };
    cout << "whole32BitNumber2 equals " << whole32BitNumber2 << endl;

    int32_t whole32BitNumber3{ whole32BitNumber2 - whole32BitNumber1 };
    cout << "whole32BitNumber3 equals " << whole32BitNumber3 << endl;

    int32_t whole32BitNumber4{ whole32BitNumber2 * whole32BitNumber1 };
    cout << "whole32BitNumber4 equals " << whole32BitNumber4 << endl;

    int32_t whole32BitNumber5{ whole32BitNumber4 / whole32BitNumber1 };
    cout << "whole32BitNumber5 equals " << whole32BitNumber5 << endl;

    int whole32BitNumber6{ whole32BitNumber2 % whole32BitNumber1 };
    cout << "whole32BitNumber6 equals " << whole32BitNumber6 << endl;

    return 0;// w  w  w . j a  v a2  s. c  o  m
}

Result


Related Tutorials