Linux / UNIX Tech Support Forum
This is a discussion on Fix sprintf warning within the Coding in General forums, part of the Development/Scripting category; Hi all, I can't figure out how to remove a single warning in my project. Example code: typedef long long ...
|
|||||||
| Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi all,
I can't figure out how to remove a single warning in my project. Example code: typedef long long int_64; void write_integer(int x, int y, int_64 i) { char number[35]; sprintf(number, "%u", i); //code that passes number to string write function } When I compile this on linux (gcc with option -Wall) I get the warning: warning: unsigned int format, different type arg (arg 3) The sprintf does work for numbers larger then 32 bit though, the output is correct. Is there a good way to satisfy the compiler? And if there isn't, is there a way to hide a single warning? Thank you, Rick. |
| Sponsored Links | ||
|
|
|
||||
|
Rick, I am getting following warning:
Code:
file.c: In function 'write_integer': file.c:6: warning: incompatible implicit declaration of built-in function 'sprintf' May be you need to put complete function code here so that we can help you
__________________
Vivek Gite Linux Evangelist |
|
|||
|
Thank you for your reply nixcraft.
This is the complete code that reproduces the warning: example.cpp : Code:
#include <stdio.h>
#include <string.h>
typedef long long int_64;
int main(void)
{
char buffer[51];
int_64 huge_number = 2000000000 * 4;
memset(buffer, 0, 50);
sprintf(buffer, "%u", huge_number);
printf("Result: %s\n", buffer);
return 0;
}
Code:
gcc -lstdc++ -Wall example.cpp -o example Code:
example.cpp: In function `int main()': example.cpp:11: warning: unsigned int format, different type arg (arg 3) Code:
./example Result: 3705032704 Quote:
In this example 2000000000 * 4 should be 8000000000 instead of 3705032704. So it seems the compiler warning is right. That leaves the question: How can I convert a very large number (over 32 bits) to a string? |
|
||||
|
Do not use %u for large unsigned int format, use %llu i.e.
Replace Code:
sprintf(buffer, "%u", huge_number); Code:
sprintf(buffer, "%llu", huge_number);
__________________
Vivek Gite Linux Evangelist |
|
|||
|
Thank you !
It works like a charm. I'm a Delphi programmer for windows. Recently I started to play with C++ and gcc. Linux is way more strict then Windows, which is good IMO. I just encounter a lot of unknowns Thank you for helping. |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| WARNING: add_spec: No major number for SUNW,socal | vasanth | Solaris/OpenSolaris | 0 | 26-07-2006 05:25 PM |
| WARNING: General Protection Faults in these executables-sed | Usagi | Linux software | 7 | 23-06-2006 12:15 PM |
| Script disabling error or warning message output | sweta | Shell scripting | 2 | 18-04-2006 08:24 PM |
| gtk warning cannot open display gnome | Linux software | 1 | 14-03-2006 12:50 PM | |