c++ length of an integer code example

Example 1: length of int c++

sizeof(num);

Example 2: intlen in c++

// getting the legth of an int by turning it into a string
// works with double and float too

#include <iostream>

int num = 0;

// turning num into a string using std::string
std::string temp = std::to_string(num);

// getting the length using .length()
int len = temp.length();

Example 3: c++ length of int

unsigned int number_of_digits = 0;

do {
     ++number_of_digits; 
     n /= base;
} while (n);

Tags:

Cpp Example