Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read in numbers as command arguments?

Tags:

How can make it where the program reads any two integers input before the program is run? I want the output to look like this, with x and y being any variables typed in (I am using Cygwin):

$ ./a x y

product of x and y

sum of x and y

I used int main(int argc, char *argv[]). I tried to assign argv[2] to x and argv[3] to y, but when I compile the program it says assignment makes integer from pointer without cast. What does this mean and how do I fix it?

like image 609
Kaity Avatar asked Feb 07 '09 05:02

Kaity


People also ask

How do you pass values into command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do I open command line arguments?

option. You can test command line arguments by running an executable from the "Command Prompt" in Windows or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run.

What are the command arguments?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.


1 Answers

Assuming the C language:

  • Command line arguments are found in the argv array - argv[1], argv[2] etc.
  • Converting a string argument to an integer can be done with the atoi function.
  • Output can be done with the printf function.

[Trying to teach you to fish, rather than providing a fish. Good luck!]

like image 169
Paul Beckingham Avatar answered Oct 12 '22 23:10

Paul Beckingham