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 trialMike Atkinson
6,882 PointsWhat is Placeholder "%ld" ?
The video used the placeholder %ld.
printf("float %ld bytes /n", sizeof(float);
Any expansion on this would be appreciated.
4 Answers
sunil prakash
4,323 Pointsits the placeholder to printing the long variables, as in your case, its going to print the size of float
Gloria Dwomoh
13,116 Points%ld is for long integers, the functions sizeof(float) returns an integer to be more specific it actually returns the number of bytes but it appears to look like an integer, that defines the size of the float number, it doesn't return a float.
Antoine Guenet
2,843 PointsThanks for the clear responses. They do raise a couple of extra questions for me... In this case the return was 4. How is 4 a "long" integer ? Are we using %ld by convention because in most cases the return of sizeof() will be longer than just 4? Btw, what's the limit between an "integer" and a "long integer" ? Iow, what's the max length of an int ?
Gloria Dwomoh
13,116 PointsHi Antoine, if you check my answer I said it will return the number of bytes. In other words the storage size of a long integer which is 4 bytes. Now, the limit between an integer and long integer along with their storage size depends on the platform you are using. For example based on the platform you are on sizeof() an integer can return 2 or 4. Usually int values are from -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 and -2,147,483,648 to 2,147,483,647 for long integers (take note that this is for signed values). I hope this clears some things.
Antoine Guenet
2,843 PointsThanks Gloria for taking the time! I guess I'll have to continue on with this course until he starts explaining the data types because I still don't understand why the fact that the return is in bytes makes it a "long" integer. Nevermind I guess I'm just lacking some essential knowledge at this point. Thanks anyway, you did bring in some valuable extra information!
Gloria Dwomoh
13,116 PointsYou are welcome, and you'll learn more about that once you dive into datatypes more. You can read this too, it will make things a bit clearer sizeof Operator (C).
PS. The first few lines of the link above are easier to understand and the main point, after that it might get somehow complex but over time you'll be able to fully understand it if you keep learning.
Joseph Potts
11,187 PointsJoseph Potts
11,187 PointsThe %d placeholder is used for integers, not floats. %f is for floats.
sunil prakash
4,323 Pointssunil prakash
4,323 PointsHi Joseph, just to add, %ld is for long, and in the example, it was tried to print the size of a float datatype, the size of any datatype is represented as long integral value, therefore %ld is used to print the long value.