segmentation fault with sprintf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    segmentation fault with sprintf

    Hi, I am trying to convert integer to string.

    this is my code -

    Code:
    long num = 23456789;
    char buff[20];
    sprintf(buff, "%s", num);
    printf("%s\n", buff);
    but I am getting segmentation fault in sprintf() call.

    Any idea why this is coming?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    That is because you have used %s which is the code for a string and then passed an integer.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      By the way, when you replace "%s" with the format code for an integer, don't forget to include a qualifier indicating that it is a long integer.

      Comment

      Working...