-1

I am trying to convert an unsigned long using the sprintf function in c. Code goes like:

char ID[6];
sprintf(ID,"%lu",a.id);

a.id is a number that is passed in that can range from 0 > but I only want the first 6 regardless. Using printf("%lu",a.id); prior to conversion prints the right number but once I try print the string from the char the outcome is 0. Not too sure why this is happening any advice would be much appreciated.

user14063792468
  • 839
  • 12
  • 28
  • 7
    Please provide complete code as a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). That is, exact and complete code that anyone can run to see the problem. – kaylum Apr 13 '21 at 11:11
  • 2
    `but I only want the first 6 regardless.` soooo `a.id % 1000000`? – KamilCuk Apr 13 '21 at 11:13
  • 1
    Does this answer your question? [Converting int to string in C](https://stackoverflow.com/questions/5242524/converting-int-to-string-in-c) – Irelia Apr 13 '21 at 11:13
  • You cannot print a `char` using `%lu` specifier. For functions without parameter list, no conversion to `unsigned long` can be done automatically. – Gerhardh Apr 13 '21 at 12:03
  • @Gerhardh: `sprintf(ID,"%lu",a.id);` does not attempt to print a `char`. (Presumably `a.id` is `unsigned long`.) – Eric Postpischil Apr 13 '21 at 12:47
  • @EricPostpischil I was refering to "once I try print the string from the char the outcome is 0." which sounds that the OP provides a `char`. But maybe they mean the result buffer when they say `char`. – Gerhardh Apr 13 '21 at 13:02

2 Answers2

3

... once I try print the string from the char the outcome is 0

Code is risking undefined behavior (UB): buffer overrun, potential wrong specifier.


but I only want the first 6 regardless.

  1. Insure the buffer is big enough for 6 characters and a terminating null character.

     //char ID[6];
     char ID[6+1];
    
  2. Handle a.id outside the expected range of 0...999999 with % some_unsigned_constant. This does print the last 6.

    // sprintf(ID,"%lu",a.id);
    sprintf(ID,"%lu",a.id % 1000000u);
    
  3. As type of a.id, not posted, 2 steps may be useful to make sure a matching print specifier is used.

    // sprintf(ID,"%lu",a.id % 1000000u);
    unsigned long ul = a.id
    sprintf(ID,"%lu", ul % 1000000u);
    

To print the first six, even if outside the 0...999999 range, use snprintf() which will print only up to the first 6.

   char ID[6+1];
   snprintf(ID, sizeof ID, "%lu", (unsigned long) a.id);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

And the correct way to do what the OP wants is to

#include <stdio.h>

int main()
{
  char ID[6];
  unsigned long x = 3453342432;
  
  int r = snprintf(ID, sizeof(ID), "%lu",x);
  
  printf("%d %s\n", r, ID);

 return 0;
}

Note that the snprintf result is a number of characters that would had been written. It was noted by others that you must reserve one character extra space for terminating NULL.

user14063792468
  • 839
  • 12
  • 28