stdlib头文件中包含的函数
stdlib.h文件的函数

在有些程序中我们需要产生一些随机数,C语言中的随机数问题可以用<stdlib.h>中的rand()函数解决程序1#include<stdio.h>#include<stdlib.h>int main(void){int i=1;printf("RAND_MAX = %d\n",RAND_MAX);for(;i<=20;i++){printf("%10d",rand());}printf("\n");getchar();return 0;}在这个程序中rand()函数用来生成一个随机数,rand()函数的原形位于<stdlib.h>中,函数的返回值是一个0~RAND_MAX的无符号整型,rand_max是<stdlib.h>中定义的一个符号常量(RAND_MAX is the maximum value that may be returned by rand.The minimum is zero.)标准C规定至少要32767,故而0≤rand()≤RAND_MAX .在这个程序中,可以看到产生的随机数范围在0≤rand()≤RAND_MAX,而有时候我们需要将随机数的范围缩小,例如用来模拟骰子的点数,范围就在1~6之间,此时我们可以参照以下程序程序2#include<stdio.h>#include<stdlib.h>int main(void){int i=1;for(;i<=20;i++){printf("%10d",1+rand()%6);if(0==i%5)printf("\n");}printf("\n");getchar();return 0;}在这个程序中我们采用取余的方式来进行比例缩放,rand()%6用来产生0~5之间的随机数,然后(1+1+rand()%6)就可以产生1~6的随机数.对以上两个程序多次运行可以发现:程序运行的结果总是相同的!程序的结果具有可再现性,这种可再现性也表明了这两个程序运行的正确性,因为rand()函数产生的”随机数”是一个伪随机数,需要对他进行随机化(Randomizing)Srand()函数配合rand()函数使用可以实现随机数的生成,srand()需要外界提供一个无符号整数用来做产生随机数的种子,”种瓜得瓜,种豆得豆”种子不一样产生的随机数也就不一样,种子一样,产生的随机数也就一样.srand()函数也是<stdlib.h>中的函数程序3#include<stdio.h>#include<stdlib.h>int main(void){int i=1;unsigned seed;printf("Please input the seed "); scanf("%u",&seed);srand(seed);for(;i<=20;i++){printf("%10d",1+rand()%6);if(0==i%5)printf("\n");system("PAUSE");return 0;}可以看到输入的种子一样,产生的随机数列就一样\我们可以通过利用系统时间作为seed从而来产生随机数程序4#include<stdio.h>#include<stdlib.h>#include<time.h>int main(void){int i=1;srand(time(NULL));for(;i<=20;i++)printf("%10d",1+rand()%6);if(0==i%5)printf("\n");}system("PAUSE");return 0;}函数time()将返回以秒为单位的从1970年1月1日午夜开始到现在所经历的时间,返回的时钟值以字符串的形式显示出来,但是实参NULL将屏蔽掉这个功能,又说:time()通常的参数是一个time_t类型对象的地址,那种情况下,时间也存在那个地址中,然而可以提供空指针来作为参数,此时时间值仅通过返回值机制提供另外如果种子是个固定的或者种子的范围比较有规律,生成的随机数也同样有规律可言的把种子取成系统当前的时间或者日期或者随时都不同的数字,比如GUID srand((unsigned)time(NULL));程序执行的CPU时间较快,调用time函数获取的时间精度却较低(55ms),这样循环体内每次产生随机数用到的种子数都是一样的,因此产生的随机数也是一样的。
includestdlib.h是什么意思啊

include<stdlib.h>是什么意思啊2008-11-18 18:17提问者:大怪鸟OG |浏览次数:15319次include<conio>也顺便问一下我来帮他解答满意回答2008-11-18 20:25stdlib.hstdlib头文件里包含了C语言的一些函数该文件包含了的C语言标准库函数的定义stdlib.h里面定义了五种类型、一些宏和通用工具函数。
类型例如size_t、wchar_t、div_t、ldiv_t和lldiv_t;宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等;常用的函数如malloc()、calloc()、realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()等等。
具体的内容你自己可以打开编译器的include目录里面的stdlib.h头文件看看。
conio.hconio.h不是C标准库中的头文件。
conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,主要是一些用户通过按键盘产生的对应操作,比如getch()函数等等。
包含的函数cgets(char *);cprintf(const char *, ...);cputs(const char *);cscanf(const char *, ...);inp(unsigned short);inpw(unsigned short);getch(void);getche(void);kbhit(void);outp(unsigned short, int);outpw(unsigned short, unsigned short);putch(int);ungetch(int);void _Cdeclclreol (void);void _Cdeclclrscr (void);void _Cdecldelline (void);int _Cdeclgettext (int left, int top, int right, int bottom,void *destin);void _Cdeclgettextinfo (structtext_info *r);void _Cdeclgotoxy (int x, int y);void _Cdeclhighvideo (void);void _Cdeclinsline (void);void _Cdecllowvideo (void);int _Cdeclmovetext (int left, int top, int right, int bottom,intdestleft, intdesttop);void _Cdeclnormvideo (void);int _Cdeclputtext (int left, int top, int right, int bottom,void *source);void _Cdecltextattr (intnewattr);void _Cdecltextbackground (intnewcolor);void _Cdecltextcolor (intnewcolor);void _Cdecltextmode (intnewmode);int _Cdeclwherex (void);int _Cdeclwherey (void);void _Cdecl window (int left, int top, int right, int bottom);har *_Cdeclcgets (char *str);int _Cdeclcprintf (const char *format, ...);int _Cdeclcputs (const char *str);int _Cdeclcscanf (const char *format, ...);int _Cdeclgetch (void);int _Cdeclgetche (void);char *_Cdeclgetpass (const char *prompt);int _Cdeclkbhit (void);int _Cdeclputch (int c);int _Cdeclungetch (intch);388|评论(15)。
c语言中常用的函数和头文件

头文件ctype.h函数列表<>函数类别函数用途详细说明字符测试是否字母和数字isalnum是否字母isalpha是否控制字符iscntrl是否数字isdigit是否可显示字符(除空格外)isgraph是否可显示字符(包括空格)isprint是否既不是空格,又不是字母和数字的可显示字符ispunct是否空格isspace是否大写字母isupper是否16进制数字(0-9,A-F)字符isxdigit字符大小写转换函数转换为大写字母toupper转换为小写字母tolower地区化本类别的函数用于处理不同国家的语言差异。
头文件local.h函数列表函数类别函数用途详细说明地区控制地区设置setlocale数字格式约定查询国家的货币、日期、时间等的格式转换localeconv数学函数本分类给出了各种数学计算函数,必须提醒的是ANSI C标准中的数据格式并不符合IEEE754标准,一些C语言编译器却遵循IEEE754(例如frinklin C51)头文件math.h函数列表函数类别函数用途详细说明错误条件处理定义域错误(函数的输入参数值不在规定的范围内)值域错误(函数的返回值不在规定的范围内)三角函数反余弦acos反正弦asin反正切atan反正切2 atan2余弦cos正弦sin正切tan双曲函数双曲余弦cosh双曲正弦sinh双曲正切tanh指数和对数指数函数exp指数分解函数frexp乘积指数函数fdexp自然对数log以10为底的对数log10浮点数分解函数modf幂函数幂函数pow平方根函数sqrt整数截断,绝对值和求余数函数求下限接近整数ceil绝对值fabs求上限接近整数floor求余数fmod本分类函数用于实现在不同底函数之间直接跳转代码。
头文件setjmp.h io.h函数列表函数类别函数用途详细说明保存调用环境setjmp恢复调用环境longjmp信号处理该分类函数用于处理那些在程序执行过程中发生例外的情况。
c语言malloc函数的使用

