nixCraft Linux Forum

nixCraft

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

Linux answers from nixCraft.


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.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-06-2006, 11:50 PM
Junior Member
User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 02-07-2006, 12:01 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 02-07-2006, 05:10 PM
Junior Member
User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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 bits) to a string?
Reply With Quote
  #4 (permalink)  
Old 02-07-2006, 07:26 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 03-07-2006, 12:00 AM
Junior Member
User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 01:48 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38