I got the most simple code to display sizeof() of a datatype, say an int.
#include <stdio.h>
int main() {
printf('%i', sizeof(int));
}
No matter what I do, such as put sizeof(int) into an integer, or use 'zu' instead of 'i', it hands me this error:
error: invalid conversion from ‘int’ to ‘const char*’
Is there something wrong with my compiler? I do not get why I cannot print such a simple sizeof
..
EDIT: It seems a printf('%s', 'foo');
STILL tells me I am converting int
to const char*
, how on earth??
Printf is not the thing being buffered, it's stdio, so all similar functions will behave in the same way. To demonstrate the buffering, all you have to do is printf a lot of characters, usually more than 1024, and printf will print as you will have exceeded the maximum buffer length and it will automatically fflush.
Re: Alternative for sizeof operatorNo there is not alternative to sizeof() operator in standard ANSI C. Sizeof() is a compiletime operator can be applied to type too ,i.e. sizeof(int); runtime not call any function so sizeof() is very quick. Why do you will not use sizeof() operator?
We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”. In “%zu” format, z is a length modifier and u stand for unsigned type.
%s and string We can print the string using %s format specifier in printf function. It will print the string from the given starting address to the null '\0' character. String name itself the starting address of the string. So, if we give string name it will print the entire string.
printf("%zu", sizeof(int));
%zu
, not %i
(it's an unsigned value, not an integer)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With