C从CMD获取输入

标签 c

如何使用提示获取输入?我尝试将下面的代码编译为“a.exe”并从 CMD 执行它,如“gcc a.exe 5”,但它没有像预期那样输出数字。

#include <stdio.h>

int main(int a)
{
    printf("%d\n", a);
    return 1;
}

我在安装编译器时是否做错了什么,或者我是否尝试错误地运行它?

最佳答案

你的main()参数是错误的,你应该这样做:

int main(int argc, char **argv) {
     if(argc > 2) {
         printf("%s\n", argv[2]);
     }
     else {
         printf("No arguments\n");
     }          
}

请注意,int argc 表示参数的数量,char **argv 是一个包含所有参数的数组,作为字符串,包括“gcc”、“a. exe”等 在您的情况下,如果您以这种方式运行程序:gcc a.exe 5,您的参数将为:argc = 3, argv = ["gcc", "a.exe", “5”]

关于C从CMD获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584898/

相关文章:

c - 是什么导致 "incompatible implicit declaration"警告?

c - HTTP 通过 C 中的套接字 : Basic file server fails to properly return file

c - 如何在结构中使用结构?

c - 尝试在 c 中分配内存函数?

c - ptrdiff_t 在 C 中定义在哪里?

c - 与线程共享变量的最佳方式

通过循环将数据复制到整数指针数组?

c++ - C/C++ : Is it possible to configure the Eclipse project settings to accept void pointer without casting for all Linux distros

c - 格式 '_' 需要类型为 '__' 的参数,但参数 2 的类型为 '__' 。 C编程,我是初学者

c - 错误 MSB3073 : The command "staticdv.exe/devenv/check" exited with code -1