#include <stdio.h>
int main()
{
printf("%ld", sizeof(void *));
return 0;
}
The output for the program was 8, but I am unable to figure out how.
#include <stdio.h>
int main()
{
printf("%ld", sizeof(void *));
return 0;
}
The output for the program was 8, but I am unable to figure out how.
The program outputs 8 because your target is a 64-bit system with 8-bit bytes where pointers are 64-bit wide, hence occupy 8 bytes.
sizeof(void *) is the size in bytes of a generic pointer. The value depends on the platform. I have seen different ones where it was 1, 2, 4 or 8, but other values are possible, albeit highly unlikely.
Note that sizeof() expressions evaluate to the type size_t which is not the same as long int (especially on Windows 64-bit platforms, where long int only has 32 bits). You should use %zu to print a value of this type and output a trailing newline for clarity.
#include <stdio.h>
int main(void) {
printf("%zu\n", sizeof(void *));
return 0;
}
I don't know what the precise question was, but if asked what the output should be, here is a long answer:
8 or 4 or even 2 or 1, or some other value.size_t is not the same as unsigned long, the behavior is undefined, anything can happen.4% or 8%.The proper format is %zu, but for maximum portability to pre-c99 systems, here is an alternative:
#include <stdio.h>
int main(void) {
printf("%d\n", (int)sizeof(void *));
return 0;
}