Results 1 to 2 of 2
Hi,
I'm writing a program in C, how can i specify a value with the -l command line option? and how do i use -s <num> option?
And how do ...
- 04-05-2007 #1Just Joined!
- Join Date
- Feb 2007
- Posts
- 12
Various programming issues
Hi,
I'm writing a program in C, how can i specify a value with the -l command line option? and how do i use -s <num> option?
And how do i pass an integer argument to the main function? how do i make sure an integer is passed and not a string? like instanceOf in Java?
thanks
- 04-05-2007 #2
So if you want to obtain commandline options, you can either process the argv array yourself, or use the glibc getopt function:
http://www.gnu.org/software/libc/man...pt.html#Getopt
As for passing an integer argument, argv consists entirely of strings. You can convert one to an int using glibc's atoi() function (see the man page) or manually using code rather like the following:
val now holds the value of the string in int form. This of course assumes that string is a base-10 integer. It relies on the pow function which is in math.h.Code:char *string = "123"; int len = strlen(string); int val = 0; int i; for(i = 0; i < len; i++) { int pow = len - 1 - i; char digit = string[i]; int digit_val = digit - '0'; val += digit_val * pow(10, pow); }DISTRO=Arch
Registered Linux User #388732


Reply With Quote