C++ Integer Types

Introduction

We use integer type to store integral values, or whole numbers, both negative and positive:

#include <iostream> 

int main() //from   w  ww.  j  a v  a 2  s.com
{ 
    int x = 123; 
    int y = -256; 

    std::cout << "The value of x is: " 
              << x 
              << ", the value of y is: " 
              << y; 
} 

Here we declared and initialized two variables of type int.

The size of int is usually 4 bytes.

We can also initialize the variable with another variable.

It will receive a copy of its value.

We still have two separate objects in memory:

#include <iostream> 

int main() //  w ww.  j  a v  a 2  s  .  co m
{ 
    int x = 123; 
    int y = x; 
    std::cout << "The value of x is: " << x << " ,the value of y is: " << y; 
    // x is 123 
    // y is 123 
    x = 456; 
    std::cout << "The value of x is: " << x << " ,the value of y is: " << y; 
    // x is now 456 
    // y is still 123 
} 



PreviousNext

Related