29

I am writing a C code in codeblock version 10.05.

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?

Green goblin
  • 9,898
  • 13
  • 71
  • 100

4 Answers4

48

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

Project > Set programs' arguments...

This opens a window where you can insert your parameters.

xQuare
  • 1,293
  • 1
  • 12
  • 21
  • 2
    Just notice that codeblocks is more bug than my application :) . Codelite is more stable. – tncas Mar 10 '13 at 05:15
5

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

To Son Nguyen
  • 51
  • 1
  • 1
1

Code Blocks' Project - set programs' arguments (then type arguments in lower textbox of the pop-up dialog) should supply just what you type to your program when it starts. (CodeBlocks also can run on Ubuntu, as well as Windows, btw.) To make sure your code is processing arguments, you might Start - CMD.EXE under Win, navigate (e: cd \homework\projectname ) to your project directory, then cd bin\Debug. do a DIR and you'll see an EXE file. That is the part of your program which runs. Type its name, a space, then some arguments before you hit ENTER. See if your code is processing the args OK. (make sure main looks like: int main(int argc, char *argv[]) then in main() have a statement: cout << argv[1] << endl ; that should print the first word you type after the name of the EXE file.

Theoni
  • 11
  • 1
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.

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.

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".

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 3
    Well, my requirement is not to take input at execution time. I just want to take input from command line argument.The above program works perfectly with the ubuntu platform v 12.04. Why i am not able to take the command line argument under codeblock environment? – Green goblin Aug 09 '12 at 17:15