warning message in C

Posted on 2008-10-30
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.


//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

Question by:akohan
  • 3
  • 2
  • 2
LVL 84

Expert Comment

ID: 22847114
How is struct counts declared?  

Author Comment

ID: 22847150

Hi,  thanks for your response.

It is declared as:

struct counts
   long unsigned fields;
   long unsigned rows;

Any suggestions?

LVL 84

Accepted Solution

ozo earned 125 total points
ID: 22847180
cast them to (int) when you pass them to printf
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

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.


LVL 23

Expert Comment

ID: 22847622
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.

LVL 23

Assisted Solution

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.


Author Comment

ID: 22847892

Thank you for the detailed explanation!


Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

830 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