C standard library: stdio.h,ctype.h, stdlib.h, assert.h, stdarg.h, time.h
<stdio.h>: File operations
int remove(const char∗ filename)
• removes the file from the filesystem.
• retrn non-zero on error.
int rename(const char∗ oldname,const char∗ newname)
• renames file
• returns non-zero on error(reasons?: permission, existence)
第一次发现C的<stdio.h>中有这两个函数,一直觉得文件操作很复杂和神秘,现在看还是很简单的。不过底层应该会涉及到安全机制,尤其是在涉及到多种操作系统时,不同的安全策略也许会带来很大的麻烦。
<stdio.h>:Temporary files
FILE∗ tmpfile(void)
• creates a temporary file withmode "wb+".
• the file is removed automaticallywhen program terminates.
char∗ tmpnam(char s[L_tmpnam])
• creates a string that is not thename of an existing file.
• return reference to internalstatic array if s is NULL. Populate s otherwise.
• generates a new name every call.
这个临时的命名用时间来命名是最合适的了。\
<stdio.h>: Raw I/O
size_t fread(void∗ ptr , size_t size, size_t nobj,FILE∗stream)
• reads at most nobj items of sizesize from stream into ptr.
• returns the number of items read.
• feof and ferror must be used totest end of file.
size_t fwrite (const void∗ ptr,size_t size, size_t nobj,FILE∗stream)
• write at most nobj items of sizesize from ptr onto stream.
• returns number of objects written.
fread使我想起了malloc时对所获得的内存进行强制类型转换,大概也是这个样子吧,将新的内存段按大小分,再登记下这段内存的类型。
<stdio.h>: File position
int fseek(FILE∗ stream, long offset,int origin )
• sets file position in the stream.Subsequent read/write begins at this location
• origin can be SEEK_SET, SEEK_CUR,SEEK_END.
• returns non-zero on error.
long ftell (FILE∗ stream)
• returns the current positionwithin the file. (limitation? long data type).
• returns -1L on error.
int rewind(FILE∗ stream)
• sets the file pointer at thebeginning.
• equivalent tofseek(stream,0L,SEEK_SET);
这几个函数用的多一些,记得上课时就学过。在进行文件流操作时,至少要记录文件的起始位置和当前文件指针的位置。
<stdio.h>: File errors
void clearerr (FILE∗ stream)
• clears EOF and other errorindicators on stream. int feof (FILE∗ stream)
int feof (FILE∗ stream)
• return non-zero (TRUE) if end offile indicator is set for stream.
• only way to test end of file forfunctions such as fwrite(),fread()
int ferror (FILE∗ stream)
• returns non-zero (TRUE) if anyerror indicator is set for stream.
<string.h>: Memory functions
void∗ memcpy(void∗ dst,const void∗ src,size_t n)
• copies n bytes from src tolocation dst
• returns a pointer to dst.
• src and dst cannot overlap.
void∗ memmove(void∗ dst,const void∗ src,size_t n)
• behaves same as memcpy() function.
• src and dst can overlap.
int memcmp(const void∗ cs,const void∗ ct,int n)
• compares first n bytes between csand ct.
void∗ memset(void∗ dst,int c,int n)
• fills the first n bytes of dstwith the value c.
• returns a pointer to dst
memcpy相对memmove最大的好处就是方便并行化处理吧。这两个名字起的很精彩,copy就是copy,move就是move。在设计类库时多考虑下数据的独立性是很好的编程习惯。
<stdlib.h>:Utility
double atof(const char∗ s)
int atoi (const char∗ s)
long atol(const char∗ s)
• converts character to float,integerand long respectively.
int rand()
• returns a pseduo-random numbersbetween 0 and RAND_MAX
void srand(unsigned int seed)
• sets the seed for thepseudo-random generator!
前面的几个函数实现起来好像并不难。就是整型转浮点的实现可能麻烦一些,我还想不到特别优雅的方法。
<stdlib.h>: Exiting
void abort(void)
• causes the program to terminateabnormally.
void exit ( int status)
• causes normal program termination.The value status is returned to the operating system.
• 0 EXIT_SUCCESS indicatessuccessful termination. Any other value indicates failure(EXIT_FAILURE)
void atexit (void (∗fcn )( void))
• registers a function fcn to becalled when the program terminates normally;
• returns non zero when registrationcannot be made.
• After exit() is called, thefunctions are called in reverse order of registration.
int system(const char∗ cmd)
• executes the command in stringcmd.
• if cmd is not null, the programexecutes the command and returns exit status returned by the command
system这个函数好像python里面也有,并且在python里面用的很多。atexit的执行顺序为什么是逆序,实在想不通。
<stdlib.h>:Searchign and sortin
void∗ bsearch(const void∗ key, const void∗ base, size_t n,size_t size, int(∗cmp)(const void∗ keyval, const void∗ datum));
• searches base[0] through base[n-1] for *key.
• function cmp() is used to perform comparison.
• returns a pointer to the matching item if it exists and NULLotherwise.
void qsort(void∗ base, size_t n, size_t sz, int(∗cmp)(constvoid∗, const void∗));
• sorts base[0] through base[n-1] in ascending/descending order.
• function cmp() is used to perform comparison.
搜索和排序中的比较函数指针是使用函数指针的一个典型例子。bsearch和qsearch如此常用,以至于很多编程语言的库函数都将其包含在内。
<assert.h>: Diagnostics
void assert(int expression)
• used to check for invariants/codeconsistency during debugging
• does nothing when expression istrue.
• prints an error messageindicating, expression, filename and line number.
Alternative ways to print filename and line number duringexecution is to use: __FILE__,__LINE__ macros.
assert函数在发布为release时应该会去掉,现在也明白了为什么JUnit里面那么多aassert打头的函数了。
<stdarg.h>:Variable argument lists
Variable argument lists:
• functions can variable number ofarguments.
• the data type of the argument canbe different for each argument.
• atleast one mandatory argument isrequired.
• Declaration:
int printf (char∗ fmt ,...); /∗fmt is last named argument∗/
va_list ap
• ap defines an iterator that willpoint to the variable argument.
• before using, it has to beinitialized using va_start.
C与Java、C#等运行在虚拟机中的解释性语言不同,是彻底的编译运行,因此这种动态特性是由预处理的宏来实现的。
分享到:
相关推荐
lec10.ppt lec11.ppt lec12.ppt lec13.ppt lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt lec19.ppt lec2.ppt lec20.ppt lec21.ppt lec22.ppt lec23.ppt lec24.ppt lec25.ppt lec3.ppt lec4.ppt lec5.ppt lec6...
In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...
programming in computing 10a lec2
EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf
6. **划分风险等级**:根据LEC分值将危险源分为不同的风险等级,如低风险、中风险和高风险。 总的来说,"lec.rar_LEC"提供的工具是进行LEC风险评估的重要助手,它使用MATLAB语言编写,能够客观地计算和分析工作场所...
Lec 10 Collision Detection and Response Quiz Lec 11 Ray Casting and Rendering Lec 12 Ray Casting II Lec 13 Ray Tracing Lec 14 Acceleration Structures for Ray Casting Assignment 3 Lec 15 Shading ...
数字逻辑设计及应用教学英文课件:Lec10-Chap 6.ppt
EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf
麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。
《LEC培训(完整版).pdf》是一份关于逻辑等效检查的详细教程,重点介绍了使用Conformal工具进行逻辑等效验证的方法和技术。Conformal是一款强大的逻辑等效检查工具,广泛应用于芯片设计的验证阶段,确保设计的逻辑...
在Linux系统中,头文件是C语言编程的关键部分,它们允许程序员在源代码(如lec.c)中使用特定的函数和数据类型,而无需在每个文件中包含完整的实现。lec.c很可能是这个LAN Emulation客户端的实现文件,包含实际的...
麻省理工matlab课件-MIT6_094IAP10_lec05.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。
由于提供的文件内容主要是一些网站链接、电子邮箱地址和数字的排列,没有提供实际的HOLLiAS-LEC G3 PLC选型手册的详细内容,我无法直接从中提取相关的知识点。然而,我可以根据HOLLiAS-LEC G3 PLC这个主题,根据一般...
LEC-3210AU8F4型号除了具备6个千兆LAN端口和10个隔离串口外,还提供4个百兆多模光纤接口;LEC-3210AU8B型号和LEC-3210AU8C2型号分别提供1路IRIG-B码对时和2路CAN接口。 LEC-3210在电力自动化系统中具有重要的应用...
Week1—4_Note_Lec1—6.pdf
总结而言,“lec.zip_LEC _点名_点名系统_点名系统C_随机点名”提供的是一款基于C语言实现的随机点名系统,它集成了随机数生成和数据管理技术,为教学环境带来了便捷和公平。通过理解和掌握这样的系统,不仅可以提升...
在"Lec10_review.pptx"中,我们回顾了模拟电路的基础知识,特别是关于信号和放大器的部分。以下是一些关键概念和理论的详细解释: 1. **基本定律**: - **基尔霍夫电流定律(KCL)**:电流守恒原理,规定进入节点...
demo_Lec.m
"Lec 1 SDLC.pptx"这个文件很可能包含了对SDLC和UML的详细介绍,涵盖了以上各个阶段的理论知识和实际应用案例。通过学习这个讲座材料,可以深入理解这两个概念如何在实际项目中协同工作,提升软件开发的效率和质量。...