Progamming Languages: C


We can see the relationships among octal, decimal, and hexadecimal numbers with the aid of this trivial C language program

This program demonstrates the relationships among Decimal (base 10), Octal (base 8), and Hexadecimal (base 16) numbers. Here is the source code:

#include 
        int inbuf; 
/* This C program prompts the user to enter a decimal number at the keyboard, */
/* converts that nubmer to Hexadecimal and Octal, and displays the results. */
//* It contains an example of a do...while control loop. */

main()
{
    printf("\n\nBase systems demonstration. Enter `0' to quit.");
      do
        {
        printf("\n\nEnter a Decimal number: ");
        scanf("%d",&inbuf);

        printf("\tDecimal: \t%d",inbuf); 
        printf("\tHexadecimal: \t%x",inbuf); 
        printf("\tOctal: \t%o",inbuf); 
        } 
      while (inbuf > 0);
    printf("\nGoodbye.\n");
}

Here is what the output might look like:

Base systems demonstration. Enter `0' to quit.
Enter a Decimal number: 10
Hexadecimal:    a       Decimal:        10      Octal:  12
Enter a Decimal number: 15
Hexadecimal:    f       Decimal:        15      Octal:  17
Enter a Decimal number: 16
Hexadecimal:    10      Decimal:        16      Octal:  20
Enter a Decimal number: 0
Hexadecimal:    0       Decimal:        0       Octal:  0
Goodbye.

There is no error-handling built into this program. Consequently, when an unexpected piece of data is input, the program may behave in unpredictable ways. What might happen if a user entered a letter of the alphabet instead of a number? What might happen if a user entered a negative number, or a non-integer? How are letters of the alphabet stored in computer systems? If you are not sure what these terms mean, look them up in your copy of the Dictionary of Computing and Internet Terms, or visit an online computing encyclopedia like Whatis.com.

Copyright © Christopher Brown-Syed 1995-2001. Disclaimers.