Open In App

ecvt() in C/C++ with Examples

Last Updated : 28 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This function ecvt() converts the double value to string value and returns a pointer to it. The library function defined in stdlib.h header file.

Syntax:

char * ecvt (double value, int num, int * dec, int * sign);

Parameters:

  • double value: It is the double value which will convert into string.
  • int num: It is a number of digits to be returned by the function.
  • int * dec: It is integer pointer, which stores the decimal point position with respect to beginning the string.
  • int * sign: It is integer pointer, which receives the sign indicator like 0 means positive sign and non-zero means negative.

The function return a character string terminated with null with same length specified as num that contains the digits of the double number passes as parameter.

Below is the program to illustrate the use of ecvt() function:

C




// C program to illustrate the
// use of ecvt() function
#include <stdio.h>
#include <stdlib.h>
  
// Function to illustrate the
// use of ecvt() function
void useOfEcvt()
{
    double x = 123.4567;
    char* buf;
    int dec, sign;
  
    // Function Call
    buf = ecvt(x, 6, &dec, &sign);
  
    // Print the converted string
    printf("The converted string "
           "value is: %c%c.%sX10^%d\n",
           sign == 0 ? '+' : '-', '0', buf, dec);
}
  
// Driver Code
int main()
{
    // Function Call
    useOfEcvt();
  
    return 0;
}


Output:

The converted string value is: +0.123457X10^3

Explanation: In the above C program, the double(float) value i.e., 123.4567 is converted into the string value(+ 0.123457X103) using ecvt() function.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads