格式system(“指令語句”); 需要在代碼中加上#include<stdlib.h>才可用。
一、system
(1)、system("pause")
作用:暫停
示例代碼1:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
輸出為:
加上 system("pause")後,輸出為:
然後點選執行檔案框,摁兩下Enter鍵:
以上兩者的差別:摁了兩下回車,才出現執行檔案框最下面的字型!,但是除了Enter鍵,其他的鍵隻需摁一次即可!
增加system("pause")後;除Enter鍵外,隻需摁任一鍵即可:
(2)、COLOR [attr]
attr 指定控制台輸出的顔色屬性。
顔色屬性由兩個十六進制數字指定 --
第一個:對應于背景,
第二個:對應于前景。
例:color 5A
設定預設的控制台前景和背景顔色。
5 :紫色背景色
A :淡綠色的字型顔色
每個數字可以為以下任何值:
0 = 黑色 8 = 灰色
1 = 藍色 9 = 淡藍色
2 = 綠色 A = 淡綠色
3 = 淺綠色 B = 淡淺綠色
4 = 紅色 C = 淡紅色
5 = 紫色 D = 淡紫色
6 = 黃色 E = 淡黃色
7 = 白色 F = 亮白色
#include<stdio.h>
#include <stdlib.h>
int main()
{
while (1)
{
system("color 4A");
printf("\nHello World\n");
}
return 0;
}
運作結果:
(3)、system("cls");
用于清楚螢幕内容;
示例代碼:
#include <stdio.h>
#include <windows.h>
void gotoxy(int x, int y) {
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 擷取标準輸出裝置句柄
SetConsoleCursorPosition(hOut, pos);//兩個參數分别是指定哪個窗體,具體位置
}
int main() {
int x, y;
gotoxy(2, 2);
printf("hello,world!\n");
system("CLS");
gotoxy(10, 10);
printf("Good good study!\n");
system("pause");
return 0;
}
運作結果:
二、exit和return
exit(0)與exit(1)、return差別
C語言中exit(0)與exit(1)有什麼差別(詳解)
【C】C語言中的_exit()與exit()
https://wenku.baidu.com/view/4c67e89231126edb6e1a100a.html
https://wenku.baidu.com/view/ffe5fb89647d27284a73510a.html