سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Just change the printf format:

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

Just append the zero while printing. Try the following

print("0%d\n", conv);

Why so complicated and not just do directly:

printf("%d%d%d%d", comp[cont][0], comp[cont][1], comp[cont][2], comp[cont][3]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top