atof 函数参考

double atof ( const char * str );

Convert string to double

Parses the C string str interpreting its content as a floating point number and returns its value as a double .

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals, and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.

A valid floating point number for atof is formed by a succession of:

  • An optional plus or minus sign
  • A sequence of digits, optionally containing a decimal-point character
  • An optional exponent part, which itself consists on an 'e' or 'E' character followed by an optional sign and a sequence of digits.

If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just defined, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed.

Parameters

str
C string beginning with the representation of a floating-point number.

 

Return Value

On success, the function returns the converted floating point number as a double value.
If no valid conversion could be performed, or if the correct value would cause underflow, a zero value (0.0 ) is returned.
If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* atof example: sine calculator */

#include <stdio.h>

#include <stdlib.h>

#include <math.h>


int
 main ()
{
  double
 n,m;
  double
 pi=3.1415926535;
  char
 szInput [256];
  printf ( "Enter degrees: "
 );
  gets ( szInput );
  n = atof ( szInput );
  m = sin (n*pi/180);
  printf ( "The sine of %f degrees is %f/n"
 , n, m );
  return
 0;
}



Output:


Enter degrees: 45
The sine of 45.000000 degrees is 0.707101

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值