C Programming - Library Functions - Discussion

7. 

What will function gcvt() do?

[A]. Convert vector to integer value
[B]. Convert floating-point number to a string
[C]. Convert 2D array in to 1D array.
[D]. Covert multi Dimensional array to 1D array

Answer: Option B

Explanation:

The gcvt() function converts a floating-point number to a string. It converts given value to a null-terminated string.


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

int main(void)
{
	char str[25];
	double num;
	int sig = 5; /* significant digits */

	/* a regular number */
	num = 9.876;
	gcvt(num, sig, str);
	printf("string = %s\n", str);

	/* a negative number */
	num = -123.4567;
	gcvt(num, sig, str);
	printf("string = %s\n", str);

	/* scientific notation */
	num = 0.678e5;
	gcvt(num, sig, str);
	printf("string = %s\n", str);

	return(0);
}

Output:
string = 9.876
string = -123.46
string = 67800


Tom said: (May 12, 2018)  
Why num in scientific notation (num = 0.678e5) is printed as string = 67800?

Laasya said: (May 27, 2018)  
0.678e5 is nothing but 0.678*10^5.

So the output string is 67800.

Adit said: (Feb 15, 2019)  
Does gcvt() same as the ftoa()?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.