Tal
Tal

Reputation:

What are the lengths of common datatypes?

How many bytes would an int contain and how many would a long contain in C++?

Please provide some information about the 32-bit and the 64-bit computers, and the differences.

Upvotes: 2

Views: 559

Answers (8)

Roddy
Roddy

Reputation: 68033

As others have said endlessly, it depends on the compiler you're using (and even the compiler options that you select).

However, in practice, with compilers for many 32-bit machines, you will find:-

  • char: 8 bit
  • short: 16 bit
  • int: 32-bit
  • long: 32-bit
  • long long: 64-bit ( if supported)

The C standard basiucally says that a long can't be shorter than an int which can't be shorter than a short, etc...

For 64-bit CPUs, those often don't change, but you MUST beware that pointers and ints are frequently not the same size:

 sizeof(int) != sizeof(void*)

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190945

it is platform and compiler specific. do sizeof(int) and sizeof(long) in c or c++.

Upvotes: 7

David Turvey
David Turvey

Reputation: 2941

I think it depends on the hardware your using. on 32-bit platforms it is typically 4 bytes for both int and long. in C you can use the sizeof() operator to find out.

int intBytes;
long longBytes;
intBytes= sizeof(int);
longBytes = sizeof(long);

I'm not sure if long becomes 8 bytes on 64-bit architectures or if it stays as 4.

Upvotes: 1

Ryan
Ryan

Reputation: 9928

That depends greatly on the language you are using.

In C, "int" will always be the word length of the processor. So 32 bits or 4 bytes on a 32 bit architecture.

Upvotes: 0

friol
friol

Reputation: 7096

(I assume you're talking about C/C++)

It's implementation dependant, but this rule should be always valid:

sizeof(short) <= sizeof(int) <= sizeof(long)

Upvotes: 5

Mark Bessey
Mark Bessey

Reputation: 19782

It depends on your compiler. And your language, for that matter. Try asking a more specific question.

Upvotes: 0

Zebra North
Zebra North

Reputation: 11492

It depends on the compiler.

On a 32 bit system, both int and long contain 32 bits. On a 16 bit system, int is 16 bits and long is 32.

There are other combinations!

Upvotes: 1

changelog
changelog

Reputation: 4681

See the wikipedia article about it.

Upvotes: 7

Related Questions