Lenght of int type

Nov 26, 2017 at 9:50am
Hello, can someone one tell me what is the easiest way to find the length of int type?
Nov 26, 2017 at 11:49am
Hello Delcho,

This should give you everything you need to know:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	std::cout << "bool:\t\t" << sizeof(bool) << " bytes" << std::endl;
	std::cout << "char:\t\t" << sizeof(char) << " bytes" << std::endl;
	std::cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << std::endl;
	std::cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
	std::cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << std::endl; // C++11, may not be supported by your compiler
	std::cout << "short:\t\t" << sizeof(short) << " bytes" << std::endl;
	std::cout << "int:\t\t" << sizeof(int) << " bytes" << std::endl;
	std::cout << "long:\t\t" << sizeof(long) << " bytes" << std::endl;
	std::cout << "long long:\t" << sizeof(long long) << " bytes" << std::endl; // C++11, may not be supported by your compiler
	std::cout << "float:\t\t" << sizeof(float) << " bytes" << std::endl;
	std::cout << "double:\t\t" << sizeof(double) << " bytes" << std::endl;
	std::cout << "long double:\t" << sizeof(long double) << " bytes" << std::endl;

	return 0;
}


This is old code, so some of the comments may not apply.

Hope that helps,

Andy

P.S. The sizes may be different on different computers.
Last edited on Nov 26, 2017 at 11:50am
Nov 26, 2017 at 11:52am
Thnks but somehow this doesn't work for me for example
1
2
int a=4; //lenhgt=1
cout<<sizeof(a)<<endl;  //it displays 4 not 1 
Nov 26, 2017 at 12:08pm
It means that an int takes up 4 bytes of memory.
Nov 26, 2017 at 12:08pm
Hello Delcho,

That is because the "sizeof()" is returning the size of "a" which is an "int" and on you computer an "int" is worth 4 bytes.

Change "a" to = 100 and see what you get. It would still say 4. Your example is a little misleading because you set "a" is equal to 4.

Hope that helps,

Andy
Last edited on Nov 26, 2017 at 12:09pm
Nov 26, 2017 at 12:13pm
Yes, I saw that but I want to understand how to find the length of an int type.
If we have variable a=123456 I want to understand its length i.e that it is six characters long.
Is there a function for this like the .size() for strings.
Nov 26, 2017 at 12:20pm
No, but you can convert it to a string an get the length of it.
1
2
3
int a = 500468;
std::string len = std::to_string(a);
std::cout << len.length();
Nov 26, 2017 at 12:31pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int intLength( int N )
{
   if      ( N < 0  ) return 1 + intLength( -N );
   else if ( N < 10 ) return 1;
   else               return 1 + intLength( N / 10 );
}

int main()
{
   int a[] = { 123456, -345, 9, -5, 0 };
   for ( int N : a ) cout << "The length of " << N << " is " << intLength( N ) << endl;
}
The length of 123456 is 6
The length of -345 is 4
The length of 9 is 1
The length of -5 is 2
The length of 0 is 1
Last edited on Nov 26, 2017 at 1:29pm
Nov 26, 2017 at 12:32pm
Thanks!
Nov 26, 2017 at 12:35pm
Hello, can someone one tell me what is the easiest way to find the length of int type?

Thnks but somehow this doesn't work for me for example
1
2
int a=4; //lenhgt=1
cout<<sizeof(a)<<endl;  //it displays 4 not 1  

It seems that you did not ask what you actually wanted to know.

To us, the "length of int type" means how much memory is allocated for an object of type int. All ints have same length, because every int is int. The int can store 1 or -2147483648 with equal ease. Really big numbers the int cannot store at all.


Your example implies a different question: How many digits does a base 10 representation of an integer value have?


A basic type is not necessarily "same length" in every platform. See http://www.cplusplus.com/reference/limits/numeric_limits/ how to check values in your platform.
Nov 26, 2017 at 8:54pm
(int) of (log base 10 of N + 1)
Nov 26, 2017 at 10:17pm
I was close to posting this solution earlier.

 
cout << ceil(log10(a + 1)) << endl;

But I realized you would need to add extra code to handle the value 0 (even more if you want to handle negative values). I'm also not comfortable relying on exact values with floating point numbers.

I think goldenchicken's solution is nice for its simplicity.

 
cout << to_string(a).length() << endl;

If you want something more efficient, but still reliable, you can use integers to calculate the value using a loop, or using recursion as lastchance did.
Last edited on Nov 26, 2017 at 10:19pm
Topic archived. No new replies allowed.