首页 > 其他分享 >C语言标准库

C语言标准库

时间:2023-09-24 19:32:40浏览次数:44  
标签:const stream int void C语言 char 标准 size

https://www.gnu.org/software/libc/manual/pdf/libc.pdf

https://cplusplus.com/reference/clibrary/

NOTICE

  1. 不能在头文件中定义全局变量,否则该变量会存在于任何#include <ThisHeader_H>的地方。

  2. 要学会防御式编程,即要保证头文件的幂等性

    #ifndef _STDIO_H
    	#define _STDIO_H
    	... /* Body of <stdio.h> */
    #endif
    
  3. 一个程序应该可以包含两个定义了相同名字的头文件而不会造成错误

    • 例如size_t,使用另一个宏保护来防止这种类型的多次定义

      #ifndef _SIZE_T
      #define _SIZE_T
      typedef unsigned int size_t;
      #endif
      
    • 而例如NULL,则不需要考虑这种问题,#define NULL (void*) 0,标准C允许宏的良性重定义,具有相同宏名的两个定义必须具有相同的记号序列。

1. 初级

1.1 <stdio.h>

It defines three variable types, several macros, and various functions for performing input and output.

1.1.1 Library Variables

Variable Description
size_t This is the unsigned int type and is the result of the sizeof keyword.
FILE This is an object type suitable for storing information for a file stream.
fpos_t This is an object type suitable for storing any position in a file.

1.1.2 Library Macros

Macro Description
NULL It is the value of a null pointer constant.
_IOFBF, _IOLBF and _IONBF These which expand to integral constant expressions with distinct values and suitable for the use as third argument to the setvbuf function.
BUFSIZ It is an int, which represents the size of the buffer used by the setbuf functions.
EOF This macro is a negative integer, which indicates that the end-of-file has been reached.
FOPEN_MAX It is an integer, which represents the maximum number of files that the system can guarantee to be opened simultaneously.
FILENAME_MAX It is an integer, which represents the longest length of a char array suitable for holding the longest possible filename. If the implementation imposes no limit, then this value should be the recommended maximum value.
L_tmpnam It is an integer, which represents the longest length of a char array suitable for holding the longest possible temporary filename created by the tmpnam function.
SEEK_CUR, SEEK_END, and SEEK_SET These are used in the seek function to locate different positions in a file.
TMP_MAX It is the maximum number of unique filenames that the function tmpnam can generate.
stderr, stdin and stdout These are pointers to FILE types which correspond to the standard error, standard input, and standard output systems.

1.1.3 Library Functions

Function Description
int fclose(FILE *stream) Closes the stream. All buffers are flushed.
void clearerr(FILE *stream) Clears the end-of-file and error indicators for the given stream.
int feof(FILE *stream) Tests the end-of-file indicator for the given stream.
int ferror(FILE *stream) Tests the error indicator for the given stream.
int fflush(FILE *stream) Flushes the output buffer of a stream.
int fgetpos(FILE *stream, fpos_t *pos) Gets the current file position of the stream and writes it to pos.
FILE *fopen(const char *filename, const char *mode) Opens the filename pointed to by filename using the given mode.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) Reads data from the given stream into the array pointed to by ptr.
FILE *freopen(const char *filename, const char *mode, FILE *stream) Associates a new filename with the given open stream and same time closing the old file in stream.
int fseek(FILE *stream, long int offset, int whence) Sets the file position of the stream to the given offset. The argument offset signifies the number of bytes to seek from the given whence position.
int fsetpos(FILE *stream, const fpos_t *pos) Sets the file position of the given stream to the given position. The argument pos is a position given by the function fgetpos.
long int ftell(FILE *stream) Returns the current file position of the given stream.
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) Writes data from the array pointed to by ptr to the given stream.
int remove(const char *filename) Deletes the given filename so that it is no longer accessible.
int rename(const char *old_filename, const char *new_filename) Causes the filename referred to, by old_filename to be changed to new_filename.
void rewind(FILE *stream) Sets the file position to the beginning of the file of the given stream.
void setbuf(FILE *stream, char *buffer) Defines how a stream should be buffered.
int setvbuf(FILE *stream, char *buffer, int mode, size_t size) Another function to define how a stream should be buffered.
FILE *tmpfile(void)
  • FILE* fopen(const char* fname, const char* mode);

  • int fclose(FILE* file);

  • int fgetc(FILE* in);

  • char* fgets(char* dest, int n, FILE* in);

  • int fputc(int ch, FILE* out);

  • int ungetc(int ch, FILE* in);

  • int printf(const char* format_string...);

  • int scanf(const char* format, ...);

