📄 王大刚--c语言编程宝典--c.htm
字号:
<P> setcolor(getmaxcolor()); <BR> ht =
textheight("W"); <BR>
<P> /* message in default full-screen viewport */
<BR> outtextxy(0, 0, "* <-- (0, 0) in default viewport");
<BR>
<P> /* create a smaller viewport */ <BR>
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); <BR>
<P> /* display some messages */ <BR> outtextxy(0,
0, "* <-- (0, 0) in smaller viewport"); <BR> outtextxy(0,
2*ht, "Press any key to clear viewport:"); <BR>
<P> /* wait for a key */ <BR> getch(); <BR>
<P> /* clear the viewport */ <BR> clearviewport();
<BR>
<P> /* output another message */ <BR> outtextxy(0,
0, "Press any key to quit:"); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: _close, close <BR>功 能: 关闭文件句柄 <BR>用 法: int close(int
handle); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<fcntl.h> <BR>#include <io.h> <BR>
<P>main() <BR>{ <BR> int handle; <BR> char buf[11]
= "0123456789"; <BR>
<P> /* create a file containing 10 bytes */ <BR>
handle = open("NEW.FIL", O_CREAT); <BR> if (handle > -1)
<BR> { <BR> write(handle,
buf, strlen(buf)); <BR>
<P> /* close the file */
<BR> close(handle); <BR> }
<BR> else <BR> {
<BR> printf("Error opening file\n");
<BR> } <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: clock <BR>功 能: 确定处理器时间 <BR>用 法: clock_t clock(void);
<BR>程序例: <BR>
<P>#include <time.h> <BR>#include <stdio.h> <BR>#include
<dos.h> <BR>
<P>int main(void) <BR>{ <BR> clock_t start, end;
<BR> start = clock(); <BR>
<P> delay(2000); <BR>
<P> end = clock(); <BR> printf("The time was:
%f\n", (end - start) / CLK_TCK); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: closegraph <BR>功 能: 关闭图形系统 <BR>用 法: void far
closegraph(void); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int x, y; <BR>
<P> /* initialize graphics mode */ <BR>
initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR>
<P> if (errorcode != grOk) /* an error
<BR> occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> x = getmaxx() / 2; <BR> y = getmaxy() / 2;
<BR>
<P> /* output a message */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
"Press a key to close the graphics system:"); <BR>
<P> /* wait for a key */ <BR> getch(); <BR>
<P> /* closes down the graphics system */ <BR>
closegraph(); <BR>
<P> printf("We're now back in text mode.\n"); <BR>
printf("Press any key to halt:"); <BR> getch();
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: clreol <BR>功 能: 在文本窗口中清除字符到行末 <BR>用 法: void
clreol(void); <BR>程序例: <BR>
<P>#include <conio.h> <BR>
<P>int main(void) <BR>
<P>{ <BR> clrscr(); <BR> cprintf("The function
CLREOL clears all characters from the\r\n"); <BR>
cprintf("cursor position to the end of the line within the\r\n");
<BR> cprintf("current text window, without moving the
cursor.\r\n"); <BR> cprintf("Press any key to continue . .
."); <BR> gotoxy(14, 4); <BR> getch(); <BR>
<P> clreol(); <BR> getch(); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: clrscr <BR>功 能: 清除文本模式窗口 <BR>用 法: void clrscr(void);
<BR>程序例: <BR>
<P>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> int i; <BR>
<P> clrscr(); <BR> for (i = 0; i < 20; i++)
<BR> cprintf("%d\r\n", i); <BR>
cprintf("\r\nPress any key to clear screen"); <BR> getch();
<BR>
<P> clrscr(); <BR> cprintf("The screen has been
cleared!"); <BR> getch(); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: coreleft <BR>功 能: 返回未使用内存的大小 <BR>用 法: unsigned
coreleft(void); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <alloc.h> <BR>
<P>int main(void) <BR>{ <BR> printf("The difference between
the highest allocated block and\n"); <BR> printf("the top of
the heap is: %lu bytes\n", (unsigned long) coreleft()); <BR>
<P> return 0; <BR>} <BR> <BR>
<P>函数名: cos <BR>功 能: 余弦函数 <BR>用 法: double cos(double x);
<BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <math.h> <BR>
<P>int main(void) <BR>{ <BR> double result; <BR>
double x = 0.5; <BR>
<P> result = cos(x); <BR> printf("The cosine of
%lf is %lf\n", x, result); <BR> return 0; <BR>} <BR>
<BR> <BR> <BR>
<P>函数名: cosh <BR>功 能: 双曲余弦函数 <BR>用 法: dluble cosh(double x);
<BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <math.h> <BR>
<P>int main(void) <BR>{ <BR> double result; <BR>
double x = 0.5; <BR>
<P> result = cosh(x); <BR> printf("The hyperboic
cosine of %lf is %lf\n", x, result); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: country <BR>功 能: 返回与国家有关的信息 <BR>用 法: struct COUNTRY
*country(int countrycode, struct country *country); <BR>程序例: <BR>
<P>#include <dos.h> <BR>#include <stdio.h> <BR>
<P>#define USA 0 <BR>
<P>int main(void) <BR>{ <BR> struct COUNTRY country_info; <BR>
<P> country(USA, &country_info); <BR>
printf("The currency symbol for the USA is: %s\n",
<BR>
country_info.co_curr); <BR> return 0; <BR>} <BR>
<BR> <BR> <BR>
<P>函数名: cprintf <BR>功 能: 送格式化输出至屏幕 <BR>用 法: int cprintf(const
char *format[, argument, ...]); <BR>程序例: <BR>
<P>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* clear the screen */
<BR> clrscr(); <BR>
<P> /* create a text window */ <BR> window(10, 10,
80, 25); <BR>
<P> /* output some text in the window */ <BR>
cprintf("Hello world\r\n"); <BR>
<P> /* wait for a key */ <BR> getch();
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: cputs <BR>功 能: 写字符到屏幕 <BR>用 法: void cputs(const char
*string); <BR>程序例: <BR>
<P>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* clear the screen */
<BR> clrscr(); <BR>
<P> /* create a text window */ <BR> window(10, 10,
80, 25); <BR>
<P> /* output some text in the window */ <BR>
cputs("This is within the window\r\n"); <BR>
<P> /* wait for a key */ <BR> getch();
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: _creat creat <BR>功 能: 创建一个新文件或重写一个已存在的文件 <BR>用
法: int creat (const char *filename, int permiss); <BR>程序例: <BR>
<P>#include <sys\stat.h> <BR>#include <string.h> <BR>#include
<fcntl.h> <BR>#include <io.h> <BR>
<P>int main(void) <BR>{ <BR> int handle; <BR> char
buf[11] = "0123456789"; <BR>
<P> /* change the default file mode from text to binary */
<BR> _fmode = O_BINARY; <BR>
<P> /* create a binary file for reading and writing */
<BR> handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); <BR>
<P> /* write 10 bytes to the file */ <BR>
write(handle, buf, strlen(buf)); <BR>
<P> /* close the file */ <BR> close(handle);
<BR> return 0; <BR>} <BR> <BR>
<P>函数名: creatnew <BR>功 能: 创建一个新文件 <BR>用 法: int creatnew(const
char *filename, int attrib); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<errno.h> <BR>#include <dos.h> <BR>#include <io.h> <BR>
<P>int main(void) <BR>{ <BR> int handle; <BR> char
buf[11] = "0123456789"; <BR>
<P> /* attempt to create a file that doesn't already exist */
<BR> handle = creatnew("DUMMY.FIL", 0); <BR>
<P> if (handle == -1) <BR>
printf("DUMMY.FIL already exists.\n"); <BR> else
<BR> { <BR> printf("DUMMY.FIL
successfully created.\n"); <BR>
write(handle, buf, strlen(buf)); <BR>
close(handle); <BR> } <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: creattemp <BR>功 能: 创建一个新文件或重写一个已存在的文件 <BR>用 法: int
creattemp(const char *filename, int attrib); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<io.h> <BR>
<P>int main(void) <BR>{ <BR> int handle; <BR> char
pathname[128]; <BR>
<P> strcpy(pathname, "\\"); <BR>
<P> /* create a unique file in the root directory */
<BR> handle = creattemp(pathname, 0); <BR>
<P> printf("%s was the unique file created.\n", pathname);
<BR> close(handle); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: cscanf <BR>功 能: 从控制台执行格式化输入 <BR>用 法: int cscanf(char
*format[,argument, ...]); <BR>程序例: <BR>
<P>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> char string[80]; <BR>
<P> /* clear the screen */ <BR> clrscr(); <BR>
<P> /* Prompt the user for input */ <BR>
cprintf("Enter a string with no spaces:"); <BR>
<P> /* read the input */ <BR> cscanf("%s",
string); <BR>
<P> /* display what was read */ <BR>
cprintf("\r\nThe string entered is: %s", string); <BR> return
0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: ctime <BR>功 能: 把日期和时间转换为字符串 <BR>用 法: char *ctime(const
time_t *time); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <time.h> <BR>
<P>int main(void) <BR>{ <BR> time_t t; <BR>
<P> time(&t); <BR> printf("Today's date and
time: %s\n", ctime(&t)); <BR> return 0; <BR>} <BR>
<BR> <BR> <BR>
<P>函数名: ctrlbrk <BR>功 能: 设置Ctrl-Break处理程序 <BR>用 法: void
ctrlbrk(*fptr)(void); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <dos.h> <BR>
<P>#define ABORT 0 <BR>
<P>int c_break(void) <BR>{ <BR> printf("Control-Break
pressed. Program aborting ...\n"); <BR> return (ABORT);
<BR>} <BR>
<P>int main(void) <BR>{ <BR> ctrlbrk(c_break);
<BR> for(;;) <BR> {
<BR> printf("Looping... Press
<Ctrl-Break> to quit:\n"); <BR> } <BR>
return 0; <BR>} <BR> <BR>
<HR width="94%" color=#ee9b73 SIZE=1>
</TD>
<TD class=tt3 vAlign=bottom width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/023.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/021.htm">前一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A
href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -