C语言函数库——stdlib.h

1、abs

功能:求绝对值


2、atof

功 能: 把字符串转换成浮点数

例子:

#include <stdlib.h>
#include <stdio.h>
int main()
{
    float f;
    char *str = "12345.67";
    f = atof(str);
    printf("string = %s float = %f\n", str, f);
    return 0;
}


3、atoi

功能: 把字符串转换成整型数。

例子:

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

    int n;

    char *str = "12345.67";

    n = atoi(str);

    printf("string = %s integer = %d\n", str, n);

    return 0;

}

执行结果:

string = 12345.67 integer = 12345


4、atol

功能:把字符串转换成长整型数


5、bsearch

功能:二分法搜索

参数:第一个:要查找的关键字。第二个:要查找的数组。第三个:指定数组中元素的数目。第四个:每个元素的长度(以字符为单位)。第五个:指向比较函数的指针

例子:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort, bsearch, NULL */

int compareints (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int values[] = { 50, 20, 60, 40, 10, 30 };

int main ()
{
  int * pItem;
  int key = 40;
  qsort (values, 6, sizeof (int), compareints);                //必须有序才可以进行二分查找
  pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
  if (pItem!=NULL)
     printf ("%d is in the array.\n",*pItem);
  else
     printf ("%d is not in the array.\n",key);
  return 0;
}


例2:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort, bsearch, NULL */
int compareints (const void * a, const void * b)
{
  return ( *(char*)a - *(char*)b );
}
char test[] = "chenchong";
int main ()
{
  char* pItem;
  char key = 'e';
  qsort (test, 6, sizeof (char), compareints);
  printf("%s\n",test);
  pItem = (char*) bsearch (&key, test, 6, sizeof (char), compareints);
  printf("%s\n",test);
  if (pItem!=NULL)
    printf ("%d is in the array.\n",*pItem);
  else
    printf ("%d is not in the array.\n",key);
  return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值