Solved

warning message in C

Posted on 2008-10-30
7
945 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

738 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