Link to home
Start Free TrialLog in
Avatar of mikedinh1234
mikedinh1234

asked on

SImple explain of the atof function ???

Just a simple questions about atof function?
Why and when would you need a function that convert char to double value???
What is the purpose??
Can you explain to me the following 2 programs
give me some situations when such a function need to be use.
thanks
 
 str = "  3336402735171707160320 ";  
 
Is "333640273......"  being treated as a string and not a value?
 
* atof example: sine calculator */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
double n,m;
double pi=3.1415926535;
char sizeInput [256];
printf ( "Enter degrees: " );
gets_s ( sizeInput );       // WHY does this read in a degree value but declare as char to read up to 256 ???
n = atof ( sizeInput );
m = sin (n*pi/180);
printf ( "The sine of %f degrees is %f\n" , n, m );
return 0;
}
 
 
-----------------------------------------------------------------------------------------
 
 
// crt_atof.c
//
// This program shows how numbers stored as
// strings can be converted to numeric
// values using the atof function.

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

int main( void )
{
    char    *str = NULL;
    double  value = 0;

    // An example of the atof function
    // using leading and training spaces.
    str = "  3336402735171707160320 ";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // Another example of the atof function
    // using the 'd' exponential formatting keyword.
    str = "3.1412764583d210";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );

    // An example of the atof function
    // using the 'e' exponential formatting keyword.
    str = "  -2309.12E-15";
    value = atof( str );
    printf( "Function: atof( \"%s\" ) = %e\n", str, value );
Avatar of ozo
ozo
Flag of United States of America image

gets_s ( sizeInput );       // WHY does this read in a degree value but declare as char to read up to 256 ???
It reads in a degree value because that's what you told the user to type in, and because you are multiplying by pi/180 before passing to sin which takes a radian value
char sizeInput [256]; is allocated so that we don't write past the end of the buffer  if the user types 250 characters
str = "  3336402735171707160320 ";  
 
Is "333640273......"  being treated as a string and not a value?


The value of str is the address of the first character in "  3336402735171707160320 "
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NovaDenizen
NovaDenizen

All your input is in the form of text until you convert it into something else.

gets(sizeInput) reads a series of characters from standard input until it sees a newline character, then it stores that sequence of characters into the sizeInput buffer.  After the last character, it puts a special zero character ('\0') to signal the end of the string.

At this point, you don't have a number loaded in the sizeInput buffer.  You just have a sequence of characters like '2', '.', '7', '1', '8', '2', '8', then a '\0' at the end.

atof() looks at a character string, calculates the floating point number that the character string represents, then returns that number, in this case 2.71828.
Avatar of mikedinh1234

ASKER

ozo  -  your statement make sense but it didn't exactly leaning toward a clear explain I was looking for
           what you said is all correct

itsmeandnobodyelse: yes you point out the idea where it would be use, so when it read in a file, then the value in the file is treated as string, i see. thanks

but can you guys explain two lines:

printf ( "Enter degrees: " );
gets_s ( sizeInput );      

// so when I enter 45, the program does not view the value as an int, but as a string???
// so when I enter 45, the program does not view the value as an int, but as a string???
yes
http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html

I would say a good place you would use something like atof() is if you were parsing a file and you needed to use parts of that file as a number and not as a string.