Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

strtod - convert string to double in C

September 8, 2013 Posted by: GPA Views: 588 0 comments

Problem

How does strtod parse the string in C programming?

Solution

In this article, you will learn to convert a sequence of characters representing a floating-point
value to double. For it you should include header file <stdlib.h> and use function strtod().

Declaration:

double strtod(const char *inputString, char **endinputString ); 

where inputString is the string to be converted;

endinputString - pointer to a string, it is the end of inputString after a floating-point value.

Example:

inputString = "59.65$ - 59.65 dollars" then this function will return 59.65 and endinputString will be "$ - 59.65 dollars".

 

Example:

   

#include <stdio.h>
#include <stdlib.h>//use this header for function strtod
//main function
int main(int argc, char *argv[])
{  
char* inputString;//input string
char* outputString;//output string
  printf("globalproganswer.com\n");
  printf("Enter string (example \"59.65$ - 59.65 dollars\"): ");//promt the user to input string
  scanf("%s",&inputString);//read string from keyboard
  double inputValue=strtod(&inputString,&outputString);//parce inputString to double and string using function strtod
  printf("The input double value is %0.3f.\n\n",inputValue);//show result
  system("PAUSE");//delay
  return 0;//retun 0 exit program
}

Output

strtod - convert string

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe