Packing two characters to an integer

后端 未结 2 913
耶瑟儿~
耶瑟儿~ 2021-01-23 14:53

I have written a code to pack two characters to an integer; which should output in both decimal and binary formats.

The code compiled successfully, but the problem is,

相关标签:
2条回答
  • 2021-01-23 15:41

    One other possible approach using getchar() is to manually eat the \n with another call to getchar(). Also, your displayInBitFormat needs to take a wider type than char if you are going to use it to print the combined expression.

    #include <stdio.h>
    #include <stdlib.h>
    
    void displayInBitFormat(unsigned int character);
    void packCharacters(char character1, char character2 );
    int character1, character2;
    
    int main(void){
        puts("Enter the first character");
        character1 = getchar();
    
        // Eat the newline that came from reading the first character.
        getchar();
    
        printf("\n%c's bit representation is: \n",character1);
        displayInBitFormat(character1); 
    
        printf("Enter the second character");
        character2 = getchar();
    
        printf("\n%c's bit representation is: \n",character2);
        displayInBitFormat(character2);
        puts("");
        packCharacters(character1,character2);
    }
    
    void packCharacters(char character1, char character2){
    
        unsigned int c;
    
        c=character1; // assigning first character with unsigned
    
        c=c<<8; // shifting first char to 8 bits left
        c=c|character2; //bitwise inclusive OR 
    
        printf("After packing characters, Result in binary is: \n");
        displayInBitFormat(c);
        puts("\nResult in decimal number is: ");
        printf("%u\n", c);
    }
    
    //bit representer of an int
    void displayInBitFormat(unsigned int character){
    
        unsigned int i;//counter
    
        unsigned int displayMask=1<<31;
        printf("%10u=",character);
    
        for (i=1;i<=32;++i)
        {
            putchar(character&displayMask ? '1':'0');
    
            character<<=1; 
            if (i%8==0){
            putchar(' ');
        }
    }
    putchar('\n');
    }
    
    0 讨论(0)
  • 2021-01-23 15:45
    scanf("%c",&character2);
    

    picks up the newline character that was left on the input stream when you read the first character.

    Change it to:

    scanf(" %c",&character2);
    

    Update In response to the comment by @user3615120

    Let's say you entered a and Enter when you wanted to read the first character. At that time, the input stream has two characters in it: 'a' and '\n'. When the line

    scanf("%c",&character1);
    

    gets executed, 'a' is read and stored in character1. The '\n' is still left in the input stream.

    When

    scanf("%c",&character2);
    

    is executed, the '\n' is read and stored in character2. When you change that line to

    scanf(" %c",&character2);
    

    the white spaces that are left in the stream is discarded. The first non-whitespace character is read and stored in character2.

    0 讨论(0)
提交回复
热议问题