只有一个scanf()值正在执行并正在执行,但在运行时它将获取所有值但未正确执行

时间:2014-04-16 14:09:12

标签: c gcc scanf

这是我的程序,我正在使用linux GCC

#include<stdio.h>
main()
{
    int data,bitpos;
    printf("choose the choice\n 's'=>setbit\n 'c'=>clearbit\n 't'=>testbit\n 'm'=>complementbit\n");
    char ch=getchar();
    switch(ch)
    {
    case 's':printf("enter data & bit position:");
             scanf("%d",&data);
             scanf("%d",&bitpos);
             data|=(1<<bitpos);
             break;
    case 'c':printf("enter data & bit position:");
             scanf("%d",&data);
             scanf("%d",&bitpos);
             data&=(1<<bitpos);
             break;
    case 't':printf("enter data & bit position:");
             scanf("%d",&data);
             scanf("%d",&bitpos);
             data=((1<<bitpos)&data)?1:0;
             break;
    case 'm':printf("enter data & bit position:");
             scanf("%d",&data);
             scanf("%d",&bitpos);
             data^=(1<<bitpos);
             break;
    default:printf("INVALID CHOICE\n");
    }
    printf("%d\n",data);
}`

所有逻辑&amp;想要输出正在变得正确。但是当我试图扫描数据&amp;主要乐趣中的bitpos值正在执行错误......

#include<stdio.h>
main()
{
        int data,bitpos;
        printf("enter data & bit position:");
        scanf("%d",&data);
        scanf("%d",&bitpos);
        printf("choose the choice\n 's'=>setbit\n 'c'=>clearbit\n 't'=>testbit\n 'm'=>complementbit\n");
        char ch=getchar();
        switch(ch)
    {
    case 's':data|=(1<<bitpos);
             break;

    case 'c':data&=(1<<bitpos);
             break;

    case 't':data=((1<<bitpos)&data)?1:0;
             break;

    case 'm':data^=(1<<bitpos);
             break;

    default:printf("INVALID CHOICE\n");
    }
    printf("%d\n",data);
}

我得到的错误输出低于

[root@localhost test]# ./a.out
enter data & bit position:127 5
choose the choice
's'=>setbit
'c'=>clearbit
't'=>testbit
'm'=>complementbit
INVALID CHOICE
127

0 个答案:

没有答案