getchar vs scanf

can any one tell me the difference b/w

int a[n];

for(int i=0;i<n;i++)

{

a[i]=getchar()-48;

}

and

int a[n];

for(int i=0;i< n;i++)

{

scanf("%1d",&a[i]);

}

getchar() reads one character at a time when we substract 48 from the ascii value of the character we get the desired int eg. if we have to read 1( ‘1’-48=1 );
in the scanf %d specifier automatically convert it into int. (1 is used before d just to read one character at a time)

2 Likes