Beginner in C programming here and processing a small programming task here to help me understand a bit how hexadecimal numbers work in programming. Reading a book with an introduction to hexadecimals and other base counting methods used by computers. Trying to understand hexadecimals in C.
The small program below is supposed to accept input of a single hexadecimal number and output the next ten digits after it.
Example:
Input ABC1
Output ABC2, ABC3, ABC4, ABC5, ABC6, ABC7, ABC8, ABC9, ABCA, and ABCB.
Current output:
Enter the number ABC1
0001 0002 0003 0004 0005 0006 0007 0008 0009 000a
Obviously my current lack of understanding is keeping me from seeing the error in the code as I am new to counting in hex. I have tried several things, but this is the closest I've come so far.
Would anyone be so kind as to help me identify what I am doing wrong and advise what needs to be corrected? So that I may add it to my notes.
Thank you very much in advance for your time and help!
Code:
int main(void) {
int n, hexNum[4] = { 0 };
int i, j;
printf("Enter the number ");
scanf("%hhhhx", &n);
for (j = 0; j<10; j++)
{
hexNum[3]++;
for (i = 3; i>0; i--)
{
if (hexNum[i] == 16)
{
hexNum[i - 1]++;
hexNum[i] = 0;
}
}
printf("\n%x%x%x%x\n", hexNum[0], hexNum[1], hexNum[2], hexNum[3]);
}
return 0;
}