char is 1 byte incremnt. int is 2 byte increment. float is 4 byte increment. double is 8 byte increment. why int increase 4 byte here?
Asked
Active
Viewed 223 times
-4
Konrad Rudolph
- 506,650
- 124
- 909
- 1,183
Spacez Ly Wang
- 177
- 5
-
2Who says an `int` is necessary 2 bytes? On your system (and most), it is 4. – GManNickG Oct 19 '12 at 06:35
-
Your premise is wrong. Problem solved. – Konrad Rudolph Oct 19 '12 at 06:35
-
3Please insert the code as a text. – Andrey Oct 19 '12 at 06:36
-
@GManNickG so, int and float increment is 4? – Spacez Ly Wang Oct 19 '12 at 06:36
-
@SpacezLyWang: According to the language, the increment is `sizeof(int)` and `sizeof(float)` respectively. On your particular system, that means 4 for both. – GManNickG Oct 19 '12 at 06:37
-
@Andrey , I can't use stackoverflow editor well. that's why i uploaded pics – Spacez Ly Wang Oct 19 '12 at 06:38
-
possible duplicate of [size of int, long, etc](http://stackoverflow.com/questions/589575/size-of-int-long-etc) – jogojapan Oct 19 '12 at 06:40
-
1What is your trouble with the editor? It's bad practice to attach screenshots in SO because a screenshot is not searchable and not editable. – Andrey Oct 19 '12 at 06:42
-
It is a real shame since this is one of the better editors I have seen with instant feedback and good help. – r_ahlskog Oct 19 '12 at 07:53
2 Answers
3
Try this one:
int i;
...
printf("%d",sizeof(i));
What did you get? Most likely 4. Why? Because your CPU is most likely a 32bit one. The 2 byte int was true on older CPUs...
Never assume sizes of variables based on "should be", always use sizeof()!
ppeterka
- 20,372
- 6
- 62
- 77
0
int is 32 bits, so is float. Here, double is 64 bits.
On many 16-bit systems, int is 16 bits. C's int was intended to be whatever size is most natural for the system it is compiled for. There is no requirement that int be the same size across systems.
wallyk
- 55,472
- 16
- 84
- 144
-
1...on most architectures used nowadays! This wasn't evident a few decades ago. Maybe he just learnt from an outdated book... – ppeterka Oct 19 '12 at 06:39
-
@ppeterka yes, i'm reading a book from old age. You've pointed out. :-) – Spacez Ly Wang Oct 19 '12 at 06:41