View Single Post

  #3 (permalink)  
Old 07-02-2006, 06:10 PM
Rick Rick is offline
Junior Member
 
Join Date: Jul 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