c - 如何解释用户输入的字符串?

标签 c string embedded ascii type-conversion

int main()
{
printf("Welcome to the temperature control program:\n");
    printf("enter the ascii temperature characters");
    printf("enter the ascii characters of an euipment name with + or - and followed by a integer value");
    printf("type character terminated by carriage return");

    char tbuffer [tbuffer_length];
    printf("enter the temperature value");
    scanf("%7c", tbuffer);
    char *t;
    t = fgets (tbuffer, tbuffer_length, stdin );
    if(tbuffer[0] = 'A')
    {

    }
    if (tbuffer[0] = 'B')
    {

    }
    if (tbuffer[0] = 'C')
    {

    }

return 0;
}

在这种情况下,用户输入字符串值(例如:char buffer[7]= {'A', '+', '2', '5', '.','5','\r'} ) 如何将此字符串解释为 A 有设备 A 或 B 有设备 B,+25.5 有温度值(我想将此温度值转换为整数)和回车。解释并将其转换为整数后,我需要通过接口(interface)发送(我知道如何通过接口(interface)发送)。给我一些解释上的想法。

最佳答案

使用==运算符比较值

int main()
{
printf("Welcome to the temperature control program:\n");
    printf("enter the ascii temperature characters");
    printf("enter the ascii characters of an euipment name with + or - and followed by a integer value");
    printf("type character terminated by carriage return");

    char tbuffer [tbuffer_length];
    printf("enter the temperature value");
    scanf("%7c", tbuffer);
    char *t;
    t = fgets (tbuffer, tbuffer_length, stdin );
    if(tbuffer[0] == 'A')
    {

    }
    if (tbuffer[0] == 'B')
    {

    }
    if (tbuffer[0] == 'C')
    {

    }

return 0;
}

关于c - 如何解释用户输入的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16794575/

相关文章:

c - 欧文 : webapi call returning 401 unauthoried Error in Azure

c++ - 在不同字节中写入位 c c++

C++:使用sizeof的char数组的大小

linux - 字符串命令: 3 rows near.

c - 外部 RAM 工作和内存管理

c++ - 为什么这个文件不写?

c - 为什么在c语言中扫描输入的字符串时,在scanf中不指定 '&'符号?

eclipse - 如何通过 JSP 中的按钮单击来调用函数

c - AVR 定时器溢出中断不工作

c - 制作一个简单的C头文件