C库-atoi(), atol(), atoll(), atof(), ceil(), ceilf(), ceill()

一、atoi,atol,atoll

1.声明

atoi, atol, atoll - convert a string to an integer
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);

2.说明

atoi, atol, atoll分别把nptr指向的字符串转换成int,long,long long型。但是atoi不会检错,示例中传入字母时,结果返回0

DESCRIPTION
       The atoi() function converts the initial portion of the string pointed to by nptr to
       int.  The behavior is the same as

           strtol(nptr, NULL, 10);

       except that atoi() does not detect errors.

       The atol() and atoll() functions behave the same as atoi(), except that they convert
       the initial portion of the string to their return type of long or long long.

3.示例

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

 int main()
 {
    int val1;
    long val2;
    long long val3;
    char str[] = "123456";
    char str1[] = "ABC";
    
    printf("atoi使用示例:\n");
    val1 = atoi(str);
    printf("字符串: %s\t整型: %d\n", str, val1);

    val1 = atoi(str1);
    printf("字符串: %s\t整型: %d\n", str1, val1);


    printf("atol使用示例:\n");
    val2 = atol(str);
    printf("字符串: %s\t整型: %ld\n", str, val2);

    val2 = atol(str1);
    printf("字符串: %s\t整型: %ld\n", str1, val2);


    printf("atoll使用示例:\n");
    val3 = atoll(str);
    printf("字符串: %s\t整型: %lld\n", str, val3);

    val3 = atoll(str1);
    printf("字符串: %s\t整型: %lld\n", str1, val3);


    return 0;
 }
}

从结果上看也看不出区别,毕竟他们的区别只是返回int,long,long long型数据

atoi使用示例:
字符串: 123456  整型: 123456
字符串: ABC     整型: 0

atol使用示例:
字符串: 123456  整型: 123456
字符串: ABC     整型: 0

atoll使用示例:
字符串: 123456  整型: 123456
字符串: ABC     整型: 0

二、atof

1.声明

double atof(const char *nptr);

2.说明

atof把字符串转换成double,其余和atoi一样

3.示例

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

int main()
{
   double val;
   char str[] = "123.3";
   char str1[] = "ABC";
	
   printf("atof使用示例:\n");
   val = atof(str);
   printf("字符串: %s\t整型: %f\n", str, val);
 
   val = atof(str1);
   printf("字符串: %s\t整型: %f\n", str1, val);

   return 0;
}
atof使用示例:
字符串: 123.3   整型: 123.300000
字符串: ABC     整型: 0.000000

顺手记录一下刚看到的ceilceilfceill

#include <math.h>

       double ceil(double x);
       float ceilf(float x);
       long double ceill(long double x);

返回一个大于x的最小的整数(其实吧,定义的函数返回值就不是整数,不知道为啥描述的是整数,可能integral翻译问题吧),如ceil(0.5) = 1.0,ceil(-0.5) = 0.0

ceil,  ceilf,  ceill - ceiling function: smallest integral value not less than argument

#include <stdio.h>
#include <math.h>

int main()
{

   double x = 2.9;
   float y = 0.5;
   long double z = -3.9;

   printf("%f\n",ceil(x));
   //intf("%f", ceilf(y));
   printf("%Lf\n",ceill(z));


   return 0;
}
3.000000
-3.000000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值