RE: Using argv[1] in C program

Página superior
Adjuntos:
Obtener este mensaje como un correo
+ (text/plain)
+ (text/html)
Eliminar este mensaje
Responder a este mensaje
Autor: Joseph Sinclair
Fecha:  
A: plug-discuss
Asunto: 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.
>
>
>
>
>
>
>