Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Suggestion #3

Closed
sebetci opened this issue Jul 13, 2021 · 0 comments
Closed

Fix Suggestion #3

sebetci opened this issue Jul 13, 2021 · 0 comments

Comments

@sebetci
Copy link

sebetci commented Jul 13, 2021

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.

@sebetci sebetci closed this as completed Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant