Warning: format '% d expects type' int, but argument 4 is of type 'size_t

I used several similar messages to change the code, however, warnings are still generated.

$ g++ ncfile.c -o ncfile -g -lnetcdf ncfile.c: In function 'int main(int, char**)': ncfile.c:363: warning: format '%d' expects type 'int', but argument 4 has type 'size_t' ncfile.c:363: warning: format '%d' expects type 'int', but argument 4 has type 'size_t' ncfile.c:364: warning: format '%d' expects type 'int', but argument 4 has type 'size_t' ncfile.c:364: warning: format '%d' expects type 'int', but argument 4 has type 'size_t' 

In this block around lines 363 and 364:

  for(i = 0; i < ndimsp; i++) { char * temp_name; size_t temp_len; temp_name = (char *)malloc(sizeof(char) * 20); nc_inq_dim(cid, dimids[i], temp_name, &temp_len); dimlens[i] = temp_len; if(dimids[i] == unlimdimidp) printf("\t\t%d %s \tlength: %d (UNLIMITED)\n", i, temp_name, temp_len); else printf("\t\t%d %s \tlength: %d\n", i, temp_name, temp_len); total_records *= temp_len; free(temp_name); } 

How can I get rid of warnings. Is it bad for the results?

Thanks,

Michael

+4
source share
1 answer

Try using the z modifier. Basically% zu for size_t value.

this will be the result:

 printf("\t\t%d %s \tlength: %zu\n", i, temp_name, temp_len); 

Take a look at this question:

How can I print a portable variable size_t using the printf family?

+7
source

All Articles