• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.