c语言malloc函数的使用在C语言中,malloc函数用于动态分配内存。
它可以在运行时根据需要分配内存空间。
这对于在程序执行期间根据用户需求创建新的数据结构或缓冲区非常有用。
malloc函数的使用包括以下步骤:1.包含头文件:首先,需要包含头文件<stdlib.h>,因为malloc函数是在这个头文件中定义的。
2.调用malloc函数:使用malloc函数来分配内存。
它的语法是void*malloc(size_t size)。
其中,size是要分配的内存的大小(以字节为单位)。
3.检查分配是否成功:malloc函数返回一个指向分配的内存的指针。
如果分配成功,它会返回一个非空指针。
否则,它会返回一个空指针(NULL)。
因此,在分配内存后,应该检查返回的指针是否为NULL。
4.使用分配的内存:一旦成功分配了内存,就可以使用这个内存地址来存储数据或创建对象。
5.释放内存:当不再需要分配的内存时,应该使用free函数来释放它。
这是为了防止内存泄漏。
下面是一个简单的示例,演示了如何使用malloc函数来动态分配内存:#include <stdio.h>#include <stdlib.h>int main() {// 定义要分配的内存大小int size = 5;// 使用malloc分配内存int* ptr = (int*)malloc(size * sizeof(int));// 检查分配是否成功if (ptr == NULL) {printf("Memory allocation failed!\n");return 1;}// 使用分配的内存存储数据for (int i = 0; i < size; i++) {ptr[i] = i + 1;}// 打印分配的内存中的值for (int i = 0; i < size; i++) {printf("%d ", ptr[i]);}// 释放内存free(ptr);printf("\nMemory freed.\n");return 0;}在这个示例中,我们首先定义了要分配的内存大小(5个整数)。
malloc的用法和意义

malloc的用法和意义1. 什么是malloc?malloc是C语言中的一个库函数,用于动态分配内存空间。
它的全称是memory allocation(内存分配)。
通过调用malloc函数,我们可以在程序运行时根据需要申请指定大小的内存块,并返回一个指向该内存块起始地址的指针。
2. malloc的用法malloc函数的用法如下所示:#include <stdlib.h>void* malloc(size_t size);其中,size参数表示需要分配的内存空间大小,单位是字节。
malloc函数返回一个void*类型的指针,指向分配的内存空间的起始地址。
具体的使用步骤如下:1.包含头文件<stdlib.h>,该头文件中包含了malloc函数的声明。
2.调用malloc函数,并传入需要分配的内存空间大小作为参数。
3.检查malloc函数的返回值,如果返回值为NULL,则表示分配失败,可能是内存不足。
如果返回值不为NULL,则表示分配成功。
4.使用返回的指针访问分配的内存空间。
5.在不需要使用分配的内存空间时,使用free函数释放内存。
下面是一个使用malloc函数动态分配内存的示例代码:#include <stdio.h>#include <stdlib.h>int main() {int* ptr;int n, i;printf("Enter the number of elements: ");scanf("%d", &n);// 动态分配内存空间ptr = (int*)malloc(n * sizeof(int));// 检查分配是否成功if (ptr == NULL) {printf("Memory allocation failed!\n");exit(1);}// 读取输入数据printf("Enter elements: ");for (i = 0; i < n; i++) {scanf("%d", &ptr[i]);}// 输出数据printf("Elements: ");for (i = 0; i < n; i++) {printf("%d ", ptr[i]);}// 释放内存free(ptr);return 0;}3. malloc的意义malloc函数在C语言中具有重要的意义,其主要体现在以下几个方面:3.1 动态内存分配malloc函数可以根据程序的实际需要,在运行时动态分配内存空间。
stdlib.h 标准库头文件

