C/C++——小编谈C语言函数那些事(13)
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. malloc函数
malloc函数的功能是内存分配函数,其用法为:void *malloc(unsigned size);程序实例如下:
#include <stdio.h>
#include <string.h>
#include <alloc.h>
#include <process.h>
int main(void)
{
char *str;
if ((str = malloc(10)) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1); /* terminate program if out of memory */
}
strcpy(str, "Hello");
printf("String is %s\n", str);
free(str);
return 0;
}
2. memchr函数
memchr函数的功能是在数组的前n个字节中搜索字符,其用法为:void *memchr(void *s, char ch, unsigned n); 程序实例代码如下:
#include <string.h>
#include <stdio.h>
int main(void)
{
char str[17];
char *ptr;
strcpy(str, "This is a string");
ptr = memchr(str, 'r', strlen(str));
if (ptr)
printf("The character 'r' is at position: %d\n", ptr - str);
else
printf("The character was not found\n");
return 0;
}
3. memicmp函数
memicmp函数的功能是比较两个串s1和s2的前n个字节, 忽略大小写,其用法为:int memicmp(void *s1, void *s2, unsigned n);程序实例代码如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *buf1 = "ABCDE123";
char *buf2 = "abcde456";
int stat;
stat = memicmp(buf1, buf2, 5);
printf("The strings to position 5 are ");
if (stat)
printf("not ");
&
相关文档:
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. kbhit函数
kbhit函数是检查当前按下的键,其用法为:int kbhit(void);程序例子如下:
#include <conio.h>
int main(void)
{
c ......
日期:2009-11-21 10:54:22
本节主要参考:
曹乐的《在Emacs下用C/C++编程》
王纯业的《Emacs 一个强大的平台》
emacswiki.org
emcas难学易用,可扩展性强。有人把她当作信仰,有人认为他是魔鬼!学习首先记住基本的键盘快捷键,学会常用插件, ......
神乎其技,惟C程序,功到自成,十大建议!
1、汝应频繁催动lint工具,据其语法声明修习内力,此事皆因lint之思虑决断实远在君上。
2、不可依随NULL指针,如若不然,混沌痴颠必俟君于彼岸。
3、纵有天赋大智慧,知晓其事无碍,汝亦当尽数强制挪移函数参数为原型所期之数据类型,以免一 ......
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. ldexp函数
ldexp函数的功能是计算value*2的幂,其用法为:double ldexp(double value, int exp);程序实例如下:
......