1. c語言中怎樣用代碼結束一個程序
結束自身,用exit(0);
結束其它的
你需要查找到該程序的進程名後者進程號
然後用system調用系統的taskkill
具體命令語法,在命令行輸入taskkill /? 或者網路。
2. C語言如何讓程序一直不斷運行直到按了某個鍵以後停止,代碼怎麼寫
可以參考下面的代碼:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
main( )
{
int p;
while( ! _kbhit() ) {
// run progs
_cputs( "Please hit me ! " );
Sleep(500);
}
return 0;
}
(2)c語言結束程序的代碼怎麼寫擴展閱讀:
kbhit()是一個C和C++函數,用於非阻塞地響應鍵盤輸入事件。
函數名:kbhit()
功能及返回值: 檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0。
用 法:int kbhit(void);
C++語言包含頭文件: include <conio.h>。
C語言不需包含額外頭文件。
在VC++6.0下為_kbhit()
功能及返回值同上。
3. 程序的終止代碼有哪些
結束當前函數用return
如果你想結束當前程序的代碼,只要在主函數里return就可以了
還有一個辦法就是exit(0),這個是系統函數。
更多追問追答
追問
能具體說一下怎麼用嗎,之前用過,運行之後程序沒有關閉。
追答
/*總結一下,結束自己的方法:1、在main里return2、調用void exit(int status)函數3、安裝信號,來結束自己//這個新人還是不用的好,其實也不能,只要你安裝一個信號,然後拋出信號就可以了。4、調用系統函數system()來kill自己的進程。*/ /*下面是我寫的類子,main函數里最後是"Not end",如果沒有跑出這個文言,說明函數中間終端了。我只寫了return和status,信號,這個你可以自己看一下這方面的資料。*/ #include <iostream>#include <stdlib.h> #define true 1 using namespace std; int main (int argc, char** argv) { cout << "Sleep 1s ..." << endl; sleep(1); cout << "Exit(0) work ..." << endl; // return true; exit(0); //void exit(int status);這個是exit的函數,返回狀態,int類型,該函數可以/*If one of these functions does not return (e.g., it calls _exit(2), or kills itself with a signal)*///這個為我摘要的該函數說明,大體意思就是說如果他沒有return,那麼就使用信號來kill自己。 cout << "Not end!" << endl;}
4. C語言怎麼設計一個函數使輸入某個值時強制結束程序
1.引入頭文件#include "stdafx.h"和#include "stdio.h"。
程序清單:
// 停止循環.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
void main()
{
int number = 0;
while(true)
{
printf("請輸入數字:
");
scanf ("%d",&number);
if (number == 1)
{
return;
}
}
}