Printing leading zeros in a number in C

user9994319 picture user9994319 · Aug 18, 2018 · Viewed 13.7k times · Source

I'm working on a project and I want my program to read a strictly 5 digits number with a leading zero.

How can I print the number with the leading zero included?
Plus: how can I provide that my program reads 5 digits including a zero as a leading number?

Answer

kiran Biradar picture kiran Biradar · Aug 18, 2018

I assume you want to read in int variable. If so you can try the below solution.

#include<stdio.h>
void main()
{
  int a;

  scanf("%5d", &a);
  printf("%05d",a);

}