Is this page helpful? Yes No. Any additional feedback? Skip Submit. Submit and view feedback for This product This page. Still, using sizeof int is the best way to get the size of an integer for the specific system the program is executed on. EDIT: Fixed wrong statement that int is 8 bytes on most bit systems. For example, it is 4 bytes on bit GCC. This is one of the points in C that can be confusing at first, but the C standard only specifies a minimum range for integer types that is guaranteed to be supported.
In that case, int , is 2 bytes. However, implementations are free to go beyond that minimum, as you will see that many modern compilers make int bit which also means 4 bytes pretty ubiquitously. The reason your book says 2 bytes is most probably because it's old. At one time, this was the norm. In general, you should always use the sizeof operator if you need to find out how many bytes it is on the platform you're using.
Prior to that, there was no universal way to get an integer of a specific width although most platforms provided similar types on a per-platform basis. There's no specific answer. It depends on the platform. It is implementation-defined. It can be 2, 4 or something else.
The idea behind int was that it was supposed to match the natural "word" size on the given platform: 16 bit on bit platforms, 32 bit on bit platforms, 64 bit on bit platforms, you get the idea. However, for backward compatibility purposes some compilers prefer to stick to bit int even on bit platforms. The time of 2-byte int is long gone though bit platforms? Your textbooks are probably very old.
The answer to this question depends on which platform you are using. But irrespective of platform, you can reliably assume the following types:. The size of int and all other integer types are implementation defined, C99 only specifies:.
That depends on the platform you're using, as well as how your compiler is configured. The only authoritative answer is to use the sizeof operator to see how big an integer is in your specific situation. Range might be best considered, rather than size. Both will vary in practice, though it's much more fool-proof to choose variable types by range than size as we shall see. The following statement, taken from the C standard linked to above , describes this in words that I don't think can be improved upon.
The sizeof operator yields the size in bytes of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand.
Assuming a clear understanding will lead us to a discussion about bytes. That's just another one of those nuances which isn't considered when talking about the common two or four byte integers. That's not necessarily useful information, is it? Let's delve deeper From this, we can derive a minimum for how many bits are required to store the value , but we may as well just choose our variables based on ranges.
Nonetheless, a huge part of the detail required for this answer resides here. For example, the following that the standard unsigned int requires at least sixteen bits of storage:. This explains the phenomena you've observed:. You should upgrade your textbook and compiler , and strive to remember that I.
Enough about that, though; let's see what other non-portable secrets those underlying integer bytes store Value bits are what the common misconceptions appear to be counting. The above example uses an unsigned integer type which typically contains only value bits, so it's easy to miss the devil in the detail. Floating-point types may support special values :. Some operations on floating-point numbers are affected by and modify the state of the floating-point environment most notably, the rounding direction.
Implicit conversions are defined between real floating types and integer types. Create account Log in.
Namespaces Page Discussion. Views View Edit History. From cppreference. Keywords Escape sequences. Namespace declaration. Namespace aliases. Fundamental types Enumeration types Function types. Compound types Union types. Default initialization Value initialization Zero initialization Copy initialization Direct initialization. Expressions Value categories Order of evaluation.
Operators Operator precedence. Class declaration Constructors this pointer. Access specifiers friend specifier. Class template Function template. The minimum value is -9 , the maximum value is 9 The ulong type also occupies 8 bytes and can store values from 0 to 18 Since the unsigned integer types are not designed for storing negative values, the attempt to set a negative value can lead to unexpected consequences.
Such a simple script will lead to an infinite loop:.
0コメント