Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Pointers and Memory Arrays, Length, and Size

Why does a float consume 4 bytes?

As we are calculating the length of an array, we first need to gather the length of a float. I did some experimenting on my own, to see if I could get this value to change, but it seems that the length of a float is a constant 4 bytes. Can anybody give any more explanation into this? Why is a float composed of 4 bytes? What if the number that it is storing has significantly more than 4 digits? Any insight would be greatly appreciated!

1 Answer

Stone Preston
Stone Preston
42,016 Points

just because a float has a size of 4 bytes doesnt mean it can only store 4 digits. It can store a number that is 4 bytes in size, not digits. If you wish to store a number that is larger than 4 bytes, you would need to use a double, which is 8 bytes so it can store even larger numbers, and if that doesnt work you can use a long double which stores even more. Datatype size also depends on your machine (32bit vs 64bit).

see this for an in depth tutorial on primitive types.