RE: Using argv[1] in C program

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
Delete this message
Reply to this message
Author: Joseph Sinclair
Date:  
To: plug-discuss
Subject: RE: Using argv[1] in C program
Dave,
    Actually, you may wish to use "strtoul(argv[1], NULL, 10)" instead 
of the atoi call, since atoi will silently return MAX_INT if the input 
exceeds the range of a standard integer, and may return negative numbers 
(which would result in very odd sleep times in some cases as the 
negative integer was interpreted as an unsigned value).


==Joseph++

>>>
>>> I think what you're looking for is atoi(char*). (see `man atoi` for
>>> other options).
>>>
>>> The parameter to usleep needs to be an unsigned long.
>>>
>>> Bart
>>
>>
>
>
>Thank you. It works now. I had figured out how to take command line
>arguments, but needed the atoi() function to finish this off.
>
>shell> vi usleep.c
>
>int main(int argc, char** argv) {
>  if ( argc > 1 )
>    usleep(atoi(argv[1]));
>}

>
>shell> make usleep
>shell> cp usleep /usr/local/bin/
>shell> usleep 250000 # sleep for a quarter second
>
>Now my system has usleep on it!! Awesome, that was fun.
>
>
>
>
>
>
>