write a program that allows integer input only.

I have the solution but cannot understand it…

#include 
main()
{
    int x=0;
    x=getIntegerOnly();
    printf("\nYou have entered %d",x);
    getch();

}
int getIntegerOnly()
{
    int num=0;
    int ch;
    for(;;)
    {    ch=getch();
        if(ch>=48 && ch<=57)

        {

           printf("%c",ch);
            num=num*10+(ch-48);
          }
           if(ch==13)
            break;
    }
    return num;
}
Please explain how getch() function is working.What value gets stored in ch when we use ch=getch();.
When we declare ch as int then how does in the printf statement the value of integer gets printed even if we use %c format specifier?

getch takes a character input which is storing into int variable so ASCII value get stored in ch and again when using printf %c is used ch is again convert into character from its ASCII value.

13 is the ASCII value of carriage return so when enter is pressed function breaks and return number input.

I think it is clear now.

//