C语言中system("pause")是什么作用和意思
发布时间:2025-05-14 06:08:45 发布人:远客网络
一、C语言中system("pause")是什么作用和意思
1、system("pause")意思就是让程序暂停一下,然后按任意键继续,初学的时候最多见于程序的末尾处,用于看运行结果,避免程序一闪而过。相同的我们还可以用getchar(),避免程序运行完直接结束而看不到运行结果。
2、执行到 system("pause");按任意键继续
3、用法: int system(char*command);
4、system函数已经被收录在标准c库中,可以直接调用。
二、C语言的system函数
windows操作系统下system()函数详解(主要是在C语言中的应用)
用法: int system(char*command);
system函数已经被收录在标准c库中,可以直接调用
printf("About to spawn and run a DOS command\n");
又如:system("pause")可以实现冻结屏幕,便于观察程序的执行结果;system("CLS")可以实现清屏操作。而调用color函数可以改变控制台的前景色和背景,具体参数在下面说明。
例如,用 system("color 0A");其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色
(注意:Microsoft Visual C++6.0支持system)
看了下面实例,相信你会对学到更多system在C程序设计中的应用。
C语言调用DOS命令实现定时关机:
printf("╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");
printf("╔═══╧╧C语言关机程序╧╧═══╗\n");
printf("║※1.实现10分钟内的定时关闭计算机║\n");
printf("║※2.立即关闭计算机║\n");
printf("║※3.注销计算机║\n");
printf("║※0.退出系统║\n");
printf("╚═══════════════════╝\n");
system("title C语言关机程序");//设置cmd窗口标题
system("mode con cols=48 lines=25");//窗口宽度高度
case 1:printf("您想在多少秒后自动关闭计算机?(0~600)\n");scanf("%s",t);system(strcat(cmd,t));break;
case 2:system("shutdown-p");break;
case 3:system("shutdown-l");break;
default:printf("Error!\n");
用C语言删除文件,例如文件的位置是d:\123.txt
用system()函数执行windows命令。
system("del d:\\123.txt");
三、在C语言中,程序有一个是system("CLS");时什么意思
当你编写的程序有输出的时候,如果要进行多次调试,屏幕上会显示很多次的输出的结果,看上去非常的复杂非常的乱。那么我们就可以在程序中的输出语句之前加上“system("CLS");”,当我们用上这条语句之后。
这样每次程序运行的时候都会将上一次运行输出的内容给清除掉,屏幕上只显示本次输出的结果。这样看起来就非常的简洁。
在VC环境下有两种办法实现清屏:
system("cls");这种办法的缺点是程序额外运行系统程序执行清屏操作,延长了程序执行时间。
/* Standard error macro for reporting API errors*/
#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error%d from%s \
on line%d\n", __FILE__, GetLastError(), api, __LINE__);}
COORD coordScreen={ 0, 0};/* here's where we'll home the
CONSOLE_SCREEN_BUFFER_INFO csbi;/* to get buffer info*/
DWORD dwConSize;/* number of character cells in
/* get the number of character cells in the current buffer*/
bSuccess= GetConsoleScreenBufferInfo( hConsole,&csbi);
PERR( bSuccess,"GetConsoleScreenBufferInfo");
dwConSize= csbi.dwSize.X* csbi.dwSize.Y;
/* fill the entire screen with blanks*/
bSuccess= FillConsoleOutputCharacter( hConsole,(TCHAR)'',
dwConSize, coordScreen,&cCharsWritten);
PERR( bSuccess,"FillConsoleOutputCharacter");
/* get the current text attribute*/
bSuccess= GetConsoleScreenBufferInfo( hConsole,&csbi);
PERR( bSuccess,"ConsoleScreenBufferInfo");
/* now set the buffer's attributes accordingly*/
bSuccess= FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
dwConSize, coordScreen,&cCharsWritten);
PERR( bSuccess,"FillConsoleOutputAttribute");
bSuccess=SetConsoleCursorPosition( hConsole, coordScreen);
PERR( bSuccess,"SetConsoleCursorPosition");
参考资料来源:百度百科-system("cls")