You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The use of the %dformat specifier for the sizeof operator in the example program in the ex11_16.c file is not accepted by the GCC (ISO C90, ISO C99 and ISO C11); this usage causes a warning by the GCC.
intmain( intargc, char**argv )
{
....
fprintf( ofPtr, "%-25s%5d\n", "char", sizeof( char ) );
fprintf( ofPtr, "%-25s%5d\n", "unsigned char", sizeof( unsigned char ) );
fprintf( ofPtr, "%-25s%5d\n", "short int", sizeof( short int ) );
fprintf( ofPtr, "%-25s%5d\n", "unsigned short int", sizeof( unsigned short int ) );
fprintf( ofPtr, "%-25s%5d\n", "int", sizeof( int ) );
fprintf( ofPtr, "%-25s%5d\n", "unsigned int", sizeof( unsigned int ) );
fprintf( ofPtr, "%-25s%5d\n", "long int", sizeof( long int ) );
fprintf( ofPtr, "%-25s%5d\n", "unsigned long int", sizeof( unsigned long int ) );
fprintf( ofPtr, "%-25s%5d\n", "float", sizeof( float ) );
fprintf( ofPtr, "%-25s%5d\n", "double", sizeof( double ) );
fprintf( ofPtr, "%-25s%5d\n", "long double", sizeof( long double ) );
....
}
As a result of my research, I learned that after the ISO C99 standard, the format specifier for the sizeof operator should be %zu. You can change the above code as follows:
Click to see recommended source code.
intmain( intargc, char**argv )
{
....
fprintf( ofPtr, "%-25s%5zu\n", "char", sizeof( char ) );
fprintf( ofPtr, "%-25s%5zu\n", "unsigned char", sizeof( unsigned char ) );
fprintf( ofPtr, "%-25s%5zu\n", "short int", sizeof( short int ) );
fprintf( ofPtr, "%-25s%5zu\n", "unsigned short int", sizeof( unsigned short int ) );
fprintf( ofPtr, "%-25s%5zu\n", "int", sizeof( int ) );
fprintf( ofPtr, "%-25s%5zu\n", "unsigned int", sizeof( unsigned int ) );
fprintf( ofPtr, "%-25s%5zu\n", "long int", sizeof( long int ) );
fprintf( ofPtr, "%-25s%5zu\n", "unsigned long int", sizeof( unsigned long int ) );
fprintf( ofPtr, "%-25s%5zu\n", "float", sizeof( float ) );
fprintf( ofPtr, "%-25s%5zu\n", "double", sizeof( double ) );
fprintf( ofPtr, "%-25s%5zu\n", "long double", sizeof( long double ) );
....
}
Also in your use GCC (ISO C90, ISO C99 and ISO C11) gives the following warning:
Click to see the GCC warning.
warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
19 | fprintf( ofPtr, "%-25s%5d\n", "char", sizeof( char ) );
| ~~^ ~~~~~~~~~~~~~~
| | |
| int long unsigned int
| %5ld
Thank you for your contributions to the community. I wish you good work.
The text was updated successfully, but these errors were encountered:
Hi @AndrewLaing,
The use of the
%d
format specifier for thesizeof
operator in the example program in the ex11_16.c file is not accepted by the GCC (ISO C90, ISO C99 and ISO C11); this usage causes a warning by the GCC.Click to see the source code in the ex11_16.c
As a result of my research, I learned that after the ISO C99 standard, the format specifier for the
sizeof
operator should be%zu
. You can change the above code as follows:Click to see recommended source code.
Also in your use GCC (ISO C90, ISO C99 and ISO C11) gives the following warning:
Click to see the GCC warning.
Thank you for your contributions to the community. I wish you good work.
The text was updated successfully, but these errors were encountered: