C语言stdlib.h常用方法

stdlib.h常用方法
方法描述
double atof(const char *str)把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
int atoi(const char *str)把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。
long int atol(const char *str)把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。
double strtod(const char *str, char **endptr)把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置。
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void))对数组进行排序。
long int labs(long int x)返回 x 的绝对值。
int abs(int x)返回 x 的绝对值。
int rand(void)返回一个范围在 0 到 RAND_MAX 之间的伪随机数。
void srand(unsigned int seed)播种由函数 rand 使用的随机数发生器。

 


 
 
  

1. atof

double atof(const char *str)

作用: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。

  • str – 要转换为浮点数的字符串。
  • 函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{	
	float val;
   	char str[20];
   
   	strcpy(str, "98993489");
   	val = atof(str);
   	printf("字符串值 = %s, 浮点值 = %f\n", str, val);
 
   	strcpy(str, "runoob");
   	val = atof(str);
   	printf("字符串值 = %s, 浮点值 = %f\n", str, val);
   	
   	strcpy(str, "6666.6666");
   	val = atof(str);
   	printf("字符串值 = %s, 浮点值 = %f\n", str, val);
	return 0;
} 

运行截图
 
 

2. atoi

int atoi(const char *str)

作用: 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

  • str – 要转换为整数的字符串。
  • 该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int val;
	char str[20];
   
	strcpy(str, "98993489");
	val = atoi(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);

	strcpy(str, "runoob.com");
	val = atoi(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);

	strcpy(str, "6666.666");
	val = atoi(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);
	return 0;
}

运行截图
 
 

3. atol

long int atol(const char *str)

作用: 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。

  • str – 要转换为长整数的字符串。
  • 该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int val;
	char str[20];
   
	strcpy(str, "98993489");
	val = atol(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);

	strcpy(str, "runoob.com");
	val = atol(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);

	strcpy(str, "6666.666");
	val = atol(str);
	printf("字符串值 = %s, 整型值 = %d\n", str, val);
	return 0;
}

运行截图
 
 

4. strtod

double strtod(const char *str, char **endptr)

作用: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针会存储在 endptr 引用的位置。

  • str – 要转换为双精度浮点数的字符串。
  • endptr – 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
  • 该函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	char str[30] = "20.30300 This is test";
   	char *ptr;
   	double ret;

   	ret = strtod(str, &ptr);
   	printf("数字(double)是 %lf\n", ret);
   	printf("字符串部分是 |%s|", ptr);
	return 0;
}

运行截图
 
 

5. qsort

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

作用: 对数组进行排序。

  • base – 指向要排序的数组的第一个元素的指针。
  • nitems – 由 base 指向的数组中元素的个数。
  • size – 数组中每个元素的大小,以字节为单位。
  • compar – 用来比较两个元素的函数。
  • 该函数不返回任何值。
#include <stdio.h>
#include <stdlib.h>

int values[] = { 88, 56, 100, 2, 25 };

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

int main()
{
   int n;

   printf("排序之前的列表:\n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\n排序之后的列表:\n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }
  
  return 0;
}

运行截图
 
 

6. abs

int abs(int x)

作用: 返回 x 的绝对值。

  • x – 完整的值。
  • 该函数返回 x 的绝对值。
#include <stdio.h>
#include <stdlib.h>

int main ()
{
   int a, b;

   a = abs(5);
   printf("a 的值 = %d\n", a);

   b = abs(-10);
   printf("b 的值 = %d\n", b);
   
   return 0;
}

运行截图
 
 

7. labs

long int labs(long int x)

作用: 返回 x 的绝对值。

  • x – 完整的值。
  • 该函数返回 x 的绝对值。
#include <stdio.h>
#include <stdlib.h>

int main ()
{
   long int a,b;

   a = labs(65987L);
   printf("a 的值 = %ld\n", a);

   b = labs(-1005090L);
   printf("b 的值 = %ld\n", b);
   
   return 0;
}

运行截图
 
 

8. rand

int rand(void)

作用: 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。

  • 该函数返回一个范围在 0 到 RAND_MAX 之间的整数值。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   /* 初始化随机数发生器 */
   srand((unsigned) time(&t));
 
   /* 输出 0 到 49 之间的 5 个随机数 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }
   
  return 0;
}

运行截图
 
 

9. srand

void srand(unsigned int seed)

作用: 播种由函数 rand 使用的随机数发生器。

  • seed – 这是一个整型值,用于伪随机数生成算法播种。
  • 该函数不返回任何值。

使用见上例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值