1.2 <ctype.h>

1.2.1 环境变量

environ保存了当前的环境变量,

int setenv(const char *name, const char *value, int overwrite);

int unsetenv(const char *name);

char *getenv(const char *name);

1.3 <stdlib.h>

1.4 <string.h>

2. 中级

2.1assert.h

#include <assert.h>
void assert(int expression);
#define assert(ignore) ((void) 0)
  1. <assert.h>提供宏assert的定义

  2. 用于断言

  3. 配合宏NDEBUG使用

    1. 如果程序中某个包含<assert.h>的地方没有定义NDEBUG,则该头文件就会将宏assert定义为活动形式,它就可以展开为一个表达式,测试断言,并在断言为假的时候输出一条错误 信息,然后程序终止。

    2. 反之,如果定义了NDEBUG,头文件就会将这个宏定义为不执行任何操作的静止形式。

    3. 如果你认为不需要断言,则只需要如下添加。但是这种方式,当重新需要断言的时候,必须移除上述define,还需要重新编译头文件

      #define NDEBUG /* disable assertion */
      #include <assert.h>
      
    4. 另一种方式,是由编译器提供支持,在那里定义NDEBUG说明断言无效。

  4. 宏的参数表面上是一个整型表达式,如果表达式的值为0,宏就会写出一条信息并终止程序的运行。

  5. 宏不能直接调用库的任何输出函数,也不能引用宏。

2.2 <limits.h>

2.3 <stddef.h>

2.4 <time.h>

3. 高级

3.1 <float.h>

3.2 <math.h>

3.3 <error.h>

3.4 <locale.h>

3.5 <setjmp.h>

3.6 <signal.h>

3.7 <stdarg.h>

描述
assert.h a macro called assert which can be used to verify assumptions made by the program
stdio.h file input and output
ctype.h character tests
string.h string operations
math.h mathematical functions such as sin() and cos()
stdlib.h utility functions such as malloc() and rand()
assert.h the assert() debugging macro
stdarg.h support for functions with variable numbers of arguments
setjmp.h support for non-local flow control jumps
signals.h support for exceptional condition signals
time.h date and time
limits.h, float.h constants which define type range values such as INT_MAX

ctype.h

  • isalpha(ch)
  • islower(ch)
  • isspace(ch)
  • isdigit(ch)
  • toupper(ch), tolower(ch)

string.h

  • size_t strlen(const char* string);
  • char* strcpy(char* dest, const char* source);
  • size_t strlcpy(char* dest, const char* source, size_t dest_size);
  • char* strcat(char* dest, const char* source);
  • int strcmp(const char* a, const char* b);
  • char* strchr(const char* searchIn, char ch);
  • char* strstr(const char* searchIn, const char* searchFor);
  • void* memcpy(void* dest, const void* source, size_t n);
  • void* memmove(void* dest, const void* source, size_t n);

stdlib.h

  • int rand();
  • void srand(unsigned int seed);
  • void* malloc(size_t size);
  • void free(void* block);
  • void* realloc(void* block, size_t size);
  • void exit(int status);
  • void* bsearch(const void* key, const void* base, size_t len, size_t elem_size, <compare_function>);
  • void qsort(void* base, size_t len, size_t elem_size, <compare_function>);

