【问题标题】:c - how do you accept input without using scanf (using input from compile command in terminal)?c - 如何在不使用 scanf 的情况下接受输入(使用来自终端编译命令的输入)?
【发布时间】:2023-02-22 04:25:55
【问题描述】:

我是 c 的初学者,我正在尝试接受来自 zsh 终端的输入

./filename 1234 5678

因此,我不想在编译和运行的地方使用 scanf 然后提示输入数字,而是想编写上面的命令并直接在我的代码中使用 1234 和 5678。

我怎么做?

【问题讨论】:

  • 这些被称为命令行参数,看看你的main签名int main(int argc, char* argv[]){ ... }中的argcargv

标签: c terminal compilation user-input execute


【解决方案1】:

尝试以下一次:

#define INT_CONVERTED       (1 << 0)
#define FLOAT_CONVERTED     (1 << 1)

char *strlwr(char *str)
{
    char *ptr = str;

    while (*ptr)
    {
        *ptr = tolower(*ptr);
        ptr++;
    }
    return str;
}


int NumberOfDots(char *s)
{
    int dots = 0;
    while (*s)
        dots += *s++ == '.';
    return dots;
}

int NOTstrcasechr(char *str, int ch)
{
    return strchr(str, ch) == NULL && strchr(str, toupper(ch)) == NULL;
}

int ReadNumber(double *db, int *in)
{
    int result = 0;

    do
    {
        char str[100];
        int dots;

        result = 0;
        printf("Enter number: ");
        fgets(str, 100, stdin);
        if ((dots = NumberOfDots(str)) > 1) str[0] = '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 2021-11-04
  • 1970-01-01
相关资源
最近更新 更多