int main(int argc, char* argv[]) Stuff

What I want to do is take an integer as input.  How do I declare main?
Would I say:
int main(int argc, int* argv[])???

If so, how then do I copy the argument I passed into a new variable?

Who is Participating?
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Re: http:#20848565
Below is a very simple working example.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
		int n = atoi(argv[1]);
		printf("%d was passed.\n", n);
		puts("Not enough parameters!");
	return 0;

Open in new window

evilrixSenior Software Engineer (Avast)Commented:
Your declaration of main is correct. The numeric value will be passed into main in a stringized format as one of the values of argv. Assuming you pass it in as your first command line parameter it'll be in argv[1]. You can then use atoi() to convert it to an integer value.

Something like...
int n = atoi(argv[1]);
int new;
if( argc > 1 ){
  new= atoi(argv[1]);
i doubt if you can change the signature of main method as:
int main(int argc, int* argv[])

what you can do is to convert char* into integers. this you can do using atoi method.

check out:

Hope it helps.
All Courses

From novice to tech pro — start learning today.