Sprintf and int conversion

Hello, I'm learning C and sometimes it's rough :confused:

when I do

char compteur[6];
unsigned long x = 12345;
sprintf (compteur, "%d", x);

my output look like this : "12345"

when I do

char compteur[6];
unsigned long x = 123456;
sprintf (compteur, "%d", x);

my output look like this : "-7616"

Why ?

What is the maximum value of a 16 bit int?

Ok I'm tired. But the sprintf is something !

So I need to change the maximum value of my char.
I tried long char. No success.

I'd like to have at least 666666 values...

I'd like to have at least 666666 values..

The grand-daddy of The Beast?

Try "long". (%ld)

I think you want "%ld" (long integer in signed decimal) instead of "%d" (integer in signed decimal).

Ok I get it !
Thanks for the help.

Don't forget to make the buffer big enough too.