Thread: Small program explained

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    6

    Small program explained

    Hi there,

    I am following a C programming self-study book and was introduced to char and ctype.h with the following example. Unfortunately, the book doesn't thoroughly explain some stuff and I hope you can enlighten me.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void){
        
    /*1*/    char value;
             int number;
        
             printf("\nChoose (o)ctal, (h)exadecimal oder (A)SCII > ");
    /*2*/    value=getchar();
             printf("\nGive decimal number : ");
             scanf("%i",&number);
             switch(toupper(value)){
                case 'O':
    /*3*/         printf("Decimal %i = Octal %o \n",number,number);
                  break;
                case 'H':
                  printf("Decimal %i = Hexadecimal %x \n",number,number);
                  break;
                case 'A':
    /*4*/         if(number<=255)
                     printf("ASCII-Code %i corresponds %c \n",number,number);
                  else
                     printf("This number is too large!\n\a");
                  break;
             }
        
    return 0;
    }
    Ok.
    1) The book explains char as a variable type that covers 1 byte (8bit), so 2^8=256 different signs. Does this mean that each sign is an int and char is just a substitute for 256 possible ints from which one can choose? Moreover, the first 128 signs are standardised in ASCII-code. When I run the program with octal, hexa, or ASCII, I don't quite understand what I should expect and if my C version (or whatever) is correct. When choosing ASCII (option A) and type decimal e.g. 11, I get a sign like this [?] instead of a biological male symbol - other decimals like 8 or 15 also give this question mark. The correct symbol given out according to my book starts not sooner than with the decimal input of 65, which gives the ASCII-sign A. Do I make any mistake somewhere or how can I make all decimals work?

    2) Getchar() is a function, that's why the () are needed. The book says that opposite to scanf, getchar can only be used to read single signs. What does this mean exactly? And can't I write scanf("%f,%i",&abc,&xyz); for reading two variables at once? The way I interpret the use of getchar here is that we have three options and getchar should accept any of these three - 256 would be possible, but we only have three, and any other than "A", "O", or "H" doesn't do something with the program (no case arguments afterwards). Is this interpretation correct? How could this program be written if we wanted to only use ints?

    3) This "number,number" at the end is not logic to me. The decimal I chose is the number and is inserted in %i. But %o, %x or %c refer to the value that I chose with (O)ctal, (H)exa, or (A)SCII. So for me, it should logically be "number,value". I suppose I am wrong. Why?

    4) For the ASCII-option there is an if function given and an argument (number<=255). Why does only the ASCII-option have this argument?

    Thank you very much for your helpful clarifications! They will help me going on with my book.
    Last edited by Loucas; 11-25-2016 at 02:35 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    1) The book explains char as a variable type that covers 1 byte (8bit), so 2^8=256 different signs.
    But realize that a char can be either signed with values between -128 to +127, or unsigned with values of 0 to 255. With most current operating systems a char is actually a signed char.

    Moreover, the first 128 signs are standardised in ASCII-code.
    Yes, the ASCII Code is a 7 bit code, so the only valid codes are 0 - 127.

    When I run the program with octal, hexa, or ASCII, I don't quite understand what I should expect and if my C version (or whatever) is correct.
    The program is setup to print the value entered using either octal (base 8), hexadecimal (base 16), or the ASCII representation of the number. For more information about number bases see this link, for more information about ASCII see this link.

    When choosing ASCII (option A) and type decimal e.g. 11, I get a sign like this [?] instead of a biological male symbol - other decimals like 8 or 15 also give this question mark
    The first 32 characters (0 - 31) in the ASCII table are considered non-printable control characters. The first printable character (character 32) is the space character, the valid last ASCII value is decimal 127 and is also considered a non-printable character.

    2) Getchar() is a function, that's why the () are needed. The book says that opposite to scanf, getchar can only be used to read single signs.
    Actually you probably mean a single char, not a single sign. And be aware that getchar() actually retrieves an int value.

    And can't I write scanf("%f,%i",&abc,&xyz); for reading two variables at once?
    The scanf() function can read one variable or multiple variables using one call to the function.

    The way I interpret the use of getchar here is that we have three options and getchar should accept any of these three - 256 would be possible, but we only have three, and any other than "A", "O", or "H" doesn't do something with the program (no case arguments afterwards). Is this interpretation correct? How could this program be written if we wanted to only use ints?
    The getchar() function is designed to retrieve characters (see the ASCII table) and in this program the variable being used in this function is being used to select one of three display options.

    3) This "number,number" at the end is not logic to me. The decimal I chose is the number and is inserted in %i. But %o, %x or %c refer to the value that I chose with (O)ctal, (H)exa, or (A)SCII. So for me, it should logically be "number,value". I suppose I am wrong. Why?
    The printf() function is printing the number entered using two different specifiers to print the value as a decimal (a int), and either as a hexadecimal, octal, or character representation, which is why it is using the variable "number" twice.

    4) For the ASCII-option there is an if function given and an argument (number<=255). Why does only the ASCII-option have this argument?
    Because the ASCII code is a 7 bit code (actually that if() statement should really be (number <= 127)) because only values between 0 and 127 are valid ASCII numbers.


    By the way what book are you reading?

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Loucas View Post
    1) The book explains char as a variable type that covers 1 byte (8bit), so 2^8=256 different signs. Does this mean that each sign is an int and char is just a substitute for 256 possible ints from which one can choose? Moreover, the first 128 signs are standardised in ASCII-code. When I run the program with octal, hexa, or ASCII, I don't quite understand what I should expect and if my C version (or whatever) is correct. When choosing ASCII (option A) and type decimal e.g. 11, I get a sign like this [?] instead of a biological male symbol - other decimals like 8 or 15 also give this question mark. The correct symbol given out according to my book starts not sooner than with the decimal input of 65, which gives the ASCII-sign A. Do I make any mistake somewhere or how can I make all decimals work?
    To your point 1)


    The ASCII-standard is very old and was introduced at the time as printer and teleprinter (at the begining with daisy wheel or spherical printhead) are coming up.
    The ASCII-table has symbols to control that devices and for comunication over serial lines.
    So, there are ASCII-codes without a optical symbol.
    For more details, look at Control character - Wikipedia.
    You can replace case 'A' with the following code and you should not see the [?]-symbol anymore.
    Code:
    …
                case 'A':
    /*4*/           if(number >= 0 && number <= 255) {
                        printf("ASCII-Code %i corresponds ", number);
                        switch (number) {
                            case 0:
                                printf("'NUL' (Null)\n");
                                break;
                            case 1:
                                printf("'SOH' (Start of Heading)\n");
                                break;
                            case 2:
                                printf("'STX' (Start of Text)\n");
                                break;
                            case 3:
                                printf("'ETX' (End of Text)\n");
                                break;
                            case 4:
                                printf("'EOT' (End of Transmission)\n");
                                break;
                            case 5:
                                printf("'ENQ' (Enquiry)\n");
                                break;
                            case 6:
                                printf("'ACK' (Acknowledge)\n");
                                break;
                            case 7:
                                printf("'BEL' (Bell)\n");
                                break;
                            case 8:
                                printf("'BS' (Backspace)\n");
                                break;
                            case 9:
                                printf("'HT' (Horizontal Tab)\n");
                                break;
                            case 10:
                                printf("'LF' (Line Feed)\n");
                                break;
                            case 11:
                                printf("'VT' (Vertical Tab)\n");
                                break;
                            case 12:
                                printf("'FF' (Form Feed)\n");
                                break;
                            case 13:
                                printf("'CR' (Carriage Return)\n");
                                break;
                            case 14:
                                printf("'SO' (Shift Out)\n");
                                break;
                            case 15:
                                printf("'SI' (Shift In)\n");
                                break;
                            case 16:
                                printf("'DLE' (Data Link Escape)\n");
                                break;
                            case 17:
                                printf("'DC1' (Device Control 1)\n");
                                break;
                            case 18:
                                printf("'DC2' (Device Control 2)\n");
                                break;
                            case 19:
                                printf("'DC3' (Device Control 3)\n");
                                break;
                            case 20:
                                printf("'DC4' (Device Control 4)\n");
                                break;
                            case 21:
                                printf("'NAK' (Negative Acknowledge)\n");
                                break;
                            case 22:
                                printf("'SYN' (Synchronous Idle)\n");
                                break;
                            case 23:
                                printf("'ETB' (End of Transmission Block)\n");
                                break;
                            case 24:
                                printf("'CAN' (Cancel)\n");
                                break;
                            case 25:
                                printf("'EM' (End of Medium)\n");
                                break;
                            case 26:
                                printf("'SUB' (Substitute)\n");
                                break;
                            case 27:
                                printf("'ESC' (Escape)\n");
                                break;
                            case 28:
                                printf("'FS' (File Separator)\n");
                                break;
                            case 29:
                                printf("'GS' (Group Separator)\n");
                                break;
                            case 30:
                                printf("'RS' (Record Separator)\n");
                                break;
                            case 31:
                                printf("'US' (Unit Separator)\n");
                                break;
                            case 127:
                                printf("'DEL' (Delete)\n");
                                break;
                            default:
                                if (number < 128) {
                                    printf("'%c' (ASCII standard)\n", number);
                                }
                                else {
                                    printf("'%c' (locale dependent)\n", number);
                                }
                        }
                    }
                    else {
                        printf("This number is too large!\n\a");
                    }
                    break;
    Last edited by WoodSTokk; 11-25-2016 at 04:24 PM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Code Explained
    By chops11 in forum C Programming
    Replies: 8
    Last Post: 04-06-2011, 09:46 AM
  3. Replies: 2
    Last Post: 06-17-2008, 08:16 PM
  4. Functions Explained
    By CAP in forum C Programming
    Replies: 3
    Last Post: 08-20-2002, 12:39 PM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

Tags for this Thread