-
Notifications
You must be signed in to change notification settings - Fork 14
Closed
Description
Hi @AndrewLaing,
The use of the %d format 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.
Click to see the source code in the ex11_16.c
int main( int argc, 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.
int main( int argc, 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.
Metadata
Metadata
Assignees
Labels
No labels