当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(1)
好久没有更新C语言的文章了,因为小编最近没有研究C语言,所以嘛也没什么灵感,今天跟朋友讨论了几个C语言的函数问题,就想到要总结一篇这样的文章,希望对大家有所帮助吧。言归正传,以下是为大家总结的C语言函数的使用方法。
1.Allocmem函数
Allocmem函数是分配DOS存储段,其用法为:int allocmem(unsigned size, unsigned *seg);
为了让大家更能明白他的准确用法下面举个程序例子:
#include <dos.h>
#include <alloc.h>
#include <stdio.h>
int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, &segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",
stat);
return 0;
}
2. asctime函数
Asctime函数是转换日期和时间为ASCII码,其用法为:char *asctime(const struct tm *tblock); 程序例子如下:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)
{
struct tm t;
char str[80];
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(&t));
printf("%s\n", str);
return 0;
}
3. atof函数
Atof函数是把字符串转换成浮点数,其用法为:double atof(const char *nptr);,程序例子如下:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
由于篇幅有限,小编就先和大家分享这几个函数,以后小编会不断推出类似的文章跟大家分享。