<stdlib.h>中定义的函数(上)

目录

一、atof(str)

二、atoi(str)

三、atol(str)

四、strtod(str,endptr)

五、strtol(onstr,endptr,base)

六、strtoul(str,endptr,base)

七、calloc(nitems,size):

八、free(ptr)

九、malloc(size)

十、realloc(ptr,size)


一、atof(str)

1、描述:

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

2、参数:

str -- 要转换为浮点数的字符串。

3、返回值:

函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。

4、实例:

代码:

#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);
 
   return(0);
}

运行结果:

字符串值 = 98993489, 浮点值 = 98993488.000000
字符串值 = runoob, 浮点值 = 0.000000

二、atoi(str)

1、描述:

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

2、参数:

str -- 要转换为整数的字符串。

3、返回值:

该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。

4、实例:

代码:

#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);

   return(0);
}

运行结果:

字符串值 = 98993489, 整型值 = 98993489
字符串值 = runoob.com, 整型值 = 0

三、atol(str)

1、描述:

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

2、参数:

str -- 要转换为长整数的字符串。

3、返回值:

该函数返回转换后的长整数,如果没有执行有效的转换,则返回零。

4、实例:

代码:

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

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

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

   return(0);
}

运行结果:

字符串值 = 98993489, 长整型值 = 98993489
字符串值 = runoob.com, 长整型值 = 0

四、strtod(str,endptr)

1、描述:

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

2、参数:

str -- 要转换为双精度浮点数的字符串。

endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。

3、返回值:

该函数返回转换后的双精度浮点数,如果没有执行有效的转换,则返回零(0.0)。

4、实例:

代码:

#include <stdio.h>
#include <stdlib.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);
}

运行结果:

数字(double)是 20.303000
字符串部分是 | This is test|

五、strtol(onstr,endptr,base)

1、描述:

把参数 str 所指向的字符串根据给定的 base 转换为一个长整数(类型为 long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

2、参数:

str -- 要转换为长整数的字符串。

endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。

base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

3、返回值:

该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。

4、实例:

代码:

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

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

   ret = strtol(str, &ptr, 10);
   printf("数字(无符号长整数)是 %ld\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

运行结果:

数字(无符号长整数)是 2030300
字符串部分是 | This is test|

六、strtoul(str,endptr,base)

1、描述:

 把参数 str 所指向的字符串根据给定的 base 转换为一个无符号长整数(类型为 unsigned long int 型),base 必须介于 2 和 36(包含)之间,或者是特殊值 0。

2、参数:

str -- 要转换为无符号长整数的字符串。

endptr -- 对类型为 char* 的对象的引用,其值由函数设置为 str 中数值后的下一个字符。

base -- 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。

3、返回值:

该函数返回转换后的长整数,如果没有执行有效的转换,则返回一个零值。

4、实例:

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

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

   ret = strtoul(str, &ptr, 10);
   printf("数字(无符号长整数)是 %lu\n", ret);
   printf("字符串部分是 |%s|", ptr);

   return(0);
}

 运行结果:

数字(无符号长整数)是 2030300
字符串部分是 | This is test|

七、calloc(nitems,size):

1、描述:

分配所需的内存空间,并返回一个指向它的指针。malloc 和 calloc 之间的不同点是,malloc 不会设置内存为零,而 calloc 会设置分配的内存为零。

2、参数:

nitems -- 要被分配的元素个数。

size -- 元素的大小。

3、返回值:

该函数返回一个指针,指向已分配的内存。如果请求失败,则返回 NULL。

4、实例:

代码:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
   int i, n;
   int *a;
 
   printf("要输入的元素个数:");
   scanf("%d",&n);
 
   a = (int*)calloc(n, sizeof(int));
   printf("输入 %d 个数字:\n",n);
   for( i=0 ; i < n ; i++ ) 
   {
      scanf("%d",&a[i]);
   }
 
   printf("输入的数字为:");
   for( i=0 ; i < n ; i++ ) {
      printf("%d ",a[i]);
   }
   free (a);  // 释放内存
   return(0);
}

运行结果:

要输入的元素个数:3
输入 3 个数字:
1
2
3
输入的数字为:1 2 3

八、free(ptr)

1、描述:

释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。

2、参数:

ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果传递的参数是一个空指针,则不会执行任何动作。

3、返回值:

该函数不返回任何值。

4、实例:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 释放已分配的内存 */
   free(str);
 
   return(0);
}

运行结果:

String = runoob,  Address = 006E0DD8
String = runoob.com,  Address = 006E0DD8

九、malloc(size)

1、描述:

分配所需的内存空间,并返回一个指向它的指针。

2、参数:

size -- 内存块的大小,以字节为单位。

3、返回值:

该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。

4、实例:

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}

运行结果:

String = runoob,  Address = 7540184
String = runoob.com,  Address = 7540184

十、realloc(ptr,size)

1、描述:

尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。

2、参数:

ptr -- 指针指向一个要重新分配内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果为空指针,则会分配一个新的内存块,且函数返回一个指向它的指针。

size -- 内存块的新的大小,以字节为单位。如果大小为 0,且 ptr 指向一个已存在的内存块,则 ptr 所指向的内存块会被释放,并返回一个空指针。

3、返回值:

该函数返回一个指针 ,指向重新分配大小的内存。如果请求失败,则返回 NULL。

4、实例:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %p\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %p\n", str, str);
 
   free(str);
   
   return(0);
}

运行结果:

String = runoob,  Address = 00B70DD8
String = runoob.com,  Address = 00B70DD8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值