Solved

warning message in C

Posted on 2008-10-30
7
939 Views
Last Modified: 2012-05-05
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
Comment
Question by:akohan
  • 3
  • 2
  • 2
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22847114
How is struct counts declared?  
0
 

Author Comment

by:akohan
ID: 22847150


Hi,  thanks for your response.


It is declared as:

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

Any suggestions?


0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 22847180
cast them to (int) when you pass them to printf
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:akohan
ID: 22847266

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
 
LVL 23

Expert Comment

by:Mysidia
ID: 22847622
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
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 125 total points
ID: 22847667
>> 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
 

Author Comment

by:akohan
ID: 22847892


Thank you for the detailed explanation!

Regards.
ak
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now