Input from the execution line in the terminal in c

The problem that i have is that i have to write a hanois tower game in c and the input for the number of the rings must not be in the programm but the code must read the number of rings in the execution.

Example: ./hanoistower 3

And the code should get the 3 as the input. How can i do that?

Answers


Command line arguments are propagated as strings through the main() function of your C program.

In int main(int argc, char *argv[]) argc is the number of arguments, and argv is an array of strings containing the arguments. Note that the program name itself is always the first "argument".

As the arguments are passed as strings, you likely need to convert your 3 to an integer, which can be done with the atoi function. Here's a start:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int rings;
   if(argc != 2) {
       printf("Usage: %s number-of-rings\n",argv[0]);
       return 1;
   }

   rings = atoi(argv[1]);
   printf("Using number-of-rings = %d\n", rings);
...

   return 0;
}

Need Your Help

First app (developer.android.com) errors, what is wrong?

java android xml eclipse

So I have followed the tutorial on https://developer.android.com/training/basics/firstapp/index.html trying to build an app, and I've written all the code now but have got a few errors. I am using

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.