• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 949
  • Last Modified:

warning message in C

Hello group,

In following code, I'm getting a warning as

   warning: int format, long unsigned int arg (arg 2)
   warning: int format, long unsigned int arg (arg 3)
   warning: char format, void arg (arg 5)

1) Number I'm using here are always between 0 and 11, why should this throw this message?
2) How can I fix it to avoid future runtime issues?


Your help is appreciated.

Thanks.



//line of code which causes the warning
printf("\n%d in line=%d data=%.*s", ((struct counts *)data)->fields, ((struct counts *)data)->rows, len, s);

Open in new window

0
akohan
Asked:
akohan
  • 3
  • 2
  • 2
2 Solutions
 
ozoCommented:
How is struct counts declared?  
0
 
akohanAuthor Commented:


Hi,  thanks for your response.


It is declared as:

struct counts
{
   long unsigned fields;
   long unsigned rows;
};

Any suggestions?


0
 
ozoCommented:
cast them to (int) when you pass them to printf
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
akohanAuthor Commented:

Thanks. Also, I had to take care of a case like

        printf("\n %.*s",  str);

where str originally is void*  so I changed it to

       printf("\n %.*s", (char*)str );

works fine but I'm hoping it is correct method.

Right?


Thanks.
0
 
MysidiaCommented:
Try:
printf("\n%lu in line=%lu data=%.*s", ((struct counts *)data)->fields, ((struct counts *)data)->rows, len, (char*)s);

To explain the warning you were getting
   warning: int format, long unsigned int arg (arg 2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The format for the 2nd argument you gave the printf() function was the "%d"
%d is used for printing an int,   but you stated that fields is an 'unsigned long'
the proper format for printing an unsigned long is %lu.

   warning: int format, long unsigned int arg (arg 3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The 3rd arg to the printf function: ((struct counts *)data)->rows
you used %d
but accordingly stated that 'rows' is unsigned long, so %lu should be used.

0
 
MysidiaCommented:
>> 1) Number I'm using here are always between 0 and 11, why should this throw this message?

Because the compiler does not know what number ranges you will put in a variable.

It only knows the types of your variables, and if they matfch the format string properly.

printf("%d\n",  (unsigned long)5);
Would generate the same warning.

printf("%lu\n", 5);
Would also generate a warning

Instead to use a numeric constant with %lu, you have to indicate the constant is unsigned long
printf("%lu\n", 5UL);

or a signed long:  printf("%ld\n", 5L);
or a float: printf("%f\n", 5.0F);
or a double:  printf("%lf\n", 5.0);
or a long double:  printf("%Lf\n", 5.0L);


And there are many others.

It's the _datatype_ of the argument matching a known invalid application of the format string that normally triggers such warnings.

The C compiler will not seamlessly convert datatypes for you and do the right thing.

If you want to print a long int,  you should use a printing mechanism that anticipates the range of long ints available  _to the long int datatype_   (regardless of whether you use those values or not).


In particular,  unnecessary casting is dangerous and ugly.
It is better to use the proper format strings, so long as a standard format string is available.

0
 
akohanAuthor Commented:


Thank you for the detailed explanation!

Regards.
ak
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now