1
#include <stdio.h>
#define max_size 100

float array[max_size];
int n,counter;

int main(){
    printf("Enter the size of the array...\n");
    scanf("%d",&n);

    for (counter=0; counter<n; counter++){
        printf("%p\t",&array[counter]);
    }
    printf("\n\n");

    return 0;
}

I am just experimenting with this C program and I am trying to verify that the size of a float is 8 bytes. But upon running this code with 5 elements in array, I get the address of these elements as following:

Enter the size of the array...
5
0x555555755040  0x555555755044  0x555555755048  0x55555575504c  0x555555755050

As you can see for the first float number, my system has allocated memory space ...40,41,42,43 which is 4 bits of space if I am not wrong. But the float data type is supposed to have 8 bytes of space for it. I am thinking that the program should have allocated memory space ...40,41,...4F for 2 bytes of space. So

...40-...4F //for first 2 bytes
...50-...5F //for second 2 bytes 
...60-...6F //for third 2 bytes 
...70-...7F //for last 2 bytes

So the second address would start at ...80. But this is not the result I am obtaining. What am I missing in this process? Thank you for the help !!

G-aura-V
  • 145
  • 1
  • 7
  • `sizeof (float)` or `sizeof *array` or `sizeof array[0]` – pmg Jun 05 '21 at 09:39
  • 2
    Almost all environments use IEEE-754 single-precision for `float` and IEEE-754 double-precision for `double`. That's been true for decades. That means `sizeof(float)` is 4 unless you're using a very unusual system. – Paul Hankin Jun 05 '21 at 09:46
  • `40 - 4F` is 16 bytes. Address are byte-addresses. It looks like you think that an address references one bit. Maybe that's your confusion (other than expecting `sizeof(float)` to be 8. – Paul Hankin Jun 05 '21 at 09:59
  • @PaulHankin that means each address consists of 8 bits. Similar to assigning a group name to a group of 8 people. – G-aura-V Jun 05 '21 at 10:34
  • I'm not sure if you're asking me a question or telling me how bytes and addresses work. If the former, yes, a byte contains 8 bits (on any system you're likely to use). – Paul Hankin Jun 05 '21 at 10:37
  • Sorry for the confusion. I was asking actually. 8 bits = 1 byte is assigned a address. Right? – G-aura-V Jun 05 '21 at 10:40
  • 1
    Yes, each address in memory stores 1 byte. If you have a 4-byte value (for example a `float`) it'll span 4 addresses, for example `0x55551230 - 0x55551233`. (On very uncommon systems you may not have 8 bits in a byte, and float may not be 4 bytes). – Paul Hankin Jun 05 '21 at 10:48
  • Most systems are byte-addressed, and most systems use 8-bit bytes, so a difference of `4` between addresses means 4 bytes (32 bits). Also most systems use 4-byte `float`s. I am not aware of any system that is bit-addressed. Historically there have been systems that used 9-bit bytes, and some that have been word-addressed systems (16 or more bits per word). But almost all modern desktops use 8-bit bytes. – John Bode Jun 05 '21 at 12:34

1 Answers1

5

C standard does not say anything about the storage size of float and it has been purposely left out to the implementer.

Maybe on your system and compiler the size is 4. You can check that out by using sizeof(float). See also this discussion.

lukeg
  • 4,189
  • 3
  • 19
  • 40
  • 2
    This is the correct answer. The size of the different types vary from system to system. –  Jun 05 '21 at 09:44
  • sizeof(float) returned 4 which means 4 bytes I guess. But still the output doesn't make sense to me. – G-aura-V Jun 05 '21 at 09:56
  • @G-aura-V probably the reason you're having trouble understanding is in your question "But the float data type is supposed to have 8 bytes of space" is wrong - normally it'll be 4 bytes. If you do size(double) it'll most likely be 8, while C doesn't guarantee it a float is 4 bytes and a double 8 on just about all architectures. – PeterJ Jun 05 '21 at 11:36