i can't understand,why???

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

i can't understand,why???

Post by Morning »

[cpp]
#include <iostream>
using namespace std;
int main()
{
long n = -2147483648;
n *= -1;
cout << n << endl;
}
[/cpp]
my output:

Code: Select all

-2147483648
why?
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

Because the range of Long is:

-2147483648 to 2147483647
so it is not possible ot make it(-2147483648) positive using n*=(-1)!

you can use __int64
Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China

Post by Morning »

but even int type can handle 2^31,why can't long be larger?
[cpp]
#include <iostream.h>
int main()
{
int i;
i = 2147483648;
cout << i - 1 << endl;
return 0;
}
[/cpp]
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

sorry standard int type range is
-2^15 to 2^15-1 that is -32768 to 32767
But it is possible that your compiler support more range! :)
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post by CDiMa »

A1 wrote:sorry standard int type range is
-2^15 to 2^15-1 that is -32768 to 32767
But it is possible that your compiler support more range! :)
Nope, C++ standards don't impose much on the size of int types.
You can only be certain that a char is exactly one byte, short is at least 2 bytes, int is at least 2 bytes, and long is at least 4 bytes. So it may happen that a certain compiler implements shorts,int and longs as 4 or 8 bytes values.

Ciao!!!

Claudio
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

Yes CDiMa you are right .
I worte "standard" but should write "in general" :oops:
That is "In general int type range is ......"
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Then your understanding of "general" is very special. Only some out-of-date compilers, like old Borlands, have sizeof(int) = 2.
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

Krzysztof Duleba wrote:Then your understanding of "general" is very special. Only some out-of-date compilers, like old Borlands, have sizeof(int) = 2.
As a good programmer,
you should always assume that sizeof (int) is 2 because you have long for 4 byte number.
(my opinion, you don't need to believe it :|)
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

As a good programmer, you shouldn't assume nothing about int size at all (knowledge that sizeof(int) >= 2 is not based on assuption of course). Have you noticed that almost all configure scripts check that?
But that's not my point, I just referred to "in general". New compilers, working on IA32, have sizeof(int) = 4 and I believe that's the most general case. Other cases are special.
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Seems that some serious talk is going on.

From what I have heard:
sizeof(char)<= sizeof(int) <= sizeof(long) <= sizeof(long long)..

.. so if we define int as 2 and long as 4, then the condition still holds.

But now the standard for int is 4 and 2 for int is obsolete.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

No, standard says that sizeof(int) is implementation-defined. sizeof(int) == 4 is just the most common case nowadays, at least for IA32.
Post Reply

Return to “C++”