nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Fix sprintf warning

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


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-30-2006, 11:50 PM
Junior Member
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0
Rick
Default Fix sprintf warning

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.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-2006, 12:01 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 906
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

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'
To fix above error i just use #include<stdio.h>

May be you need to put complete function code here so that we can help you
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #3 (permalink)  
Old 07-02-2006, 05:10 PM
Junior Member
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0
Rick
Default

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;
}
Compile it with the command:
Code:
gcc -lstdc++ -Wall example.cpp -o example
The compiler output:
Code:
example.cpp: In function `int main()':
example.cpp:11: warning: unsigned int format, different type arg (arg 3)
When I run it:
Code:
./example
Result: 3705032704
Quote:
Originally Posted by rick
The sprintf does work for numbers larger then 32 bit though, the output is correct.
That seems to be untrue.
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 bit to a string?
Reply With Quote
  #4 (permalink)  
Old 07-02-2006, 07:26 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Any distro with shell
Posts: 906
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Do not use %u for large unsigned int format, use %llu i.e.
Replace
Code:
sprintf(buffer, "%u", huge_number);
With
Code:
sprintf(buffer, "%llu", huge_number);
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 07-03-2006, 12:00 AM
Junior Member
 
Join Date: Jun 2006
Posts: 3
Rep Power: 0
Rick
Default

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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
WARNING: add_spec: No major number for SUNW,socal vasanth Solaris/OpenSolaris 0 07-26-2006 05:25 PM
WARNING: General Protection Faults in these executables-sed Usagi Linux software 7 06-23-2006 12:15 PM
Script disabling error or warning message output sweta Shell scripting 2 04-18-2006 08:24 PM
gtk warning cannot open display gnome Linux software 1 03-14-2006 12:50 PM


All times are GMT +5.5. The time now is 07:50 AM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36