walk an Integer change in C accounting 2 bytes or 4 bytes? What room the determinants that it relies on?

Most of the textbooks speak integer variables occupy 2 bytes.But as soon as I operation a routine printing the succeeding addresses of variety of integers it mirrors the difference of 4.

You are watching: How many bytes in an int java


*

*

I recognize it"s same to sizeof(int). The dimension of one int is really compiler dependent. Ago in the day, as soon as processors to be 16 bit, an int was 2 bytes. Nowadays, it"s most often 4 bytes ~ above a 32-bit and 64-bit systems.

Still, utilizing sizeof(int) is the best way to get the size of an integer for the certain system the routine is enforcement on.

EDIT: addressed wrong statement the int is 8 bytes on many 64-bit systems. For example, the is 4 bytes ~ above 64-bit GCC.


*

*

This is among the points in C that can be confusing in ~ first, but the C standard just specifies a minimum range for integer species that is guarantee to it is in supported. Int is guaranteed to be able to hold -32767 to 32767, which calls for 16 bits. In the case, int, is 2 bytes. However, implementations are complimentary to go beyond that minimum, as you will view that many contemporary compilers make int 32-bit (which also method 4 bytes pretty ubiquitously).

The factor your book says 2 bytes is most probably due to the fact that it"s old. In ~ one time, this was the norm. In general, girlfriend should always use the sizeof operator if you require to find out how many bytes it is on the communication you"re using.

To resolve this, C99 added brand-new types whereby you can explicitly ask because that a particular sized integer, for example int16_t or int32_t. Before that, there to be no universal way to obtain an integer of a details width (although most platforms noted similar varieties on a per-platform basis).


share
enhance this answer
monitor
reply Jul 11 "12 at 18:24
*

FatalErrorFatalError
48.7k1414 gold badges9494 silver badges114114 bronze title
7
| show 2 much more comments
34
There"s no details answer. It counts on the platform. That is implementation-defined. It have the right to be 2, 4 or something else.

The idea behind int was the it was claimed to match the natural "word" size on the provided platform: 16 little on 16-bit platforms, 32 little on 32-bit platforms, 64 bit on 64-bit platforms, you get the idea. However, for backward compatibility purposes some compilers favor to stick to 32-bit int even on 64-bit platforms.

The time that 2-byte int is long gone though (16-bit platforms?) unless you space using some installed platform v 16-bit indigenous size. Her textbooks space probably very old.

See more: What Happened To Lisa Mason Qvc, How Old Is Lisa Robertson Who Used To Be On Qvc


share
boost this answer
monitor
edited Nov 10 "16 at 17:51
answer Jul 11 "12 in ~ 18:15
AnTAnT
297k3939 yellow badges499499 silver badges742742 bronze badges
1
add a comment |
20
The answer to this inquiry depends top top which platform you space using.But regardless of whether of platform, you can reliably i think the following types:

share
enhance this prize
monitor
edited Nov 9 "18 in ~ 7:59
Antti Haapala
121k2121 yellow badges256256 silver badges291291 bronze title
answer Mar 14 "14 in ~ 12:14
Priyank AroraPriyank Arora
42744 silver badges55 bronze title
3
add a comment |
16
C99 N1256 typical draft

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

The size of int and all other integer types are implementation defined, C99 just specifies:

minimum size guaranteesrelative sizes in between the types

5.2.4.2.1 "Sizes the integer species " provides the minimum sizes:

1 <...> their implementation-defined values shall be same or higher in magnitude (absolute value) come those displayed <...>

6.2.5 "Types" climate says:

8 For any kind of two integer varieties with the same signedness and also different essence conversion rank(see 6.3.1.1), the range of values of the kind with smaller integer conversion location is asubrange of the values of the other type.

and 6.3.1.1 "Boolean, characters, and integers" determines the relative conversion ranks:

1 Every integer type has an creature conversion rank defined as follows:

The location of lengthy long int chandelier be better than the rank of long int, whichshall be greater than the location of int, i m sorry shall be greater than the location of shortint, i beg your pardon shall be higher than the rank of signed char.The location of any type of unsigned integer form shall equal the location of the correspondingsigned integer type, if any.For all integer types T1, T2, and T3, if T1 has greater rank 보다 T2 and T2 hasgreater rank than T3, then T1 has greater rank than T3