How to take command line argument in Codeblock 10.05?

Viewed 83453

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?

4 Answers

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.

Related