sprintf (covert int to char[])-Collection of common programming errors

In my program I try to convert a int to a char[20];

I try to do this in the following way:

  char str[20];    
  sprintf(str, "%d", timer);

in which timer is the int.

But when i build this code, i get the following warnings…

Type implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]   
incompatible implicit declaration of built-in function 'sprintf' [enabled by default]   

what does that mean?

note:( i have included string.h and stdlib.h).

great, i added stdio.h to my code and now the warnings dissapeard only to give me a even harder error. Any suggestions?

undefined reference to `_sbrk’

  1. You have to #include to use sprintf()

  2. you want to make sure you also add reference to

    stdio.h

    see this ref

  3. You probably need to put sprintf(str, "%d", timer) inside a function (not on the global part of the source code).

    Something like:

    #include 
    char str[20];
    // SPOT #1
    int f() {
        sprintf(str, "%d", timer); // this won't work if placed on SPOT #1
    }
    

Originally posted 2013-11-09 23:14:02.