Open In App
Related Articles

fcvt() in C/C++ with Examples

Improve Article
Save Article
Like Article

This function fcvt() converts the floating-point value to NULL-terminated ASCII string and returns a pointer to it. It is defined in the library function defined in stdlib.h header file.


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


1. double value: It is the floating-point value that will convert into a string.
2. int num: It is a number of digits to be returned by the function. If this is greater than the number of digits in value then the rest of the string is padded with zeros and if it is smaller the low-order digit is rounded.
3. int * dec: It is an integer pointer, which stores the decimal point position with respect to beginning the string. If it is zero or less than zero, indicates that the decimal point lies to the left of the digits
4. int * sign: It is an integer pointer, which receives the sign indicator like 0 means positive sign and non-zero means negative.

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

Below are the C programs to illustrate the use of fcvt() function:

Example 1:


// C program to illustrate the
// use of fcvt() function
#include <stdio.h>
#include <stdlib.h>
// Function to illustrate the
// use of fcvt() function
void useOfFcvt()
    double x = 123.4567;
    char* buf;
    int dec, sign;
    // Function Call
    buf = fcvt(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
    return 0;


The converted string value is: +0.123456700X10^3

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

Example 2:


// C program to implement
// FCVT() function
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main(void)
    char* string;
    double value;
    int Dec, sign;
    int ndig = 10;
    value = -9.876;
    string = fcvt(value, ndig,
                  &Dec, &sign);
    printf("The converted string"
           " value is: %s Dec "
           "is: %d sign is: %d\n",
           string, Dec, sign);
    return 0;


The converted string value is: 98760000000 Dec is: 1 sign is: 1

Last Updated : 26 Nov, 2021
Like Article
Save Article
Similar Reads