Search code examples
catoi

Using with atoi() in C


I'm trying to use atoi() but I need the 0 in the front, but the function ignores it

  sprintf(str, "%d%d%d%d",comp[cont][0],comp[cont][1],comp[cont][2],comp[cont][3]);
  conv=atoi(str);  

  printf("%d \n",conv);  

When I print str: 0100
And conv: 100
Is there any way to show the 0?


Solution

  • It's because integers simply doesn't have zeros in front of them.

    You need to print it with that:

    printf("%04d \n",conv);
    

    You might find e.g. this printf reference useful.