Thread: C Program Input Single 4 digit Hex Number Output Next 10 Hex Numbers Using Array

  1. #1
    Registered User
    Join Date
    Mar 2016

    Question C Program Input Single 4 digit Hex Number Output Next 10 Hex Numbers Using Array

    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.

    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!

    #include <stdio.h>
    int main(void) {
        int n, hexNum[4] = { 0 };
        int i, j;
        printf("Enter the number ");
        scanf("%hhhhx", &n);
        for (j = 0; j<10; j++)
            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;

    Last edited by st4evr; 03-05-2017 at 09:38 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    How about starting with a really simple example.
    #include <stdio.h>
    int main(void) {
      int n;
      printf("Enter hex number\n");
      printf("hex +1 = %x\n", n+1);
      return 0;
    $ gcc foo.c
    $ ./a.out 
    Enter hex number
    hex +1 = abc2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to get Reverse of 5-digit number as output
    By Anish Kuriakose in forum C Programming
    Replies: 12
    Last Post: 01-14-2014, 03:29 PM
  2. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  3. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  4. Replies: 7
    Last Post: 07-16-2009, 12:56 AM
  5. Verifying single digit input
    By Syked4 in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 07:11 PM

Tags for this Thread