如何在Codeblock 10.05中接受命令行参数?

时间:2021-07-07 23:25:09

I am writing a C code in codeblock version 10.05.

我正在编写codeblock版本10.05中的C代码。

The program is:

程序是:

int main(int argc , char *argv[])
{
    printf("Entered number is %s \n", argv[1]);
    return 0;
}

However, when i compile current file, & then run the program, a terminal appears. But, the terminal doesn't wait for command line input & it directly outputs

但是,当我编译当前文件并运行程序时,会出现一个终端。但是,终端不等待命令行输入&它直接输出

<null>

Note that in the above program, i have omitted the code for handling zero number of command line arguments. How can i supply command line arguments?

注意,在上面的程序中,我省略了处理零个命令行参数的代码。如何提供命令行参数?

3 个解决方案

#1


36  

With code::blocks you can set your command line arguments like this in the menu:

使用代码:::block可以在菜单中这样设置命令行参数:

Project > Set programs' arguments...

项目>设置程序的参数…

This opens a window where you can insert your parameters.

这将打开一个窗口,您可以在其中插入参数。

#2


2  

You need create a project before your code if you want you can click Project -> Set Program Arguments.

如果需要的话,您需要在代码之前创建一个项目,您可以单击project -> Set Program Arguments。

#3


0  

The command line arguments in argv are arguments that are passed to your program on the command line when the program is executed. In order to take user input during program execution, you'll need to use more code, e.g. scanf or fgets.

argv中的命令行参数是在执行程序时传递给程序的命令行参数。为了在程序执行过程中接受用户输入,您需要使用更多的代码,例如scanf或fgets。

If you're running your program from an IDE, there should be some way, e.g. project properties, to pass arguments to the program when you run it. For CodeBlocks, check the project menu: Project->Set Program Arguments.

如果您正在从IDE运行程序,那么应该有一些方法,例如项目属性,在运行程序时将参数传递给程序。对于CodeBlocks,检查项目菜单:project ->设置程序参数。

If you can run your program in a terminal, you can pass arguments yourself, e.g.:

如果你能在终端上运行你的程序,你可以自己传递参数,例如:

$ myProgram argument1

Then in your code, argv[1] will contain the string: "argument1".

然后在代码中,argv[1]将包含字符串:“argument1”。

#1


36  

With code::blocks you can set your command line arguments like this in the menu:

使用代码:::block可以在菜单中这样设置命令行参数:

Project > Set programs' arguments...

项目>设置程序的参数…

This opens a window where you can insert your parameters.

这将打开一个窗口,您可以在其中插入参数。

#2


2  

You need create a project before your code if you want you can click Project -> Set Program Arguments.

如果需要的话,您需要在代码之前创建一个项目,您可以单击project -> Set Program Arguments。

#3


0  

The command line arguments in argv are arguments that are passed to your program on the command line when the program is executed. In order to take user input during program execution, you'll need to use more code, e.g. scanf or fgets.

argv中的命令行参数是在执行程序时传递给程序的命令行参数。为了在程序执行过程中接受用户输入,您需要使用更多的代码,例如scanf或fgets。

If you're running your program from an IDE, there should be some way, e.g. project properties, to pass arguments to the program when you run it. For CodeBlocks, check the project menu: Project->Set Program Arguments.

如果您正在从IDE运行程序,那么应该有一些方法,例如项目属性,在运行程序时将参数传递给程序。对于CodeBlocks,检查项目菜单:project ->设置程序参数。

If you can run your program in a terminal, you can pass arguments yourself, e.g.:

如果你能在终端上运行你的程序,你可以自己传递参数,例如:

$ myProgram argument1

Then in your code, argv[1] will contain the string: "argument1".

然后在代码中,argv[1]将包含字符串:“argument1”。