ı have a question.
I know which data type and its specifiers . but ı do not know that for example "int" data type how many number takes

or long integer how many number takes. ı calculated their bytes but still ı do not know when ı choose them.


again example :

float 4 bytes
double 8 bytes
long double 12 bytes

But can they contain how many number

Recommended Answers

All 11 Replies

If you know the amount of bytes the type has, and the amount of bits in a byte, unless it's unsigned, the range for int, for example is:

0 to (2^(b * 8) /2)-1 for positive values and
-1 to -((2^(b * 8)) /2) for negative values where b is the number of bytes of the type and 8 is the number of bits in a byte.

You divide by 2 because there's two sets of ranges here, positive and negative and raise it to the second power because we're working in binary which only has two values: 0, 1.

In the case of unsigned int, the values range from:

0 to (2^(b * 8)) - 1

http://en.wikipedia.org/wiki/Integer_(computer_science)
i hope this helps.

by what i was told int and long int no longer have a real differance between them

int values: -2,147,483,648 to 2,147,483,647

Use sizeof:

#include <iostream>
using namespace std;


int main()
{
    cout << "int : " << sizeof(int) << endl;
    cout << "char : " << sizeof(char) << endl;
    cout << "float : " << sizeof(float) << endl;
    cout << "double : " << sizeof(double) << endl;
    cout << "long : " << sizeof(long) << endl;
    cout << "long long : " << sizeof(long long) << endl;
    cout << "long double : " << sizeof(long double) << endl;
    cin.get();
    return 0;
}

Results...

int : 4
char : 1
float : 4
double : 8
long : 4
long long : 8
long double : 12

ı understood. However , how will ı determine my borders with this data types.
there must be much easier way for this.

>> ı understood. However , how will ı determine my borders with this data types.
there must be much easier way for this.

"borders"? You mean the range of values these types can accomodate?

I was going to reply to this as well, but it appears it's already said what I was going to, but I have one small question thats related to this.

A data type that stores 1 byte can have a combination of 2^8-1 combinations, but what is the reason that it is 1 less than 2^8?

I know 2 bits can have the binary combinations 00, 01, 10, & 11, but does it really only store 3 combiations? Does this have to do with you don't count 00 as it would be the special null character?

I was going to reply to this as well, but it appears it's already said what I was going to, but I have one small question thats related to this.

A data type that stores 1 bit can have a combination of 2^8-1 combinations, but what is the reason that it is 1 less than 2^8?

I know 2 bits can have the binary combinations 00, 01, 10, & 11, but does it really only store 3 combiations? Does this have to do with you don't count 00 as it would be the special null character?

A byte is not always (but usually is) 8 bits. A char is always 1 byte. Assuming 8 bits in a byte, char will use all 256 combinations, not just 255 of them. Ditto unsigned char. The range of a char is -128 to 127. Unsigned char is 0 to 255. All 256 possibilities are allowed.

A byte is not always (but usually is) 8 bits. A char is always 1 byte. Assuming 8 bits in a byte, char will use all 256 combinations, not just 255 of them. Ditto unsigned char. The range of a char is -128 to 127. Unsigned char is 0 to 255. All 256 possibilities are allowed.

Ah. I see know. Thanks for the clarification, it's just the mixup that I usally start counting at 1 not 0!

I know 2 bits can have the binary combinations 00, 01, 10, & 11, but does it really only store 3 combiations? Does this have to do with you don't count 00 as it would be the special null character?

I uses all of the combinations available to it, but how it uses them depends on whether it's unsigned or signed.

Using your 2-bit example:
If the value is "signed", the first bit is a "sign bit" and your max value is determined by (2^(n-1))-1 and the min is -(2^(n-1)). Usually, if the sign bit is "on" the value is negative making the possible values 10(-2), 11(-1), 00(0), 01(1)

If it's unsigned, there is no "sign bit", so the max value is (2^n)-1. This produces the values 00(0), 01(1), 10(2), 11(3) instead (notice how the values with a 1# value moved to the right and fell into sequence).

They both have 4 combinations, but because the count starts at 0, the last value is

Yes, I understand that now, but VernonDozier beat you to it unfortunatly. However I did not mean to just outright hijack this thread. It would be more helpful if we went back to margeaux54's problem.

If he just wanted to know the range of the datatypes though, I think wikipedia page that was already shown could help him with that.

>>It would be more helpful if we went back to margeaux54's problem.
Yes, you're right...

ı have a question.
I know which data type and its specifiers . but ı do not know that for example "int" data type how many number takes

or long integer how many number takes. ı calculated their bytes but still ı do not know when ı choose them.


again example :

float 4 bytes
double 8 bytes
long double 12 bytes

But can they contain how many number

You really can't calculate that without knowing how the floating point value is represented internally, even then it's very confusing. Here are a couple articles that may help:

http://en.wikipedia.org/wiki/Floating-point
http://en.wikipedia.org/wiki/Single_precision_floating-point_format
http://en.wikipedia.org/wiki/Double_precision_floating-point_format

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.