stdlib.h 标准库头文件一、简介size_t、wchar_t、div_t、ldiv_t和lldiv_t;宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等;常用的函数如malloc()、calloc()、realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()等等。
二、Stdlib.H具体内容/****stdlib.h - declarations/definitions for commonly used library functions** Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.**Purpose:* This include file contains the function declarations for commonly* used library functions which either don't fit somewhere else, or,* cannot be declared in the normal place for other reasons.* [ANSI]** [Public]*****/#if _MSC_VER > 1000#pragma once#endif /* _MSC_VER > 1000 */#ifndef _INC_STDLIB#define _INC_STDLIB#if !defined (_WIN32) && !defined (_MAC)#error ERROR: Only Mac or Win32 targets supported!#endif /* !defined (_WIN32) && !defined (_MAC) */#ifndef _CRTBLD/* This version of the header files is NOT for user programs.* It is intended for use when building the C runtimes ONLY.* The version intended for public use will not have this message.*/#error ERROR: Use of C runtime library internal header file.#endif /* _CRTBLD */#ifdef _MSC_VER/** Currently, all MS C compilers for Win32 platforms default to 8 byte * alignment.*/#pragma pack(push,8)#endif /* _MSC_VER */#ifdef __cplusplusextern "C" {#endif /* __cplusplus */#ifndef _INTERNAL_IFSTRIP_#include <cruntime.h>#endif /* _INTERNAL_IFSTRIP_ *//* Define _CRTIMP */#ifndef _CRTIMP#ifdef CRTDLL#define _CRTIMP __declspec(dllexport)#else /* CRTDLL */#ifdef _DLL#define _CRTIMP __declspec(dllimport)#else /* _DLL */#define _CRTIMP#endif /* _DLL */#endif /* CRTDLL */#endif /* _CRTIMP *//* Define __cdecl for non-Microsoft compilers */#if (!defined (_MSC_VER) && !defined (__cdecl))#define __cdecl#endif /* (!defined (_MSC_VER) && !defined (__cdecl)) *//* Define _CRTAPI1 (for compatibility with the NT SDK) */#ifndef _CRTAPI1#if _MSC_VER >= 800 && _M_IX86 >= 300#define _CRTAPI1 __cdecl#else /* _MSC_VER >= 800 && _M_IX86 >= 300 */#define _CRTAPI1#endif /* _MSC_VER >= 800 && _M_IX86 >= 300 */#endif /* _CRTAPI1 */#ifndef _SIZE_T_DEFINEDtypedef unsigned int size_t;#define _SIZE_T_DEFINED#endif /* _SIZE_T_DEFINED */#ifndef _WCHAR_T_DEFINEDtypedef unsigned short wchar_t;#define _WCHAR_T_DEFINED#endif /* _WCHAR_T_DEFINED *//* Define NULL pointer value */#ifndef NULL#ifdef __cplusplus#define NULL 0#else /* __cplusplus */#define NULL ((void *)0)#endif /* __cplusplus */#endif /* NULL *//* Definition of the argument values for the exit() function */#define EXIT_SUCCESS 0#define EXIT_FAILURE 1#ifndef _ONEXIT_T_DEFINEDtypedef int (__cdecl * _onexit_t)(void);#if !__STDC__/* Non-ANSI name for compatibility */#define onexit_t _onexit_t#endif /* !__STDC__ */#define _ONEXIT_T_DEFINED#endif /* _ONEXIT_T_DEFINED *//* Data structure definitions for div and ldiv runtimes. */#ifndef _DIV_T_DEFINEDtypedef struct _div_t {int quot;int rem;} div_t;typedef struct _ldiv_t {long quot;long rem;} ldiv_t;#define _DIV_T_DEFINED#endif /* _DIV_T_DEFINED *//* Maximum value that can be returned by the rand function. */#define RAND_MAX 0x7fff/** Maximum number of bytes in multi-byte character in the current locale * (also defined in ctype.h).*/#ifndef MB_CUR_MAX#ifndef _INTERNAL_IFSTRIP_#if defined (_DLL) && defined (_M_IX86)/* Retained for compatibility with VC++ 5.0 and earlier versions */_CRTIMP int * __cdecl __p___mb_cur_max(void);#endif /* defined (_DLL) && defined (_M_IX86) */#endif /* _INTERNAL_IFSTRIP_ */#define MB_CUR_MAX __mb_cur_max_CRTIMP extern int __mb_cur_max;#endif /* MB_CUR_MAX *//* Minimum and maximum macros */#define __max(a,b) (((a) > (b)) ? (a) : (b))#define __min(a,b) (((a) < (b)) ? (a) : (b))/** Sizes for buffers used by the _makepath() and _splitpath() functions.* note that the sizes include space for 0-terminator*/#ifndef _MAC#define _MAX_PATH 260 /* max. length of full pathname */#define _MAX_DRIVE 3 /* max. length of drive component */#define _MAX_DIR 256 /* max. length of path component */#define _MAX_FNAME 256 /* max. length of file name component */#define _MAX_EXT 256 /* max. length of extension component */#else /* _MAC */#define _MAX_PATH 256 /* max. length of full pathname */#define _MAX_DIR 32 /* max. length of path component */#define _MAX_FNAME 64 /* max. length of file name component */#endif /* _MAC *//** Argument values for _set_error_mode().*/#define _OUT_TO_DEFAULT 0#define _OUT_TO_STDERR 1#define _OUT_TO_MSGBOX 2#define _REPORT_ERRMODE 3/* External variable declarations */#if (defined (_MT) || defined (_DLL)) && !defined (_MAC)_CRTIMP int * __cdecl _errno(void);_CRTIMP unsigned long * __cdecl __doserrno(void);#define errno (*_errno())#define _doserrno (*__doserrno())#else /* (defined (_MT) || defined (_DLL)) && !defined (_MAC) */_CRTIMP extern int errno; /* XENIX style error number */ _CRTIMP extern unsigned long _doserrno; /* OS system error value */#endif /* (defined (_MT) || defined (_DLL)) && !defined (_MAC) */#ifdef _MAC_CRTIMP extern int _macerrno; /* OS system error value */#endif /* _MAC */_CRTIMP extern char * _sys_errlist[]; /* perror error message table */_CRTIMP extern int _sys_nerr; /* # of entries in sys_errlist table */#if defined (_DLL) && defined (_M_IX86)#define __argc (*__p___argc()) /* count of cmd line args */#define __argv (*__p___argv()) /* pointer to table of cmd line args */#define __wargv (*__p___wargv()) /* pointer to table of wide cmd line args */ #define _environ (*__p__environ()) /* pointer to environment table */#ifndef _MAC#define _wenviron (*__p__wenviron()) /* pointer to wide environment table */#endif /* _MAC */#define _pgmptr (*__p__pgmptr()) /* points to the module (EXE) name */#ifndef _MAC#define _wpgmptr (*__p__wpgmptr()) /* points to the module (EXE) wide name */ #endif /* _MAC */_CRTIMP int * __cdecl __p___argc(void);_CRTIMP char *** __cdecl __p___argv(void);_CRTIMP wchar_t *** __cdecl __p___wargv(void);_CRTIMP char *** __cdecl __p__environ(void);_CRTIMP wchar_t *** __cdecl __p__wenviron(void);_CRTIMP char ** __cdecl __p__pgmptr(void);_CRTIMP wchar_t ** __cdecl __p__wpgmptr(void);#ifndef _INTERNAL_IFSTRIP_/* Retained for compatibility with VC++ 5.0 and earlier versions */_CRTIMP int * __cdecl __p__fmode(void);_CRTIMP int * __cdecl __p__fileinfo(void);_CRTIMP unsigned int * __cdecl __p__osver(void);_CRTIMP unsigned int * __cdecl __p__winver(void);_CRTIMP unsigned int * __cdecl __p__winmajor(void);_CRTIMP unsigned int * __cdecl __p__winminor(void);#endif /* _INTERNAL_IFSTRIP_ */#else /* defined (_DLL) && defined (_M_IX86) */_CRTIMP extern int __argc; /* count of cmd line args */_CRTIMP extern char ** __argv; /* pointer to table of cmd line args */#ifndef _MAC_CRTIMP extern wchar_t ** __wargv; /* pointer to table of wide cmd line args */#endif /* _MAC */_CRTIMP extern char ** _environ; /* pointer to environment table */#ifndef _MAC_CRTIMP extern wchar_t ** _wenviron; /* pointer to wide environment table */ #endif /* _MAC */_CRTIMP extern char * _pgmptr; /* points to the module (EXE) name */#ifndef _MAC_CRTIMP extern wchar_t * _wpgmptr; /* points to the module (EXE) wide name */ #endif /* _MAC */#endif /* defined (_DLL) && defined (_M_IX86) */#ifdef SPECIAL_CRTEXEextern int _fmode; /* default file translation mode */#else /* SPECIAL_CRTEXE */_CRTIMP extern int _fmode; /* default file translation mode */#endif /* SPECIAL_CRTEXE */_CRTIMP extern int _fileinfo; /* open file info mode (for spawn) *//* Windows major/minor and O.S. version numbers */_CRTIMP extern unsigned int _osver;_CRTIMP extern unsigned int _winver;_CRTIMP extern unsigned int _winmajor;_CRTIMP extern unsigned int _winminor;/* function prototypes */#if _MSC_VER >= 1200_CRTIMP __declspec(noreturn) void __cdecl abort(void);_CRTIMP __declspec(noreturn) void __cdecl exit(int);#else /* _MSC_VER >= 1200 */_CRTIMP void __cdecl abort(void);_CRTIMP void __cdecl exit(int);#endif /* _MSC_VER >= 1200 */#if defined (_M_MRX000)_CRTIMP int __cdecl abs(int);#else /* defined (_M_MRX000) */int __cdecl abs(int);#endif /* defined (_M_MRX000) */int __cdecl atexit(void (__cdecl *)(void));_CRTIMP double __cdecl atof(const char *);_CRTIMP int __cdecl atoi(const char *);_CRTIMP long __cdecl atol(const char *);#ifdef _M_M68K_CRTIMP long double __cdecl _atold(const char *);#endif /* _M_M68K */_CRTIMP void * __cdecl bsearch(const void *, const void *, size_t, size_t, int (__cdecl *)(const void *, const void *));_CRTIMP void * __cdecl calloc(size_t, size_t);_CRTIMP div_t __cdecl div(int, int);_CRTIMP void __cdecl free(void *);_CRTIMP char * __cdecl getenv(const char *);_CRTIMP char * __cdecl _itoa(int, char *, int);#if _INTEGRAL_MAX_BITS >= 64_CRTIMP char * __cdecl _i64toa(__int64, char *, int);_CRTIMP char * __cdecl _ui64toa(unsigned __int64, char *, int);_CRTIMP __int64 __cdecl _atoi64(const char *);#endif /* _INTEGRAL_MAX_BITS >= 64 */#if defined (_M_MRX000)_CRTIMP long __cdecl labs(long);#else /* defined (_M_MRX000) */long __cdecl labs(long);#endif /* defined (_M_MRX000) */_CRTIMP ldiv_t __cdecl ldiv(long, long);_CRTIMP char * __cdecl _ltoa(long, char *, int);_CRTIMP void * __cdecl malloc(size_t);_CRTIMP int __cdecl mblen(const char *, size_t);_CRTIMP size_t __cdecl _mbstrlen(const char *s);_CRTIMP int __cdecl mbtowc(wchar_t *, const char *, size_t);_CRTIMP size_t __cdecl mbstowcs(wchar_t *, const char *, size_t);_CRTIMP void __cdecl qsort(void *, size_t, size_t, int (__cdecl *) (const void *, const void *));_CRTIMP int __cdecl rand(void);_CRTIMP void * __cdecl realloc(void *, size_t);_CRTIMP int __cdecl _set_error_mode(int);_CRTIMP void __cdecl srand(unsigned int);_CRTIMP double __cdecl strtod(const char *, char **);_CRTIMP long __cdecl strtol(const char *, char **, int);#ifdef _M_M68K_CRTIMP long double __cdecl _strtold(const char *, char **);#endif /* _M_M68K */_CRTIMP unsigned long __cdecl strtoul(const char *, char **, int);#ifndef _MAC_CRTIMP int __cdecl system(const char *);#endif /* _MAC */_CRTIMP char * __cdecl _ultoa(unsigned long, char *, int);_CRTIMP int __cdecl wctomb(char *, wchar_t);_CRTIMP size_t __cdecl wcstombs(char *, const wchar_t *, size_t);#ifndef _MAC#ifndef _WSTDLIB_DEFINED/* wide function prototypes, also declared in wchar.h */_CRTIMP wchar_t * __cdecl _itow (int, wchar_t *, int);_CRTIMP wchar_t * __cdecl _ltow (long, wchar_t *, int);_CRTIMP wchar_t * __cdecl _ultow (unsigned long, wchar_t *, int);_CRTIMP double __cdecl wcstod(const wchar_t *, wchar_t **);_CRTIMP long __cdecl wcstol(const wchar_t *, wchar_t **, int);_CRTIMP unsigned long __cdecl wcstoul(const wchar_t *, wchar_t **, int); _CRTIMP wchar_t * __cdecl _wgetenv(const wchar_t *);_CRTIMP int __cdecl _wsystem(const wchar_t *);_CRTIMP int __cdecl _wtoi(const wchar_t *);_CRTIMP long __cdecl _wtol(const wchar_t *);#if _INTEGRAL_MAX_BITS >= 64_CRTIMP wchar_t * __cdecl _i64tow(__int64, wchar_t *, int);_CRTIMP wchar_t * __cdecl _ui64tow(unsigned __int64, wchar_t *, int); _CRTIMP __int64 __cdecl _wtoi64(const wchar_t *);#endif /* _INTEGRAL_MAX_BITS >= 64 */#define _WSTDLIB_DEFINED#endif /* _WSTDLIB_DEFINED */#endif /* _MAC */_CRTIMP char * __cdecl _ecvt(double, int, int *, int *);#if _MSC_VER >= 1200_CRTIMP __declspec(noreturn) void __cdecl _exit(int);#else /* _MSC_VER >= 1200 */_CRTIMP void __cdecl _exit(int);#endif /* _MSC_VER >= 1200 */_CRTIMP char * __cdecl _fcvt(double, int, int *, int *);_CRTIMP char * __cdecl _fullpath(char *, const char *, size_t);_CRTIMP char * __cdecl _gcvt(double, int, char *);unsigned long __cdecl _lrotl(unsigned long, int);unsigned long __cdecl _lrotr(unsigned long, int);#ifndef _MAC_CRTIMP void __cdecl _makepath(char *, const char *, const char *, const char *,const char *);#endif /* _MAC */_onexit_t __cdecl _onexit(_onexit_t);_CRTIMP void __cdecl perror(const char *);_CRTIMP int __cdecl _putenv(const char *);unsigned int __cdecl _rotl(unsigned int, int);unsigned int __cdecl _rotr(unsigned int, int);_CRTIMP void __cdecl _searchenv(const char *, const char *, char *);#ifndef _MAC_CRTIMP void __cdecl _splitpath(const char *, char *, char *, char *, char *);#endif /* _MAC */_CRTIMP void __cdecl _swab(char *, char *, int);#ifndef _MAC#ifndef _WSTDLIBP_DEFINED/* wide function prototypes, also declared in wchar.h */_CRTIMP wchar_t * __cdecl _wfullpath(wchar_t *, const wchar_t *, size_t);_CRTIMP void __cdecl _wmakepath(wchar_t *, const wchar_t *, const wchar_t *, const wchar_t *, const wchar_t *);_CRTIMP void __cdecl _wperror(const wchar_t *);_CRTIMP int __cdecl _wputenv(const wchar_t *);_CRTIMP void __cdecl _wsearchenv(const wchar_t *, const wchar_t *, wchar_t *);_CRTIMP void __cdecl _wsplitpath(const wchar_t *, wchar_t *, wchar_t *, wchar_t *, wchar_t *);#define _WSTDLIBP_DEFINED#endif /* _WSTDLIBP_DEFINED */#endif /* _MAC *//* --------- The following functions are OBSOLETE --------- *//* The Win32 API SetErrorMode, Beep and Sleep should be used instead. */#ifndef _MAC_CRTIMP void __cdecl _seterrormode(int);_CRTIMP void __cdecl _beep(unsigned, unsigned);_CRTIMP void __cdecl _sleep(unsigned long);#endif /* _MAC *//* --------- The preceding functions are OBSOLETE --------- */#if !__STDC__/* --------- The declarations below should not be in stdlib.h --------- *//* --------- and will be removed in a future release. Include --------- *//* --------- ctype.h to obtain these declarations. --------- */#ifndef tolower_CRTIMP int __cdecl tolower(int);#endif /* tolower */#ifndef toupper_CRTIMP int __cdecl toupper(int);#endif /* toupper *//* --------- The declarations above will be removed. --------- */#endif /* !__STDC__ */#ifdef _MTchar * __cdecl _getenv_lk(const char *); /* _MTHREAD_ONLY */#ifndef _MACwchar_t * __cdecl _wgetenv_lk(const wchar_t *); /* _MTHREAD_ONLY */#endif /* _MAC */int __cdecl _putenv_lk(const char *); /* _MTHREAD_ONLY */ #ifndef _MACint __cdecl _wputenv_lk(const wchar_t *); /* _MTHREAD_ONLY */ #endif /* _MAC */int __cdecl _mblen_lk(const char *, size_t); /* _MTHREAD_ONLY */int __cdecl _mbtowc_lk(wchar_t*,const char*,size_t); /* _MTHREAD_ONLY */ size_t __cdecl _mbstowcs_lk(wchar_t*,const char*,size_t); /* _MTHREAD_ONLY */int __cdecl _wctomb_lk(char*,wchar_t); /* _MTHREAD_ONLY */ size_t __cdecl _wcstombs_lk(char*,const wchar_t*,size_t); /* _MTHREAD_ONLY */#else /* _MT */#define _getenv_lk(envvar) getenv(envvar) /* _MTHREAD_ONLY */ #ifndef _MAC#define _wgetenv_lk(envvar) _wgetenv(envvar) /* _MTHREAD_ONLY */ #endif /* _MAC */#define _putenv_lk(envvar) _putenv(envvar) /* _MTHREAD_ONLY */ #ifndef _MAC#define _wputenv_lk(envvar) _wputenv(envvar) /* _MTHREAD_ONLY */ #endif /* _MAC */#define _mblen_lk(s,n) mblen(s,n) /* _MTHREAD_ONLY */ #define _mbtowc_lk(pwc,s,n) mbtowc(pwc,s,n) /* _MTHREAD_ONLY */ #define _mbstowcs_lk(pwcs,s,n) mbstowcs(pwcs,s,n) /* _MTHREAD_ONLY */ #define _wctomb_lk(s,wchar) wctomb(s,wchar) /* _MTHREAD_ONLY */ #define _wcstombs_lk(s,pwcs,n) wcstombs(s,pwcs,n) /* _MTHREAD_ONLY */ #endif /* _MT */#if !__STDC__/* Non-ANSI names for compatibility */#ifndef __cplusplus#define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))#endif /* __cplusplus */#define sys_errlist _sys_errlist#define sys_nerr _sys_nerr#define environ _environ_CRTIMP char * __cdecl ecvt(double, int, int *, int *);_CRTIMP char * __cdecl fcvt(double, int, int *, int *);_CRTIMP char * __cdecl gcvt(double, int, char *);_CRTIMP char * __cdecl itoa(int, char *, int);_CRTIMP char * __cdecl ltoa(long, char *, int);onexit_t __cdecl onexit(onexit_t);_CRTIMP int __cdecl putenv(const char *);_CRTIMP void __cdecl swab(char *, char *, int);_CRTIMP char * __cdecl ultoa(unsigned long, char *, int);#endif /* !__STDC__ */#ifdef __cplusplus}#endif /* __cplusplus */#ifdef _MSC_VER#pragma pack(pop)#endif /* _MSC_VER */#endif /* _INC_STDLIB */三、s tdlib.h用法1函数名称: calloc函数原型: void * calloc(unsigned n,unsign size);函数功能: 分配n个数据项的内存连续空间,每个数据项的大小为size 函数返回: 分配内存单元的起始地址,如果不成功,返回02函数名称: free函数原型: void free(void* p);函数功能: 释放p所指的内存区函数返回:参数说明: p-被释放的指针3函数名称: malloc函数原型: void * malloc(unsigned size);函数功能: 分配size字节的存储区函数返回: 所分配的内存区地址,如果内存不够,返回04函数名称: realloc函数原型: void * realloc(void * p,unsigned size);函数功能: 将p所指出的已分配内存区的大小改为size,size可以比原来分配的空间大或小函数返回: 返回指向该内存区的指针.NULL-分配失败5函数名称: rand函数原型: int rand(void);函数功能: 产生0到32767间的随机整数(0到0x7fff之间)函数返回: 随机整数6函数名称: abort函数原型: void abort(void)函数功能: 异常终止一个进程.7函数名称: exit函数原型: void exit(int state)函数功能: 程序中止执行,返回调用过程函数返回:参数说明: state:0-正常中止,非0-非正常中止8函数名称: getenv函数原型: char* getenv(const char *name)函数功能: 返回一个指向环境变量的指针函数返回: 环境变量的定义参数说明: name-环境字符串9函数名称: putenv函数原型: int putenv(const char *name)函数功能: 将字符串name增加到DOS环境变量中函数返回: 0:操作成功,-1:操作失败参数说明: name-环境字符串10函数名称: labs函数原型: long labs(long num)函数功能: 求长整型参数的绝对值函数返回: 绝对值11函数名称: atof函数原型: double atof(char *str)函数功能: 将字符串转换成一个双精度数值函数返回: 转换后的数值参数说明: str-待转换浮点型数的字符串12函数名称: atoi函数原型: int atoi(char *str)函数功能: 将字符串转换成一个整数值函数返回: 转换后的数值参数说明: str-待转换为整型数的字符串13函数名称: atol函数原型: long atol(char *str)函数功能: 将字符串转换成一个长整数函数返回: 转换后的数值参数说明: str-待转换为长整型的字符串14函数名称: ecvt函数原型: char *ecvt(double value,int ndigit,int *dec,int *sign) 函数功能: 将浮点数转换为字符串函数返回: 转换后的字符串指针参数说明: value-待转换底浮点数,ndigit-转换后的字符串长度15函数名称: fcvt函数原型: char *fcvt(double value,int ndigit,int *dec,int *sign) 函数功能: 将浮点数变成一个字符串函数返回: 转换后字符串指针参数说明: value-待转换底浮点数,ndigit-转换后底字符串长度。
c语言_头文件_stdlib
c语⾔_头⽂件_stdlib简介stdlib 头⽂件即standard library标准库头⽂件stdlib 头⽂件⾥包含了C、C++语⾔的最常⽤的系统函数该⽂件包含了C语⾔标准库函数的定义stdlib.h⾥⾯定义了五种类型、⼀些宏和通⽤⼯具函数。
类型例如size_t、wchar_t、div_t、ldiv_t和lldiv_t;宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等;常⽤的函数如malloc()、calloc()、realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()等等。
具体的内容你⾃⼰可以打开编译器的include⽬录⾥⾯的stdlib.h头⽂件看看。
内容1/*****stdlib.h - declarations/definitions for commonly used library functions** Copyright (c) Microsoft Corporation. All rights reserved.**Purpose:* This include file contains the function declarations for commonly* used library functio 2#if _MSC_VER > 1000#pragma once#endif3 #ifndef _INC_STDLIB#define _INC_STDLIB4 #include <crtdefs.h>#include <limits.h>5 #ifdef _MSC_VER/** * Currently, all MS C compilers for Win32 platforms default to 8 byte * alignment. */#pragma pack(push,_CRT_PACKING)#endif /** _MSC_VER */6 #ifdef __cplusplusextern "C" {#endif7/** Define NULL pointer value */#ifndef NULL#ifdef __cplusplus#define NULL 0#else#define NULL ((void *)0)#endif#endif8/** Definition of the argument values for the exit() function */9#define EXIT_SUCCESS 0#define EXIT_FAILURE 110 #ifndef _ONEXIT_T_DEFINED11#if !defined (_M_CEE_PURE) || (_MSC_VER < 1400)typedef int (__cdecl * _onexit_t)(void);#elsetypedef int (__clrcall * _onexit_t)(void);typedef _onexit_t _onexit_m_t;#endif12#if defined (_M_CEE_MIXED) && (_MSC_VER >= 1400)typedef int (__clrcall * _onexit_m_t)(void);#endif13#if !__STDC__/** Non-ANSI name for compatibility */#define onexit_t _onexit_t#endif14#define _ONEXIT_T_DEFINED#endif15/** Data structure definitions for div and ldiv runtimes. */16 #ifndef _DIV_T_DEFINED17 typedef struct _div_t { int quot; int rem;} div_t;18 typedef struct _ldiv_t { long quot; long rem;} ldiv_t;19#define _DIV_T_DEFINED#endif20/** * structs used to fool the compiler into not generating floating point * instructions when copying and pushing [long] double values */21 #ifndef _CRT_DOUBLE_DEC22 #ifndef _LDSUPPORT23#pragma pack(4)typedef struct { unsigned char ld[10];} _LDOUBLE;#pragma pack()24#define _PTR_LD(x) ((unsigned char *)(&(x)->ld))25#else26/** push and pop long, which is #defined as __int64 by a spec2k test */#pragma push_macro("long")#undef longtypedef long double _LDOUBLE;#pragma pop_macro("long")27#define _PTR_LD(x) ((unsigned char *)(x))28#endif29 typedef struct { double x;} _CRT_DOUBLE;30 typedef struct { float f;} _CRT_FLOAT;31/** push and pop long, which is #defined as __int64 by a spec2k test */#pragma push_macro("long")#undef long32 typedef struct { /** * Assume there is a long double type */long double x;} _LONGDOUBLE;33#pragma pop_macro("long")34#pragma pack(4)typedef struct { unsigned char ld12[12];} _LDBL12;#pragma pack()35#define _CRT_DOUBLE_DEC#endif36/** Maximum value that can be returned by the rand function. */37#define RAND_MAX 0x7fff38/** * Maximum number of bytes in multi-byte character in the current locale * (also defined in ctype.h). */#ifndef MB_CUR_MAX#if defined (_MT) && (defined (_M_CEE_PURE))#define MB_CUR_MAX ___mb_cur_max_func()#else#define MB_ 39/** Minimum and maximum macros */40#define __max(a,b) (((a) > (b)) ? (a) : (b))#define __min(a,b) (((a) < (b)) ? (a) : (b))41/** * Sizes for buffers used by the _makepath() and _splitpath() functions. * note that the sizes include space for 0-terminator */#define _MAX_PATH 260 /** max. length of full pathname */#define _MAX_DRIVE 3 /** max. length of drive c 42/** * Argument values for _set_error_mode(). */#define _OUT_TO_DEFAULT 0#define _OUT_TO_STDERR 1#define _OUT_TO_MSGBOX 2#define _REPORT_ERRMODE 343/** * Argument values for _set_abort_behavior(). */#define _WRITE_ABORT_MSG 0x1#define _CALL_REPORTFAULT 0x244/** * Sizes for buffers used by the getenv/putenv family of functions. */#define _MAX_ENV 3276745/** External variable declarations */#ifndef _CRT_ERRNO_DEFINED#define _CRT_ERRNO_DEFINED#ifdef _NTSUBSET_extern int errno;#else /** _NTSUBSET_ */_CRTIMP extern int * __cdecl _errno(void);#define errno (*_errno())46 errno_t __cdecl _set_errno(__in int _Value);errno_t __cdecl _get_errno(__out int * _Value);47#endif /** _NTSUBSET_ */#endif /** _CRT_ERRNO_DEFINED */48 #ifndef _NTSUBSET__CRTIMP unsigned long * __cdecl __doserrno(void);#define _doserrno (*__doserrno())49 errno_t __cdecl _set_doserrno(__in unsigned long _Value);errno_t __cdecl _get_doserrno(__out unsigned long * _Value);#endif /** _NTSUBSET_ */50#define _CRT_SYS_ERR_DATA51 #ifndef _CRT_SYS_ERR_DATA52/** you can't modify this, but it is non-const for backcompat */_CRTIMP _CRT_INSECURE_DEPRECATE(strerror) char ** __cdecl __sys_errlist(void);#define _sys_errlist (__sys_errlist())53 _CRTIMP _CRT_INSECURE_DEPRECATE(strerror) int * __cdecl __sys_nerr(void);#define _sys_nerr (*__sys_nerr())54#else /** _CRT_SYS_ERR_DATA */55 #ifndef _M_CEE_PURE56 _CRTIMP _CRT_INSECURE_DEPRECATE(strerror) extern char const * const _sys_errlist[]; /** perror error message table */_CRTIMP _CRT_INSECURE_DEPRECATE(strerror) extern const int _sys_nerr; /** # of entries in sys_errlis 57#endif58#endif /** _CRT_SYS_ERR_DATA */59#if defined(_DLL) && defined(_M_IX86)60 _CRTIMP int * __cdecl __p___argc(void);_CRTIMP char *** __cdecl __p___argv(void);_CRTIMP wchar_t *** __cdecl __p___wargv(void);_CRTIMP char *** __cdecl __p__environ(void);_CRTIMP wchar_t *** __cdecl __p__w 61#endif /** _M_IX86 && _DLL */62#if !defined(_M_CEE_PURE)_CRTIMP extern int __argc; /** count of cmd line args */_CRTIMP extern char ** __argv; /** pointer to table of cmd line args */_CRTIMP extern wchar_t ** __wargv; /** pointer to table of wide cmd line ar63 _CRTIMPerrno_t__cdecl_get_environ( __out char*** Value );64 _CRTIMPerrno_t__cdecl_get_wenviron( __out wchar_t*** Value );65#if !defined(_M_CEE_PURE)_CRTIMP extern char ** _environ; /** pointer to environment table */_CRTIMP extern wchar_t ** _wenviron; /** pointer to wide environment table */66 _CRT_INSECURE_DEPRECATE_GLOBALS(_get_pgmptr) _CRTIMP extern char * _pgmptr; /** points to the module (EXE) name */_CRT_INSECURE_DEPRECATE_GLOBALS(_get_wpgmptr) _CRTIMP extern wchar_t * _wpgmptr;67#else68 _CRTIMP char*** __cdecl __p__environ();_CRTIMP wchar_t*** __cdecl __p__wenviron();_CRT_INSECURE_DEPRECATE_GLOBALS(_get_pgmptr) _CRTIMP char** __cdecl __p__pgmptr();_CRT_INSECURE_DEPRECATE_GLOBALS(_get_ 69#define _environ (*__p__environ())#define _wenviron (*__p__wenviron())#define _pgmptr (*__p__pgmptr())#define _wpgmptr (*__p__wpgmptr())70#endif /** !defined(_M_CEE_PURE) */71 errno_t __cdecl _get_pgmptr(__deref_out_z char ** _Value);errno_t __cdecl _get_wpgmptr(__deref_out_z wchar_t ** _Value);72#if !defined(_M_CEE_PURE)_CRT_INSECURE_DEPRECATE_GLOBALS(_get_fmode) _CRTIMP extern int _fmode; /** default file translation mode */#else_CRTIMP int* __cdecl __p__fmode();#define _fmode (*__p__fmode())#endif /** 73 _CRTIMP errno_t __cdecl _set_fmode(__in int _Mode);_CRTIMP errno_t __cdecl _get_fmode(__out int * _PMode);74#if !defined(_M_CEE_PURE) __declspec(deprecated) _CRTIMP extern int _fileinfo; /** open file info mode (for spawn) */#endif /** !defined(_M_CEE_PURE) */__declspec(deprecated) errno_t __cdecl _set_fileinfo(int);__declspec(deprecate 75/** Windows major/minor and O.S. version numbers */76#pragma warning(push)#pragma warning(disable:4141)77#if !defined(_M_CEE_PURE)_CRT_INSECURE_DEPRECATE(_get_osplatform) _CRT_OBSOLETE(GetVersionEx) _CRTIMP extern unsigned int _osplatform;_CRT_INSECURE_DEPRECATE(_get_osver) _CRT_OBSOLETE(GetVersionEx) 78 #else_CRT_INSECURE_DEPRECATE(_get_osplatform) _CRT_OBSOLETE(GetVersionEx) _CRTIMP unsigned int* __cdecl __p__osplatform();_CRT_INSECURE_DEPRECATE(_get_osver) _CRT_OBSOLETE(GetVersionEx) _CRTIMP uns 79#define _osplatform (*__p__osplatform())#define _osver (*__p__osver())#define _winver (*__p__winver())#define _winmajor (*__p__winmajor())#define _winminor (*__p__winminor())#endif /** !defined(_M_CEE_PURE) */80#pragma warning(pop)81 _CRT_OBSOLETE(GetVersionEx) errno_t __cdecl _get_osplatform(__out unsigned int * _Value);_CRT_OBSOLETE(GetVersionEx) errno_t __cdecl _get_osver(__out unsigned int * _Value);_CRT_OBSOLETE(GetVersionEx) errno_t __cdecl _ 82/** _countof helper */#if !defined(_countof)#if !defined(__cplusplus)#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))#elseextern "C++"{template <typename _CountofType, size_t _SizeOfArray>char (*__countof_helper(UNALIGNED 83/** function prototypes */84 #ifndef _CRT_TERMINATE_DEFINED#define _CRT_TERMINATE_DEFINED_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);_CRTIMP __declspec(noreturn) void __cdecl _exit(__in int _Code);_CRTIMP void __cdecl abort(v85 _CRTIMP unsigned int __cdecl _set_abort_behavior(__in unsigned int _Flags, __in unsigned int _Mask);86 #ifndef _CRT_ABS_DEFINED#define _CRT_ABS_DEFINED int __cdecl abs(__in int _X); long __cdecl labs(__in long _X);#endif87#if _INTEGRAL_MAX_BITS >= 64 __int64 __cdecl _abs64(__int64);#endif#if _MSC_VER >= 1400 && defined(_M_CEE) __checkReturn int __clrcall _atexit_m_appdomain(__in_opt void (__clrcall * _Func)(void));#if defined(_M 88#if __STDC_WANT_SECURE_LIB___CRTIMP_ALT void __cdecl qsort_s(__inout_bcount(_NumOfElements* _SizeOfElements) void * _Base, __in rsize_t _NumOfElements, __in rsize_t _SizeOfElements, __in int (__cdecl * _PtFu89 _CRTIMP __checkReturn_opt errno_t __cdecl _mbstowcs_s_l(__out_opt size_t * _PtNumOfCharConverted, __out_ecount_part_opt(_SizeInWords, *_PtNumOfCharConverted) wchar_t * _DstBuf, __in size_t _SizeInWords, __in_ecount_z(_Ma90 _CRTIMP __checkReturn int __cdecl rand(void);#if defined(_CRT_RAND_S)_CRTIMP errno_t __cdecl rand_s ( __out unsigned int *_RandomValue);#endif91 _CRTIMP __checkReturn_opt int __cdecl _set_error_mode(__in int _Mode);92 _CRTIMP void __cdecl srand(__in unsigned int _Seed);_CRTIMP __checkReturn double __cdecl strtod(__in_z const char * _Str, __deref_opt_out_z char ** _EndPtr);_CRTIMP __checkReturn double __cdecl _strtod_l(__in_z const93#if _MSC_VER >= 1400 && defined(__cplusplus) && defined(_M_CEE)/** * Managed search routines. Note __cplusplus, this is because we only support * managed C++. */extern "C++"{#if __STDC_WANT_SECURE_LIB____checkReturn vo 94#if __STDC_WANT_SECURE_LIB__void __clrcall qsort_s(__inout_bcount(_NumOfElements*_SizeOfElements) void * _Base, __in rsize_t _NumOfElements, __in rsize_t _SizeOfElements, __in int (__clrcall * _PtFuncCompare)(void *, c95 #ifndef _CRT_ALLOCATION_DEFINED#define _CRT_ALLOCATION_DEFINED_CRTIMP _CRT_JIT_INTRINSIC _CRTNOALIAS _CRTRESTRICT __checkReturn __bcount_opt(_NumOfElements* _SizeOfElements) void * __cdecl calloc(_96 #ifndef _WSTDLIB_DEFINED97/** wide function prototypes, also declared in wchar.h */98 _CRTIMP_ALT __checkReturn_wat errno_t __ALTDECL _itow_s (__in int _Val, __out_ecount_z(_SizeInWords) wchar_t * _DstBuf, __in size_t _SizeInWords, __in int _Radix);__DEFINE_CPP_OVERLOAD_SECURE_FUNC_1_1(errno_t, _itow 99#if _INTEGRAL_MAX_BITS >= 64_CRTIMP_ALT __checkReturn_wat errno_t __ALTDECL _i64tow_s(__in __int64 _Val, __out_ecount_z(_SizeInWords) wchar_t * _DstBuf, __in size_t _SizeInWords, __in int _Radix);_CRTIMP _CRT_INSE 100#define _WSTDLIB_DEFINED#endif101/** Buffer size required to be passed to _gcvt, fcvt and other fp conversion routines*/#define _CVTBUFSIZE (309+40) /** # of digits in max. dp value + slop */102#if (defined(_DEBUG) || defined(_SYSCRT_DEBUG)) && defined(_CRTDBG_MAP_ALLOC)#pragma push_macro("_fullpath")#undef _fullpath#endif103 _CRTIMP __checkReturn char * __cdecl _fullpath(__out_ecount_z_opt(_SizeInBytes) char * _FullPath, __in_z const char * _Path, __in size_t _SizeInBytes);104#if (defined(_DEBUG) || defined(_SYSCRT_DEBUG)) && defined(_CRTDBG_MAP_ALLOC)#pragma pop_macro("_fullpath")#endif105 _CRTIMP_ALT __checkReturn_wat errno_t __cdecl _ecvt_s(__out_ecount_z(_Size) char * _DstBuf, __in size_t _Size, __in double _Val, __in int _NumOfDights, __out int * _PtDec, __out int * _PtSign);__DEFINE_CPP_OVERLOAD_SECUR 106 _CRTIMP __checkReturn int __cdecl _atodbl(__out _CRT_DOUBLE * _Result, __in_z char * _Str);_CRTIMP __checkReturn int __cdecl _atoldbl(__out _LDOUBLE * _Result, __in_z char * _Str);_CRTIMP __checkReturn int __cdecl _atoflt(__ 107#if _MSC_VER >= 1400 && defined(_M_CEE) _onexit_m_t __clrcall _onexit_m_appdomain(_onexit_m_t _Function); #if defined(_M_CEE_MIXED) _onexit_m_t __clrcall _onexit_m(_onexit_m_t _Function); #else inline _onexit_m_t __ 108 _CRT_INSECURE_DEPRECATE(_splitpath_s) _CRTIMP void __cdecl _splitpath(__in_z const char * _FullPath, __out_z_opt char * _Drive, __out_z_opt char * _Dir, __out_z_opt char * _Filename, __out_z_opt char * _Ext);_CRTIMP_ALT _ 109 _CRTIMP void __cdecl _swab(__inout_ecount_full(_SizeInBytes) char * _Buf1, __inout_ecount_full(_SizeInBytes) char * _Buf2, int _SizeInBytes);110 #ifndef _WSTDLIBP_DEFINED111/** wide function prototypes, also declared in wchar.h */112#if (defined(_DEBUG) || defined(_SYSCRT_DEBUG)) && defined(_CRTDBG_MAP_ALLOC)#pragma push_macro("_wfullpath")#undef _wfullpath#endif113 _CRTIMP __checkReturn wchar_t * __cdecl _wfullpath(__out_ecount_z_opt(_SizeInWords) wchar_t * _FullPath, __in_z const wchar_t * _Path, __in size_t _SizeInWords);114#if (defined(_DEBUG) || defined(_SYSCRT_DEBUG)) && defined(_CRTDBG_MAP_ALLOC)#pragma pop_macro("_wfullpath")#endif115 _CRTIMP_ALT __checkReturn_wat errno_t __ALTDECL _wmakepath_s(__out_ecount_z(_SizeInWords) wchar_t * _PathResult, __in_opt size_t _SizeInWords, __in_z_opt const wchar_t * _Drive, __in_z_opt const wchar_t * _Dir, __in_z_opt 116#define _WSTDLIBP_DEFINED#endif117/** The Win32 API SetErrorMode, Beep and Sleep should be used instead. */_CRT_OBSOLETE(SetErrorMode) _CRTIMP void __cdecl _seterrormode(__in int _Mode);_CRT_OBSOLETE(Beep) _CRTIMP void __cdecl _beep(__in unsigned _F 118#if !__STDC__119/** Non-ANSI names for compatibility */120 #ifndef __cplusplus#define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))#endif121#define sys_errlist _sys_errlist#define sys_nerr _sys_nerr#define environ _environ122#pragma warning(push)#pragma warning(disable: 4141) /** Using deprecated twice */ _CRT_NONSTDC_DEPRECATE(_ecvt) _CRT_INSECURE_DEPRECATE(_ecvt_s) _CRTIMP __checkReturn char * __cdecl ecvt(__in double _Val, __in in 123#endif /** __STDC__ */124 #ifdef __cplusplus}125#endif126 #ifdef _MSC_VER#pragma pack(pop)#endif /** _MSC_VER */127#endif /** _INC_STDLIB *//** 88bf0570-3001-4e78-a5f2-be5765546192 */View Code包含的函数输⼊样式:C语⾔模式:#include <stdlib.h>C++样式:#include <cstdlib>1函数名称:calloc函数原型: void * calloc(unsigned n,unsigned size);函数功能: 分配n个数据项的内存连续空间,每个数据项的⼤⼩为size函数返回: 分配内存单元的起始地址,如果不成功,返回02函数名称:free函数原型: void free(void* p);函数功能: 释放p所指的内存区函数返回:参数说明: p-被释放的指针3函数名称:malloc函数原型: void * malloc(unsigned size);函数功能: 分配size字节的存储区函数返回: 所分配的内存区地址,如果内存不够,返回04函数名称: realloc函数原型: void * realloc(void * p,unsigned size);函数功能: 将p所指出的已分配内存区的⼤⼩改为size,size可以⽐原来分配的空间⼤或⼩函数返回: 返回指向该内存区的指针.NULL-分配失败5函数名称: rand函数原型: int rand(void);函数功能: 产⽣0到32767间的随机整数(0到0x7fff之间)函数返回: 随机整数6函数名称: abort函数原型: void abort(void)函数功能: 异常终⽌⼀个进程.7函数名称: exit函数原型: void exit(int state)函数功能: 程序中⽌执⾏,返回调⽤过程函数返回:参数说明: state:0-正常中⽌,⾮0-⾮正常中⽌8函数名称: getenv函数原型: char* getenv(const char *name)函数功能: 返回⼀个指向环境变量的指针函数返回:环境变量的定义参数说明: name-环境字符串9函数名称: putenv函数原型: int putenv(const char *name)函数功能: 将字符串name增加到DOS环境变量中函数返回: 0:操作成功,-1:操作失败参数说明: name-环境字符串10函数名称: labs函数原型: long labs(long num)函数功能: 求长整型参数的绝对值函数返回:绝对值11函数名称: atof函数原型: double atof(char *str)函数功能: 将字符串转换成⼀个双精度数值函数返回: 转换后的数值参数说明: str-待转换浮点型数的字符串12函数名称: atoi函数原型: int atoi(char *str)函数功能: 将字符串转换成⼀个整数值函数返回: 转换后的数值参数说明: str-待转换为整型数的字符串13函数名称: atol函数原型: long atol(char *str)函数功能: 将字符串转换成⼀个长整数函数返回: 转换后的数值参数说明: str-待转换为长整型的字符串14函数名称:ecvt函数原型: char *ecvt(double value,int ndigit,int *dec,int *sign)函数功能: 将浮点数转换为字符串函数返回: 转换后的字符串指针参数说明: value-待转换底浮点数,ndigit-转换后的字符串长度15函数名称:fcvt函数原型: char *fcvt(double value,int ndigit,int *dec,int *sign)函数功能: 将浮点数变成⼀个字符串函数返回: 转换后字符串指针参数说明: value-待转换底浮点数,ndigit-转换后底字符串长度。
常见函数和所在头文件
一、头文件math.h中1.y=abs(x); / y=fabs(x); 求x的绝对值,结果放在y中2.z=pow(x,y); 求x的y次方,结果放在z中3.y=sqrt(x); 给x开根号,结果放在y中二、头文件ctype.h中1.isalnum(ch) 检验ch是否是字母或数字,是就返回1,否则返回02.isalpha(ch) 检验ch是否是字母,是就返回1,否则返回03.isdigit(ch) 检验ch是否是数字,是就返回1,否则返回04.islower(ch) 检验ch是否是小写字母,是就返回1,否则返回05.isupper(ch) 检验ch是否是大写字母,是就返回1,否则返回06.tolower(ch) ch转换为小写字母,返回ch所代表的字符的小写字母7.toupper(ch) ch转换为大写字母,返回ch所代表的字符的大写字母三、头文件string.h中1.strcat(字符数组名1,字符数组名2)//把字符数组2中的字符串连接到字符数组1中字符串的后面,返回字符数组1的首地址2.strcpy(字符数组名1,字符数组名2)//把字符数组2中的字符串拷贝到字符数组1中,返回字符数组1的首地址3.strcmp(字符数组名1,字符数组名2)//按照ASCII码顺序比较两个数组的字符串,并由函数返回值返回比较的结果,若相当,返回0,若第一个大,返回>0,否则返回<04.strlen(字符数组名)//测字符串的实际长度,返回字符个数四、头文件stdio.h中1.eof(fp) 检验fp指向的文件是否结束,结束返回非0值,否则返回02.fclose(fp) 关闭文件3.ch=fgetc(fp) 从fp中读出一个字符放入ch中4.fgets(str,n,fp) 从fp中读出一个长度为n-1字符串放入字符数组str中5.fopen 打开文件6.fputc函数:将一个字符写到磁盘文件上7.fread函数:用来读一个数据块8.fwrite函数:用来写一个数据块9.fprintf函数:格式化写函数10.fscanf函数:格式化读函数,针对的不是终端而是磁盘文件11.rewind函数:使位置指针重新返回文件的开头12.fseek函数:改变文件的位置指针13.ch=getchar( ) 从键盘读入一个字符,放入ch中14.putchar(ch) 输出ch中的字符15.gets(str) 从键盘输入一串字符到字符数组str中16.puts(str) 输出字符数组str中的字符串五、头文件stdlib.h或malloc.h中1.malloc(size) 分配size个字节的存储区,并返回该存储区首地址2.free(p) 释放p所指的内存区。
malloc使用方法
malloc使用方法
malloc是C语言中用来动态分配内存空间的函数,它的使用方法如下:
1. 首先需要包含头文件stdlib.h。
2. malloc函数的原型为void* malloc(size_t size),其中size_t是一个无符号整数类型,表示需要分配的字节数。
3. 假设需要分配一个长度为n的整型数组,可以使用以下代码: int *a;
a = (int*)malloc(n * sizeof(int));
4. 这里使用了类型转换(int*),将void指针转换为int指针,以便可以对其进行操作。
5. 如果分配成功,malloc函数会返回一个指向分配内存的指针,如果分配失败则返回NULL。
6. 分配的内存可以通过指针进行操作,使用完毕后需要使用free函数释放内存空间,避免内存泄漏。
7. 释放内存的方法为free(a),其中a为指向分配内存的指针。
8. 在使用malloc函数分配内存时,需要注意一些细节,如分配的内存空间是否够用,是否需要进行类型转换等等。
9. 在使用malloc函数时,要尽量避免越界访问内存,否则会导致程序崩溃或者出现未知错误。
10. malloc函数是C语言中常用的函数之一,掌握其使用方法对于C语言程序员来说至关重要。
c语言分配内存并且赋值的函数
C语言分配内存并赋值的函数1. 概述在C语言中,我们经常需要动态地分配内存来存储数据。
为了方便地进行内存分配和赋值操作,C语言提供了一些特定的函数。
这些函数可以帮助我们在程序运行时动态地分配内存,并将指定的值赋给所分配的内存空间。
本文将详细介绍C语言中的几个常用的分配内存并赋值的函数,包括malloc、calloc和realloc。
我们将分别介绍它们的定义、用途和工作方式,并给出一些示例代码来说明它们的使用方法。
2. malloc函数2.1 定义malloc函数是C语言中用于动态分配内存的函数。
它的原型定义在stdlib.h头文件中,其定义如下:void* malloc(size_t size);2.2 用途malloc函数用于在程序运行时动态地分配指定大小的内存空间。
这个函数返回一个指向分配内存的指针,如果分配失败则返回NULL。
2.3 工作方式malloc函数的工作方式如下:1.接收一个size_t类型的参数size,表示需要分配的内存空间的大小。
2.在堆(heap)中分配一块大小为size的连续内存空间。
3.返回指向分配内存的指针,如果分配失败则返回NULL。
2.4 示例代码下面是一个使用malloc函数分配内存并赋值的示例代码:#include <stdio.h>#include <stdlib.h>int main() {int* ptr;int size = 5;ptr = (int*)malloc(size * sizeof(int));if (ptr == NULL) {printf("内存分配失败\n");return 1;}for (int i = 0; i < size; i++) {ptr[i] = i + 1;}for (int i = 0; i < size; i++) {printf("%d ", ptr[i]);}free(ptr);return 0;}上述代码中,我们使用malloc函数分配了一块大小为 5 * sizeof(int)的内存空间,并将其地址赋给指针ptr。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
stdlib.h主要包含的函数
包含函数:
1函数名称: calloc
函数原型: void * calloc(unsigned n,unsign size);
函数功能: 分配n个数据项的内存连续空间,每个数据项的大小为size
函数返回: 分配内存单元的起始地址,如果不成功,返回0
2函数名称: free
函数原型: void free(void* p);
函数功能: 释放p所指的内存区
函数返回:
参数说明: p-被释放的指针
3函数名称: malloc
函数原型: void * malloc(unsigned size);
函数功能: 分配size字节的存储区
函数返回: 所分配的内存区地址,如果内存不够,返回0
4函数名称: realloc
函数原型: void * realloc(void * p,unsigned size);
函数功能: 将p所指出的已分配内存区的大小改为size,size可以比原来分配的空间大或小
函数返回: 返回指向该内存区的指针.NULL-分配失败
5函数名称: rand
函数原型: int rand(void);
函数功能: 产生0到32767间的随机整数(0到0x7fff之间)
函数返回: 随机整数
6函数名称: abort
函数原型: void abort(void)
函数功能: 异常终止一个进程.
7函数名称: exit
函数原型: void exit(int state)
函数功能: 程序中止执行,返回调用过程
函数返回:
参数说明: state:0-正常中止,非0-非正常中止
8函数名称: getenv
函数原型: char* getenv(const char *name)
函数功能: 返回一个指向环境变量的指针
函数返回: 环境变量的定义
参数说明: name-环境字符串
9函数名称: putenv
函数原型: int putenv(const char *name)
函数功能: 将字符串name增加到DOS环境变量中函数返回: 0:操作成功,-1:操作失败
参数说明: name-环境字符串
10函数名称: labs
函数原型: long labs(long num)
函数功能: 求长整型参数的绝对值
函数返回: 绝对值
11函数名称: atof
函数原型: double atof(char *str)
函数功能: 将字符串转换成一个双精度数值
函数返回: 转换后的数值
参数说明: str-待转换浮点型数的字符串
12函数名称: atoi
函数原型: int atoi(char *str)
函数功能: 将字符串转换成一个整数值
函数返回: 转换后的数值
参数说明: str-待转换为整型数的字符串
13函数名称: atol
函数原型: long atol(char *str)
函数功能: 将字符串转换成一个长整数
函数返回: 转换后的数值
参数说明: str-待转换为长整型的字符串
14函数名称: ecvt
函数原型: char *ecvt(double value,int ndigit,int *dec,int *sign) 函数功能: 将浮点数转换为字符串
函数返回: 转换后的字符串指针
参数说明: value-待转换的浮点数,ndigit-转换后的字符串长度
15函数名称: fcvt
函数原型: char *fcvt(double value,int ndigit,int *dec,int *sign) 函数功能: 将浮点数变成一个字符串
函数返回: 转换后字符串指针
参数说明: value-待转换的浮点数,ndigit-转换后的字符串长度。