Hello, I'm a newbie in C but I know how to code in C++ and I feel like I'm struggling with more complicated problems because I can't get my head around some basic principles. For instance, I know that the algorithm of determining each digit of a number in C++ is this one:

Code:

#include <iostream>
using namespace std;
int n, lastDigit;
int main()
{
cin>>n;
while(n!=0)
{
lastDigit=n%10;
n=n/10;
cout<<lastDigit<<' ';
}
return 0;
}

If my input for this was 548 I would get 8 4 5. Now, what I tried to do was 'translate' this basic algorithm from C++ to C, to better understand how I could obtain the digits of a number in C:

Code:

#include <stdio.h>
int digits(int n){
int uc;
while(n!=0){
uc=n%10;
n=n/10;
}
return uc;
}
int main(){
int x;
x=548;
printf("The digits of the number %d are %d", x, digits(x));
return 0;
}

However, for the same number 548, I get the message "The digits of the number 548 is 5". And I just don't understand why my while is being treated as an if in this instance. Please explain as detailed as possible, as I really want to understand. Thank you!!