将两个字符打包为整数

时间:2014-05-19 01:30:29

标签: c

我编写了一个代码来将两个字符打包成一个整数;它应该以十进制和二进制格式输出。

代码编译成功,但问题是,在我输入第一个字符character1之后,它不是要求我输入character2,而是直接进入自身并到达结束码。我在输入getch()后尝试添加character1,但没有用。我是新来的。任何帮助深表感谢。提前谢谢。

以下是我的代码:

#include <stdio.h>
void displayInBitFormat(char character);
void packCharacters(char character1, char character2 );
char character1, character2;

int main(void){

    puts("Enter the first character");
    scanf("%c",&character1);

    printf("\n%c's bit representation is: \n",character1);
    displayInBitFormat(character1); 

    printf("Enter the second character");
    scanf("%c",&character2);

    getch(); // TRIED ADDING THIS

    printf("\n%c's bit representation is: \n",character2);
    displayInBitFormat(character2);

    puts("");

    packCharacters(character1,character2);

    getch();

}

void packCharacters(char character1, char character2){

    unsigned 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", c);

}

//bit representer of an int
void displayInBitFormat(char 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');
}

2 个答案:

答案 0 :(得分:0)

scanf("%c",&character2);

选择读取第一个字符时输入流上留下的换行符。

将其更改为:

scanf(" %c",&character2);

更新响应@ user3615120的评论

假设您想要阅读第一个字符时输入aEnter。那时,输入流中包含两个字符:'a''\n'。当行

scanf("%c",&character1);

执行后,'a'被读取并存储在character1中。 '\n'仍留在输入流中。

scanf("%c",&character2);

已执行,'\n'被读取并存储在character2中。当您将该行更改为

scanf(" %c",&character2);

丢弃流中剩余的空格。读取第一个非空白字符并将其存储在character2

答案 1 :(得分:0)

使用getchar()的另一种可能方法是通过另一次调用\n手动占用getchar()。此外,如果您要使用它来打印组合表达式,则displayInBitFormat需要采用比char更宽的类型。

#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');
}