C语言标准库——<stdlib.h>

发布于:2024-07-05 ⋅ 阅读:(20) ⋅ 点赞:(0)

目录

一、库变量

二、库宏

三、库函数

3.1 free()

3.2 malloc()

3.3 calloc()

3.4 atoi()

3.5 atol()

3.6  rand()

3.7  exit()

3.8  getenv()

stdlib .h 头文件定义了四个变量类型、一些宏和各种通用工具函数。

一、库变量

变量

描述

size_t

这是无符号整数类型,它是 sizeof 关键字的结果

wchar_t

这是一个宽字符常量大小的整数类型。

div_t

这是 div 函数返回的结构。

ldiv_t

这是 ldiv 函数返回的结构。

二、库宏

描述

NULL

是一个空指针常量的值。

EXIT_FAILURE

是 exit 函数失败时要返回的值。

EXIT_SUCCESS

是 exit 函数成功时要返回的值。

RAND_MAX

是 rand 函数返回的最大值。

MB_CUR_MAX

表示在多字节字符集中的最大字符数,不能大于 MB_LEN_MAX。

三、库函数

3.1 free()

描述是C语言中释放内存空间的函数,通常与申请内存空间的函数malloc()结合使用,可以释放由 malloc()、calloc()、realloc() 等函数申请的内存空间。

声明void free(void *ptr)

参数ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。

#include <stdlib.h>
 
int main()
{   
    char *str;    /* 最初的内存分配 */   
    str = (char *) malloc(15); 
	  
    strcpy(str, "hello world");   
    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);
}

3.2 malloc()

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

声明void *malloc(size_t size)

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

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

3.3 calloc()

描述在内存的动态存储区中分配num个长度为size的连续空间,函数返回一个指向分配起始地址的指针;如果分配不成功,返回NULL。

声明void *calloc(size_t nitems, size_t size)

参数nitems -- 要被分配的元素个数 size -- 元素的大小。

#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.4 atoi()

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

声明int atoi(const char *str)

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

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

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

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

   return(0);
}

3.5 atol()

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

声明long int atol(const char *str)

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

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

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

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

   return(0);
}

3.6  rand()

描述返回一个范围在 0 到 RAND_MAX 之间的伪随机数。

RAND_MAX 是一个常量,它的默认值在不同的实现中会有所不同,但是值至少是 32767。

声明int rand(void)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main()
{
   int i, n;
   time_t t;
   n = 5;
   /* 初始化随机数发生器 */
   srand((unsigned) time(&t));
 
   /* 输出 0 到 99 之间的 5 个随机数 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 100);
   }
   
  return(0);
}

3.7  exit()

描述立即终止调用进程。任何属于该进程的打开的文件描述符都会被关闭,该进程的子进程由进程 1 继承,初始化,且会向父进程发送一个 SIGCHLD 信号

RAND_MAX 是一个常量,它的默认值在不同的实现中会有所不同,但是值至少是 32767。

声明void exit(int status)

参数status -- 返回给父进程的状态值。

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

int main ()
{
	printf("程序的开头....\n");
	
	printf("退出程序....\n");
	exit(0);

	printf("程序的结尾....\n");

	return(0);
}

3.8  getenv()

描述从环境中取字符串,获取环境变量的值getenv函数的返回值存储在一个全局二维数组里,当你再次使用getenv函数时不用担心会覆盖上次的调用结果。

声明char *getenv(const char *name)

参数name -- 包含被请求变量名称的 C 字符串。

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

int main(void) 
{
  char* s=NULL;
  s=getenv("COMSPEC"); /* 获取 comspec 环境参数 */
  printf("Command processor: %s\n",s); 
  return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到