Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: convert argv[1] to double

Tags:

People also ask

Can argv be modified?

Once argv has been passed into the main method, you can treat it like any other C array - change it in place as you like, just be aware of what you're doing with it.

What is Atoi argv1?

argv is short for argument variable. It will contain all arguments passed on the command line. argv[1] contains the first argument so atoi(argv[1]) will convert the first argument to an int. Follow this answer to receive notifications.

What is argv [] in C?

The second parameter, argv (argument vector), is an array of pointers to arrays of character objects. The array objects are null-terminated strings, representing the arguments that were entered on the command line when the program was started.

Can argv be an integer?

argv[1] is a pointer to a string. You can print the string it points to using printf("%s\n", argv[1]); To get an integer from a string you have first to convert it. Use strtol to convert a string to an int .


I need to pass a double to my program, but the following does not work:

int main(int argc, char *argv[]) {

double ddd;

ddd=atof(argv[1]);

printf("%f\n", ddd);

return 0;
}

If I run my program as ./myprog 3.14 it still prints 0.000000.

Can somebody please help?