标签:const,stream,int,void,C语言,char,标准,size
From: https://blog.51cto.com/basilguo/7587951

相关文章

  • 282_面对疫情,该如何选择口罩?这份标准下载指南请拿好
    这是一篇原发布于2020-01-2816:06:00得益小站的文章,备份在此处。前言最近新型肺炎的话题越来越引人关注了,户外有着病毒的危险,又正逢春节佳期,就连我们小区的广播都循环播放着病毒的防护知识,所以是不是好多人和轶哥一样宅在家里陪伴家人呢?虽然病毒暂时隔离了人们的交流,但隔离不......
  • PPT| 基于标准化规范化IT运维管理整体解决方案P48
        本人在四大咨询机构从事咨询工作多年,二十年一线数字化规划咨询经验,提供制造业数智化转型规划服务,顶层规划/企业架构/数据治理/数据安全解决方案资料干货.   【智能制造数字化咨询】该PPT共86页,由于篇幅有限,以下为部分资料,如需完整原版 方案,点击关注下方。  ......
  • C语言学习记录---函数3
    声明#include<stdio.h>#include<string.h>#include<windows.h>#include<stdlib.h>#include<time.h>#include<math.h>7.3递归与迭代7.3.1练习3:求n的阶乘。(不考虑溢出)参考代码:intFacl(intn){if(n>1){returnn=n*Facl......
  • C语言char类型的存储
    (目录)char是如何存储的字符型(char)用于储存字符(character),如英文字母或标点。但是char类型在内存中并不是以字符的形式储存,而是以ASII码的形式储存,也可以说char类型储存的实际上是整数。所以char类型也被归类为整形家族。intmain(){ charc='A'; printf("%d\n",c); print......
  • C语言-字符串相关库函数用法+模拟实现
    常见的与字符串有关的库函数strstr()寻找子字符串strcat()字符串追加函数strcmp()字符串比较函数strcpy()字符串拷贝函数strlen()求解字符串长度...1.strstr()寻找子字符串我们先来看MSDN中对该函数的功能描述:Findasubstring.(寻找子......
  • C语言-字符串相关库函数用法+模拟实现
    常见的与字符串有关的库函数strstr()寻找子字符串strcat()字符串追加函数strcmp()字符串比较函数strcpy()字符串拷贝函数strlen()求解字符串长度...1.strstr()寻找子字符串我们先来看MSDN中对该函数的功能描述:Findasubstring.(寻找子......
  • C语言实现身份运行游戏
    C语言实现:需要一款软件,程序,EXE等都可以假设我自己的程序名为游戏.exe需要的程序为XX.exe已有条件:来宾用户已启用,用户名,密码都有,现成的以其他用户身份运行的bat文件也有,双击bat文件,即可以其他用户身份运行游戏.exe需求:首先打开XX.exe该程序直接后台运行,不需要界面或任何提示,......
  • C++11新标准
    c++11标准(1)一、longlong类型新增了类型longlong和unsignedlonglong,以支持64位(或更宽)的整型。在VS中,int和long都是4字节,longlong是8字节。在Linux中,int是4字节,long和longlong是8字节。二、char16_t和char32_t类型新增了类型char16_t和char32_t,以支持16位和32位......
  • 【C语言版】扫雷游戏
     思路设置两个数组初始化两个数组打印数组设置雷排查雷game.h#pragmaonce#defineROW9//定义行为9#defineCOL9//定义列为9#defineROWSROW+2//排查雷时防止溢出#defineCOLSCOL+2#defineEASY_COUNT10//初步设有10个雷voidInitBoard(charboard[ROWS][COLS],i......
  • 【C语言版】扫雷游戏
     思路设置两个数组初始化两个数组打印数组设置雷排查雷game.h#pragmaonce#defineROW9//定义行为9#defineCOL9//定义列为9#defineROWSROW+2//排查雷时防止溢出#defineCOLSCOL+2#defineEASY_COUNT10//初步设有10个雷voidInitBoard(charboard[ROWS][COLS],in......