C语言函数大全-- s 开头的函数(1)
C语言函数大全
本篇介绍C语言函数大全-- s 开头的函数(1)
1. sbrk
1.1 函数说明
函数声明 | 函数功能 |
---|---|
void *sbrk(intptr_t increment);
|
它是一个 Unix 系统的函数,用于调整程序的堆空间。
|
参数:
- increment : 增加的堆空间的大小
返回值:
- 如果调用成功,返回值即为增加空间前的原始堆顶指针;
- 如果出错,则返回 (void *)-1
1.2 演示示例
#include <unistd.h>
#include <stdio.h>int main() {void *mem = sbrk(64);if (mem == (void *)-1) {perror("sbrk");return 1;}printf("Allocated 64 bytes at address %p\n", mem);return 0;
}
在上述示例中,
- 首先调用
sbrk()
函数将堆顶位置向上移动 64 字节; - 然后将返回的指针保存在变量
mem
中;如果 sbrk 调用失败(即返回值等于(void *)-1)
,则程序通过perror()
函数输出错误信息并返回1
; - 最后,程序打印出分配的内存地址,并返回
0
表示程序成功运行。
2. scalb,scalbf,scalbl
2.1 函数说明
函数声明 | 函数功能 |
---|---|
double scalb(double x, double n);
|
计算 x 乘以 2 的 n 次幂(double) |
float scalbf(float x, float n);
|
计算 x 乘以 2 的 n 次幂(float) |
long double scalbl(long double x, double n);
|
计算 x 乘以 2 的 n 次幂(long double) |
注意:如果
n
超过了可表示的范围,或者结果溢出,则函数可能返回正无穷大、负无穷大或NaN
。
2.2 演示示例
#include <stdio.h>
#include <math.h>int main() {double x = 3.14159265359;double y = scalb(x, 10);printf("Before: %lf\n", x);printf("After: %lf\n", y);return 0;
}
3. scalbln,scalblnf,scalblnl
3.1 函数说明
函数声明 | 函数功能 |
---|---|
double scalbln(double x, long int n);
|
计算 x 乘以 2 的指定长整数 n 次幂(double) |
float scalblnf(float x, long int n);
|
计算 x 乘以 2 的指定长整数 n 次幂(float) |
long double scalblnl(long double x, long int n);
|
计算 x 乘以 2 的指定长整数 n 次幂(long double) |
参数:
3.2 演示示例
#include <stdio.h>
#include <math.h>int main() {double x = 1.5;float y = 2.25f;long double z = 3.75l;long int n = 3;// 将x乘以2的3次幂double result = scalbln(x, n);printf("scalbln(%lf, %ld) = %lf\n", x, n, result);float resultf = scalblnf(y, n);printf("scalblnf(%f, %ld) = %f\n", y, n, resultf);long double resultL = scalblnl(z, n);printf("scalblnl(%Lf, %ld) = %Lf", z, n, resultL);return 0;
}
3.3 运行结果
4. scalbn,scalbnf,scalbnl
4.1 函数说明
函数声明 | 函数功能 |
---|---|
double scalbn(double x, int n);
|
计算 x 乘以 2 的指定整数 n 次幂(double) |
float scalbnf(float x, int n);
|
计算 x 乘以 2 的指定整数 n 次幂(float) |
long double scalbnl(long double x, int n);
|
计算 x 乘以 2 的指定整数 n 次幂(long double) |
4.2 演示示例
#include <stdio.h>
#include <math.h>int main()
{double x = 3.1415926;float y = 3.1415926f;long double z = 3.1415926L;int n = 5;printf("scalbn(%lf, %d) = %.20lf\n", x, n, scalbn(x, n));printf("scalbnf(%f, %d) = %.20f\n", y, n, scalbnf(y, n));printf("scalbnl(%Lf, %d) = %.20Lf\n", z, n, scalbnl(z, n));return 0;
}
4.3 运行结果
5. scanf
5.1 函数说明
函数声明 | 函数功能 |
---|---|
int scanf(const char *format, ...);
|
从标准输入中读取指定格式的数据 |
详细内容 可参考 《scanf》
5.2 演示示例
#include <stdio.h>
#include <math.h>int main()
{char name[20];scanf("%s", name);printf("Welcome, %s", name);return 0;
}
在上述示例中,实现了从标准输入中读取一个字符串,并输出欢迎消息。
- 我们首先定义一个名为
name
的字符数组,长度为 20; - 然后使用
scanf()
函数从标准输入中读取一个字符串,存储到name
数组中; - 最后使用
printf()
函数输出欢迎消息,其中%s
表示字符串格式化符号,会被name
所代替。
5.3 运行结果
6. sector
6.1 函数说明
函数声明 | 函数功能 |
---|---|
void sector( int x, int y, int stangle, int endangle, int xradius, int yradius );
|
画并填充椭圆扇区(Windows BGI) |
参数:
- x : 扇形的圆心坐标的 x 轴
- y : 扇形的圆心坐标的 y 轴
- stangle : 起始角度(以度数为单位)
- endangle : 结束角度(以度数为单位)
- xradius : 横向半径
- yradius : 纵向半径
6.2 演示示例
#include <graphics.h>int main(void)
{int gdriver = DETECT, gmode;int midx, midy, i;int stangle = 45, endangle = 135;int xrad = 100, yrad = 50;initgraph(&gdriver, &gmode, "");midx = getmaxx() / 2;midy = getmaxy() / 2;for (i=EMPTY_FILL; i<USER_FILL; i++){setfillstyle(i, getmaxcolor());sector(midx, midy, stangle, endangle, xrad, yrad);getch();}closegraph();return 0;
}
6.3 运行结果
7. segread
7.1 函数说明
函数声明 | 函数功能 |
---|---|
void segread(struct SREGS *sregs);
|
它是一个 DOS 函数,用于从内存段(segment )中读取内容。
|
7.2 演示示例
#include <dos.h>
#include <stdio.h>int main() {struct SREGS regs;segread(®s); // 读取段寄存器的值printf("Data segment: %04X\n", regs.ds);printf("Code segment: %04X\n", regs.cs);printf("Stack segment: %04X\n", regs.ss);return 0;
}
上述的这个示例程序,使用了 <dos.h>
头文件中的 segread()
函数和 struct SREGS
结构体,并打印出数据段(DS
)、代码段(CS
)和堆栈段(SS
)的值。
8. setactivepage
8.1 函数说明
函数声明 | 函数功能 |
---|---|
void setactivepage(int pagenum);
|
用于切换当前显示的页面 |
8.2 演示示例
#include <graphics.h>int main(void)
{int gdriver = EGA, gmode = EGAHI;int x, y, ht;initgraph(&gdriver, &gmode, "");x = getmaxx() / 2;y = getmaxy() / 2;ht = textheight("W");setactivepage(1);line(0, 0, getmaxx(), getmaxy());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(x, y, "This is page #1:");outtextxy(x, y+ht, "Press any key to halt:");setactivepage(0);outtextxy(x, y, "This is page #0.");outtextxy(x, y+ht, "Press any key to view page #1:");getch();setvisualpage(1);getch();closegraph();return 0;
}
上述示例将在屏幕上绘制两个页面,并允许用户通过按任意键查看第二个页面。
8.3 运行结果
9. setallpallette
9.1 函数说明
函数声明 | 函数功能 |
---|---|
void setallpalette(struct palettetype *palette);
|
用于设置图形界面的整个调色板 |
参数:
- palette : 调色版结构体
9.2 演示示例
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>int main(void)
{int gdriver = DETECT, gmode;struct palettetype pal, copypal;int color, ht;int y = 10;char msg[80];initgraph(&gdriver, &gmode, "");ht = 2 * textheight("W");getpalette(&pal);for (color = 1; color <= MAXCOLORS; color++){sprintf(msg, "old colors[%d]: %d", color, pal.colors[color]);outtextxy(1, y, msg);y += ht;}for (color = 1; color <= MAXCOLORS; color++){setpalette(color, BLACK);}getpalette(©pal);getch();y = 10;for (color = 1; color <= MAXCOLORS; color++){sprintf(msg, "new colors[%d]: %d", color, copypal.colors[color]);outtextxy(150, y, msg);y += ht;}getch();// 重置调色板setallpalette(&pal);getpalette(&pal);y = 10;for (color = 1; color <= MAXCOLORS; color++){sprintf(msg, "old colors[%d]: %d", color, pal.colors[color]);outtextxy(300, y, msg);y += ht;}getch();closegraph();return 0;
}
9.3 运行结果
10. setaspectratio
10.1 函数说明
函数声明 | 函数功能 |
---|---|
void setaspectratio( int xasp, int yasp );
|
设置图形纵横比 |
参数:
- xasp : 水平缩放比例
- yasp : 垂直缩放比例
10.2 演示示例
#include <graphics.h>int main()
{int gd = DETECT, gm;initgraph(&gd, &gm, "");// 设置水平和垂直缩放比例为 2:1setaspectratio(2, 1);// 在图形窗口中绘制一个正方形rectangle(100, 100, 200, 200);getch();closegraph();return 0;
}
注意: 因为 BGI 图形库在 Windows 上的实现是基于 Win32 API 的简单封装,并且没有提供缩放功能,所以上述程序在 Windows BGI 下不支持缩放功能,也就是 setaspectratio 没有效果。
11. setbkcolor
11.1 函数说明
函数声明 | 函数功能 |
---|---|
void setbkcolor(int color);
|
用于设置当前绘图窗口的背景色 |
参数:
11.2 演示示例
#include <graphics.h>int main()
{int gd = DETECT, gm;initgraph(&gd, &gm, "");// 设置背景色为蓝色setbkcolor(BLUE);// 清除先前绘制的图形cleardevice();// 在图形窗口中绘制一条线段line(100, 100, 200, 200);getch();closegraph();return 0;
}
注意: 在修改背景颜色后,需要使用
cleardevice()
函数清除先前绘制的图形并更新背景颜色,不然背景颜色的修改不会生效。
11.3 运行结果
12. setbuf
12.1 函数说明
函数声明 | 函数功能 |
---|---|
void setbuf(FILE *stream, char *buffer);
|
用于设置标准输入流、标准输出流或标准错误流的缓冲方式 |
参数:
12.2 演示示例
#include <stdio.h>int main()
{FILE *fp;// 打开文件,并设置为不带缓冲fp = fopen("test.txt","w");setbuf(fp, NULL);// 写入数据到文件中fprintf(fp, "This is a line of text.");// 关闭文件fclose(fp);return 0;
}
在上述这个示例程序中,我们打开了一个名为 test.txt
的文件,并使用 setbuf()
函数将其设置为不带缓冲。然后,我们向文件中写入一行文本,关闭文件并退出程序。
由于我们已经将文件设置为不带缓冲,因此写入的数据将直接写入磁盘,而不会在内存中留下缓冲区。
12.3 运行结果
13. setcolor
13.1 函数说明
函数声明 | 函数功能 |
---|---|
void setcolor(int color);
|
参数:
- color : 要设置的颜色,预定义颜色有如下:
BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
13.2 演示示
#include <graphics.h>int main()
{int gd = DETECT, gm;initgraph(&gd, &gm, "");setbkcolor(WHITE);cleardevice();// 设置绘画颜色为红色setcolor(RED);// 在图形窗口中绘制一条直线line(100, 100, 200, 200);getch();closegraph();return 0;
}
13.3 运行结果
14. setfillpattern
14.1 函数说明
函数声明 | 函数功能 |
---|---|
void setfillpattern(char *upattern, int color);
|
用于设置当前绘图窗口的填充图案 |
参数:
- upattern : 填充图案
- color : 填充颜色
在调用 setfillpattern()
函数之前,需要先定义一个名为 upattern
的字符数组,并将其用作填充图案。这个图案通常使用一个 8
字节的字节数组表示,每个元素包含一个 8
比特位的掩码,用于表示该位置是否应该被绘制斜线或其他样式。
14.2 演示示例
#include <graphics.h>int main(void)
{int gdriver = DETECT, gmode;int maxx, maxy;char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};// 初始化图形模式initgraph(&gdriver, &gmode, "");// 获取屏幕的最大 X 和 Y 坐标maxx = getmaxx();maxy = getmaxy();// 设置当前颜色为最大颜色值setcolor(getmaxcolor());// 设置填充图案setfillpattern(pattern, getmaxcolor());// 用填充图案填满整个窗口bar(0, 0, maxx, maxy);// 等待用户按下任意键后关闭窗口getch();closegraph();return 0;
}
上述程序使用了 Windows BGI 图形库来创建一个空白的图形窗口,并在其中填充一种斜线筛子图案。
何为斜线筛子图案,参考如下程序打印:
#include <stdio.h>int main() {char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};for (int i = 0; i < 8; i++) {for (int j = 0; j < 8; j++) {if (pattern[i] & (1 << (7 - j))) {printf("*");} else {printf(" ");}}printf("\n");}return 0;
}
14.3 运行结果
15. setfillstyle
15.1 函数说明
函数声明 | 函数功能 |
---|---|
void setfillstyle(int pattern, int color);
|
用于设置当前绘图窗口的填充样式,即用什么颜色或图案来填充绘制的图形 |
参数:
- pattern : 填充样式
- color : 填充颜色
如下是支持的填充样式:
EMPTY_FILL, SOLID_FILL, LINE_FILL, LTSLASH_FILL, SLASH_FILL, BKSLASH_FILL, LTBKSLASH_FILL, HATCH_FILL, XHATCH_FILL, INTERLEAVE_FILL, WIDE_DOT_FILL, CLOSE_DOT_FILL, USER_FILL
setfillstyle()
函数还支持使用用户自定义的填充样式。如果要使用用户自定义的填充样式,需要调用 setuserchars()
函数来设置填充字形,然后将填充样式参数设置为 USER_FILL
。
15.2 演示示例
#include <graphics.h>int main()
{int gd = DETECT, gm;initgraph(&gd, &gm, "");// 设置填充样式为斜线筛子图案,颜色为红色setfillstyle(SLASH_FILL, RED);// 在图形窗口中填充一个矩形rectangle(100, 100, 200, 200);floodfill(150, 150, getmaxcolor());getch();closegraph();return 0;
}
在上述的示例程序中,
- 我们首先使用
setfillstyle()
函数将填充样式设置为斜线筛子图案(即SLASH_FILL
样式),颜色设置为红色。 - 然后,我们使用
rectangle()
函数在图形窗口中绘制一个矩形,并使用floodfill()
函数将该矩形填充为红色的斜线筛子图案。 - 最后按任意键退出图形模式。
15.3 运行结果
16. setgraphmode
16.1 函数说明
函数声明 | 函数功能 |
---|---|
void setgraphmode(int mode);
|
设置当前的图形模式 |
16.2 演示示例
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>int main(void)
{int gdriver = DETECT, gmode;int x, y;initgraph(&gdriver, &gmode, "");x = getmaxx() / 2;y = getmaxy() / 2;settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(x, y, "Press any key to exit graphics:");getch();// 将图形模式恢复到文本模式restorecrtmode();printf("We're now in text mode.\n");printf("Press any key to return to graphics mode:");getch();// 返回图形模式setgraphmode(getgraphmode());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(x, y, "We're back in graphics mode.");outtextxy(x, y+textheight("W"), "Press any key to halt:");getch();closegraph();return 0;
}
16.3 运行结果
17. setjmp
17.1 函数说明
函数声明 | 函数功能 |
---|---|
int setjmp(jmp_buf env);
|
用于在程序执行过程中设置跳转点,并将当前程序状态保存到一个缓冲区中。当程序需要从该跳转点继续执行时,可以使用 longjmp() 函数恢复之前保存的程序状态并返回到该跳转点。 |
参数:
- env : 要保存程序状态的缓冲区,通常声明为
jmp_buf
类型
返回值:
setjmp()
函数返回0
,除非它是从longjmp()
函数调用返回的,这种情况下,它返回非零值。
17.2 演示示例
#include <stdio.h>
#include <setjmp.h>jmp_buf buf;void foo()
{printf("foo() before longjmp()\n");longjmp(buf, 1);printf("foo() after longjmp()\n");
}int main()
{int ret = setjmp(buf);if (ret == 0) {printf("main() before foo()\n");foo();} else {printf("main() after longjmp()\n");}return 0;
}
在上述的示例程序中,
- 我们首先定义了一个名为
buf
的jmp_buf
类型的缓冲区,并将其作为参数传递给setjmp()
函数,当前程序状态将保存到 buf 缓冲区中,并返回一个整形,用ret
变量存储;此时ret
的值为 0,则说明是第一次调用setjmp()
函数,主函数内打印输出”main() before foo()“
- 然后我们调用
foo()
函数,打印输出”foo() before longjmp()“
; - 接着调用
longjmp()
函数,将会返回到setjmp()
函数调用的位置,并传递了一个值1
。因为longjmp()
调用一定会返回setjmp()
函数调用的位置,因此foo()
函数的后面一行代码永远不会被执行。 - 最后
setjmp()
函数返回ret
的值为 1,则说明是从longjmp()
函数中返回的,主函数内打印输出”main() after longjmp()“
。
17.3 运行结果
参考
- [API Reference Document]
C语言函数大全-- s 开头的函数(1)相关推荐
- C语言函数大全-- m 开头的函数(1)
C语言函数大全 本篇介绍C语言函数大全-- m 开头的函数 1. malloc 1.1 函数说明 函数声明 函数功能 void *malloc(size_t size); 用于动态分配内存 参数: s ...
- C语言函数大全-- q 开头的函数
C语言函数大全 本篇介绍C语言函数大全-- q 开头的函数 1. qsort 1.1 函数说明 函数声明 函数功能 void qsort(void *base, size_t nmemb, size_ ...
- C语言函数大全-- i 开头的函数
C语言函数大全 本篇介绍C语言函数大全– i 开头的函数 1. imagesize 1.1 函数说明 函数声明 函数功能 unsigned imagesize(int left, int top, i ...
- C语言函数大全--d开头的函数
C语言函数大全 本篇介绍C语言函数大全–d开头的函数 1. detectgraph 1.1 函数说明 函数声明 函数功能 void detectgraph(int *graphdriver, int ...
- C语言函数大全-- n 开头的函数
C语言函数大全 本篇介绍C语言函数大全-- n 开头的函数 1. nan 1.1 函数说明 函数声明 函数功能 double nan(const char *tagp); 用于返回一个表示 NaN(非 ...
- C语言函数大全-- s 开头的函数(3)
C语言函数大全 本篇介绍C语言函数大全-- s 开头的函数(3) 1. sleep 1.1 函数说明 函数声明 函数功能 unsigned int sleep(unsigned int seconds ...
- C语言函数大全-- r 开头的函数
C语言函数大全 本篇介绍C语言函数大全-- r 开头的函数 1. raise 1.1 函数说明 函数声明 函数功能 int raise(int sig); 用于向当前进程发送指定的信号. 参数: si ...
- C语言函数大全--h开头的函数
C语言函数大全 本篇介绍C语言函数大全–h开头的函数或宏 1. hypot,hypotf,hypotl 1.1 函数说明 函数声明 函数功能 double hypot(double x, double ...
- C语言函数大全-- l 开头的函数
C语言函数大全 本篇介绍C语言函数大全-- l 开头的函数 1. labs,llabs 1.1 函数说明 函数声明 函数功能 long labs(long n); 计算长整型的绝对值 long lon ...
最新文章
- 开源模式反击之后,白嫖服务商竟然大叫“你不讲武德!”
- Git pull[push] 不用每次输入用户名和密码
- script和href
- Intel Realsense C/C++ 转 python (1)rs-hello-realsense 获取摄像头正中心对应的深度数据 get_distance()
- 蜂鸣器发出7种音阶c语言程序_C语言编程新思路
- java-信息安全(一)-BASE64,MD5,SHA,HMAC,RIPEMD算法
- 分享9个实用的电脑维修技巧,赶紧收藏吧!
- linux webapi测试,Webapi管理和性能测试工具WebBenchmark
- 【工作感悟】全网最经典26道Spring面试题总结
- 【转载保存】在python中如何用word2vec来计算句子的相似度
- 基于java+swing+mysql+JFeeChart的企业人力资源管理系统(1)
- 机器学习(四)高斯混合模型
- atomic、锁、多线程
- 湖南超级计算机研学,以超算之速,跨越弦歌千年 | 建宁实验中学中考励志研学...
- 如何区分电压串联负反馈电路和电流串联负反馈电路
- 前端面试总结2018.07
- python中的myql的execute()
- xcode证书及打包
- 计算机网络自顶向下方法华为路由器配置OSPFv3路由协议实现端到端的通信
- 2021湖南涟源高考成绩查询,2021娄底市地区高考成绩排名查询,娄底市高考各高中成绩喜报榜单...
热门文章
- java实现智能拼图,JAVA实现拼图游戏
- 关于几个图像质量评价参数的计算总结
- pymysql mysql 1064_python使用pymysql操作MySQL错误代码1054和1064处理
- Python:岱宗夫如何?齐鲁青未了.
- ansible部署和基本的操作
- 龙珠激斗获取服务器信息中,龙珠激斗战力220000贝吉塔快速获得途径攻略汇总详解...
- ARM态度暧昧,愤怒的华为或投入Risc-V怀抱
- 云平台 造就智能家居
- UE4入门学习4:C++编程介绍
- onecore\com\combase\dcomrem\channelb.cxx(6563)\combase.dll!76A59D9F: (caller: 75A95B54) ReturnHr(1)