【问题标题】:How can I take input of two different data type using scanf() from same line?如何使用 scanf() 从同一行输入两种不同的数据类型?
【发布时间】:2021-02-14 05:31:27
【问题描述】:

我必须在两个不同的变量中接受这样的输入。

例如

I am an engineering student, 34.

字符串:- 我是一名工科学生。
整数:- 34

【问题讨论】:

    标签: c string input scanf format-specifiers


    【解决方案1】:
    #include <stdio.h>
    int main()
    {
        
        char str[100];
        int age;
        scanf("%[^,],%d",str,&age);
        printf("str=%s\nage=%d",str,age);
        return 0;
    }
    

    scanf可以设置终止字符

    【讨论】:

    • "%[^,] 缺少宽度限制,比gets() 差。考虑"%99[^,] ,%d" 并检查scanf() 返回值。
    猜你喜欢
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多