Why showing problem in data size? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why showing problem in data size?

I tried to some basic coding in c language, i am new but i getting error Where is problem and what is problem Please help guys #include <stdio.h> int main (){ printf ("int: %d \n", sizeof (int)); printf ("float: %d \n", sizeof (float)); printf ("double: %d \n", sizeof (double)); printf ("char: %d \n", sizeof (char)); return 0; }

14th Jan 2020, 11:45 AM
Jitendra Kumar
Jitendra Kumar - avatar
6 Answers
+ 7
Replace all the %d with %lu. `sizeof` operator returns `unsigned long int`, but in your code you used the format specifier for `int`. This is why you got the warning.
14th Jan 2020, 11:52 AM
Ipang
+ 4
Jitendra Kumar It was not an error, it was a warning. Had it been an error your code will unlikely be able to execute completely, if not at all. I think you need to get to know about format specifiers a bit, knowing them will help understanding why such a case like this happens. Use of appropriate format specifier is as important as choosing the right fuel for your ride (seriously). In simple way, the compiler is warning you that there's a possible loss of accuracy or validity in the output because the data returned by `sizeof` is `unsigned long int` (which is bigger by memory size - 8 bytes) compared to the type that `printf` expected to get which is `int` (commonly 4 bytes). http://www.cplusplus.com/reference/cstdio/printf/
14th Jan 2020, 3:11 PM
Ipang
+ 2
Can you explain please I am starter dude
14th Jan 2020, 11:54 AM
Jitendra Kumar
Jitendra Kumar - avatar
+ 2
What do you need explanation for? The sizeof() is an unary operator and it returns the number of bytes the data type occupies that you are passing to it in the above case.
14th Jan 2020, 12:05 PM
Avinesh
Avinesh - avatar
+ 1
Its showing error and i don't know why so i want to know where is error in my coding
14th Jan 2020, 12:21 PM
Jitendra Kumar
Jitendra Kumar - avatar
+ 1
Thank you Ipang I get answer
14th Jan 2020, 3:40 PM
Jitendra Kumar
Jitendra Kumar - avatar