Sublime Text中C程序执行问题:运行时无法输入
解决Sublime Text中C程序无法输入的问题
Sublime Text默认的构建控制台不支持交互式输入,所以你的程序运行时没法接收输入。下面给你两种实用的解决办法:
方法一:修改构建系统,让程序在外部终端运行
- 打开Sublime Text,依次点
Tools->Build System->New Build System - 清空默认内容,根据你的系统粘贴对应的配置:
- Windows系统:
{ "cmd": ["gcc", "$file", "-o", "$file_base_name"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c", "variants": [ { "name": "Run", "cmd": ["start", "cmd", "/k", "$file_base_name"] } ] } - Mac系统:
{ "cmd": ["gcc", "$file", "-o", "$file_base_name"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c", "variants": [ { "name": "Run", "cmd": ["open", "-a", "Terminal", "$file_path/$file_base_name"] } ] } - Linux系统:
{ "cmd": ["gcc", "$file", "-o", "$file_base_name"], "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", "working_dir": "${file_path}", "selector": "source.c", "variants": [ { "name": "Run", "cmd": ["xterm", "-e", "$file_path/$file_base_name; read -p 'Press enter to exit'"] } ] }
- Windows系统:
- 保存文件,命名为
C_Run_In_Terminal.sublime-build,保存位置选默认路径即可 - 之后运行程序时,先按
Ctrl+B编译,再按Shift+Ctrl+B选择Run选项,程序就会在外部终端打开,就能正常输入了
方法二:手动编译+终端运行
- 打开系统终端(Windows用CMD/PowerShell,Mac/Linux用Terminal),切换到你的C程序所在的文件夹
- 用gcc编译:
gcc 你的程序文件名.c -o 生成的可执行文件名 - 编译成功后,直接运行可执行文件:
- Windows:
生成的可执行文件名.exe - Mac/Linux:
./生成的可执行文件名
- Windows:
- 这样就能在终端里正常输入数字计算总和了
另外提醒下,确保你的代码里有明确的输入提示,比如:
#include <stdio.h> int main() { int num1, num2; printf("请输入第一个数字:"); scanf("%d", &num1); printf("请输入第二个数字:"); scanf("%d", &num2); printf("总和是:%d\n", num1 + num2); return 0; }
内容的提问来源于stack exchange,提问作者edger
相关产品推荐
相关产品推荐

