w3resource logo

:

C Programming Exercises

C Exercises: Convert a string to double

C Variable Type : Exercise-4 with Solution

Write a C program to convert a string to a double.

Sample Solution:

C Code:

#include<stdio.h>
#include<stdlib.h>

int main ()
	{
		char input[] = "8.0 2.0";
		char * ptr_end;
		double x, y;

		x = strtod (input,&ptr_end);
		y = strtod (ptr_end,NULL);

		printf ("\nOutput= %.2lf\n\n", x/y);
		return 0;
	}
	

Sample Output:

Output= 4.00 

C Programming Code Editor:

Improve this sample solution and post your code through Disqus