Page 2 of 5
Posted: Thu Dec 07, 2006 5:40 am
by neel_basu
Suppose I Want to Make A C Programm That Will Take An Integer Through Commandline And Out Put That Integer*5 And I'll Pass That Int Through php And Get The Answer Through php Then What Would Be The C Code To Build That Program ??
Posted: Thu Dec 07, 2006 5:44 am
by volka
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
if ( argc > 1 ) {
long x = strtol(argv[1], NULL, 10);
printf("%ld", x*5);
}
return 0;
}
Can a moderator please move this thread to
Miscellaneous?
Posted: Thu Dec 07, 2006 5:50 am
by neel_basu
Would You Please Tell Me How Does It Works ??
And How I Have To Pass That Integer Argument To It ??
Posted: Thu Dec 07, 2006 5:54 am
by volka
Arguments are available as an array of (zero terminated) strings to a C program.
argv[1] is the first parameter/argument passed to the program, e.g. yadda.exe firstArgument -> argv[1]<=>firstArgument
To convert a string to a number you may use strtol, see
http://msdn2.microsoft.com/en-us/w4z2wdyc(VS.80).aspx or your local msdn help.
Posted: Thu Dec 07, 2006 6:05 am
by neel_basu
Hello Sir
I Am Very Sorry To Post It In THis Forum But While Posting I Didnt Know
That The Topic Will Turn Like This
Would You Please tell Me How Can I Pass Two Arguments To That Program ?
Whats The Separator Is it Space Or comma
Posted: Thu Dec 07, 2006 6:06 am
by volka
volka wrote:the arguments are separated by whitespaces.
yadda.exe firstArgument secondArgument
access them via argv[1] and argv[2]
Posted: Thu Dec 07, 2006 6:14 am
by neel_basu
Then Whats The Work Of argc What Does It Do ??
Posted: Thu Dec 07, 2006 6:15 am
by volka
argument counter, the number of arguments.
Posted: Thu Dec 07, 2006 6:17 am
by neel_basu
How Can I Separate 2 or 3 arguments By Spaces Or By commas ??
Posted: Thu Dec 07, 2006 6:19 am
by volka
Posted: Thu Dec 07, 2006 6:24 am
by neel_basu
argv[1] is Assidned To The First Argument I Send
argv[2] is Assigned To The Second Argument I Send
Suppose My First Argument Is arg1 And Second Argument Is arg2
how Would I Write This ??
Like This
Or In some Other Way !!
Posted: Thu Dec 07, 2006 6:24 am
by ok
There are some rules for passing arguments to a program. One of them is that the arguments have to be separated by signal space.
If you want to separate them by commas, you can do it, but in the program you will have to separate them (C won't do it for you!)
Posted: Thu Dec 07, 2006 6:25 am
by volka
a) already answered
volka wrote:yadda.exe firstArgument secondArgument
b) This one you really could've tried yourself.
Posted: Thu Dec 07, 2006 6:26 am
by neel_basu
So What is The Default Separator ??
Posted: Thu Dec 07, 2006 6:27 am
by neel_basu
Sorry
Ok So How Can I Separate The Input Arguments By a Comma ??