views:

414

answers:

9

I am C# developer and I am almost certain that in this language an "int" is always 32 bits regardless of the platform (32 vs 64 bit), a "long" is always 64 bits, a float is 32 and a double 64 and so on.

There is any language where its not like that? Where the int size depends on the processor?

+9  A: 

The sizes of int etc in C/C++ aren't formally defined - they are compiler specific; see here for more details.

The C# designers thankfully formally dictated in the spec: int = System.Int32, long = System.Int64, etc - so you don't have to worry about it changing. The only easily noticeable difference on x64 is IntPtr.Size.

Marc Gravell
ECMA-335 Common Language Infrastructure spells all of this out. Keeps VB.Net et al consistent as well.
sixlettervariables
@sixlettervariables - well, not quite... it might spell out how Int32 works, but the C# term "int" could have been written to mean anything (perhaps depending on context).
Marc Gravell
+4  A: 

In C++, for instance, int is defined to be the "natural" word size of the processor. If you look in limits.h (or climits, both of which are part of Standard Library), you'll see INT_MIN and INT_MAX constants, which define a valid range of the int type. It's required for INT_MIN to be -32767 or less, and for INT_MAX to be at least 32767.

Anton Gogolev
+2  A: 

C and C++ are allowed to have variable sized ints... as far as I remember, the only requirement is that small <= int <= long. So certainly a compiler could produce 32-bit ints on a 32-bit machine, and 64-bit ints on a 64-bit machine.

Incidentally, the sizes of floats and doubles in C# are only defined for storage - variables are allowed to use extra precision. So the following conditional:

class Foo
{
  private float _v = ...; // Definitely 32-bit.

  void Test()
  {
    var v = _v; // Could be 32-, 64-, or 96-bit.

    if(v == _v) {
      ...
    }
  }
}

May or may not execute. (At the time of writing, the CLR will usually use a 96-bit FP register in release mode if it can).

stusmith
double is 64-bit, not 32-bit. "v" is statically types as double by the rules of type inference. Can you qualify any scenario where it could be 96? It absolutely can't be 32.
Marc Gravell
Whoops, cheers, typo. Corrected now.
stusmith
On an x86, the floating-point registers are 96-bit. The CLR allows a JITter to 'promote' float and double values to 96-bit registers, provided no accuracy is lost. Storage (ie member variables) are always of the specified type.
stusmith
A: 

As Marc Gravell said in C++, the only type that have an officially almost fixed size defined for all platforms is char >= 8 bits.

The other types are bigger than char but their size is relative to the platform/compiler implementation. The standard only specify witch should be bigger than witch other.

Klaim
1 byte = 8 bits is absolutely NOT officially fixed.
Michael Borgwardt
Correction: while sizeof(char) is always ond (that is, 1 byte), bytes aren't required to be octets (i.e. have 8 bits). The number of bits/byte is defined as CHAR_BITS. sizeof(char) == 8 bits is true on most widely used CPU arches, but not always true.
Eduard - Gabriel Munteanu
Michael Borgwardt> Yes that's why I did't wrote "1 byte = 8 bits" but "1 byte (8 bits)" meaning "here we talk about 8 bits".As you prefer it I'll can replace by "8 bits" only.
Klaim
The standard only guarentees that char >= 8bits. Some older machines (PDP10) used char == 9 bits
Chris Dodd
+3  A: 

As mentioned in this question:

Classifying and Formally Verifying Integer Constant Folding

The Java language specification defines exactly how integer numbers are represented and how integer arithmetic expressions are to be evaluated. This is an important property of Java as this programming language has been designed to be used in distributed applications on the Internet. A Java program is required to produce the same result independently of the target machine executing it.

In contrast, C (and the majority of widely-used imperative and object-oriented programming languages) is more sloppy and leaves many important characteristics open. The intention behind this inaccurate language specification is clear. The same C programs are supposed to run on a 16-bit, 32-bit, or even 64-bit architecture by instantiating the integer arithmetics of the source programs with the arithmetic operations built-in in the target processor. This leads to much more efficient code because it can use the available machine operations directly. As long as the integer computations deal only with numbers being “sufficiently small”, no inconsistencies will arise.

In this sense, the C integer arithmetic is a placeholder which is not defined exactly by the programming language specification but is only completely instantiated by determining the target machine.

Java precisely defines how integers are represented and how integer arithmetic is to be computed.

      Java Integers
--------------------------
Signed         |  Unsigned
--------------------------
long  (64-bit) |
int   (32-bit) |
short (16-bit) |  char (16-bit)
byte  (8-bit)  |

Char is the only unsigned integer type. Its values represent Unicode characters, from \u0000 to \uffff, i.e. from 0 to 216−1.

If an integer operator has an operand of type long, then the other operand is also converted to type long. Otherwise the operation is performed on operands of type int, if necessary shorter operands are converted into int. The conversion rules are exactly specified.

[From Electronic Notes in Theoretical Computer Science 82 No. 2 (2003)
Blesner-Blech-COCV 2003: Sabine GLESNER, Jan Olaf BLECH,
Fakultät für Informatik,
Universität Karlsruhe
Karlsruhe, Germany]

VonC
A: 

c# does provide a type whose size depends on the processor, os and bitness flags of the executable IntPtr

Also any type which itself holds a reference type will vary in size on 32/64 bit since the references themselves are 32/64bits accordingly.

The CLR provides the following types which are all variable size depending on the bitness:

  • native int
  • native unsigned int
  • native references (i.e. pointers)
ShuggyCoUk
A: 

All the C/C++ standard specifies is ordinal relations between the sizes of different data types. Different sizes are possible depending on the implementation, and most likely vary with the CPU architecture you're building for; in fact, under GCC longs are always word-sized (the Linux kernel code depends upon this guarantee).

Good programming practices imply using sizeof() instead of hardcoded sizes.

Eduard - Gabriel Munteanu
A: 

The size of float and double are IEEE standards, and not computer ones. That's why C#, Java, C, C++ and every other language that implements the float and double have the exact same rounding issues, and can't really be used for money calculations, or any calculation that requires precision.

The size of short, int, and long are defined by the language spec. However you don't want them to be completely arbitrary as that could lead to arbitrary behaviour when doing calculations with those numbers. They may be IEEE standards as well, but I'm not sure.

Jim Barrows
A: 

Article The forgotten problems of 64-bit programs development

See table "Picture 1. Data Models."