当前位置: 首页 > news >正文

【随手记】vscode中C语言满足KR风格的方法

由于生产需求,需要将编码风格改为K&R,为了方便,决定直接去修改vscode来满足

主要需要修改如下两个文件 settings.json 和 c.json

#settings.json
{"workbench.settings.applyToAllProfiles": ["C_Cpp.suggestSnippets","editor.snippetSuggestions"],"[c]": {"editor.snippetSuggestions": "top"},"C_Cpp.suggestSnippets": false
}
#c.json
{// ===== 基础控制结构 ====="K&R if": {"prefix": "if","body": ["if (${1:condition}) {","\t${0}","}"],"description": "K&R style if statement"},"K&R if-else": {"prefix": "ifelse","body": ["if (${1:condition}) {","\t${2}","} else {","\t${0}","}"],"description": "K&R style if-else"},"K&R multi if-else if": {"prefix": "ifelif","body": ["if (${1:condition1}) {","\t${2}","} else if (${3:condition2}) {","\t${4}","} else {","\t${0}","}"],"description": "K&R style multi-condition if-else if"},"K&R for loop": {"prefix": "for","body": ["for (${1:int i = 0}; ${2:i < N}; ${3:i++}) {","\t${0}","}"],"description": "K&R style for loop"},"K&R while loop": {"prefix": "while","body": ["while (${1:condition}) {","\t${0}","}"],"description": "K&R style while loop"},"K&R do-while": {"prefix": "do","body": ["do {","\t${0}","} while (${1:condition});"],"description": "K&R style do-while loop"},"K&R switch": {"prefix": "switch","body": ["switch (${1:variable}) {","\tcase ${2:value}:","\t\t${3}","\t\tbreak;","\t","\tdefault:","\t\t${0}","}"],"description": "K&R style switch statement"},// ===== 函数定义 ====="K&R function": {"prefix": "func","body": ["${1:void} ${2:function_name}(${3}) {","\t${0}","}"],"description": "K&R style function definition"},"K&R main function": {"prefix": "main","body": ["int main(int argc, char *argv[]) {","\t${0}","\treturn 0;","}"],"description": "K&R style main function"},// ===== 复合结构 ====="K&R struct": {"prefix": "struct","body": ["struct ${1:name} {","\t${0}","};"],"description": "K&R style struct definition"},"K&R typedef struct": {"prefix": "tstruct","body": ["typedef struct {","\t${0}","} ${1:name};"],"description": "K&R style typedef struct"},"K&R enum": {"prefix": "enum","body": ["enum ${1:name} {","\t${2:FIRST},","\t${3:SECOND},","\t${0}","};"],"description": "K&R style enum definition"},// ===== 错误处理 ====="K&R error check": {"prefix": "check","body": ["if (${1:error_condition}) {","\t${2:/* Handle error */}","\t${0}","}"],"description": "K&R style error checking"},"K&R return error": {"prefix": "returnerr","body": ["if (${1:error_condition}) {","\treturn ${2:error_code};","}"],"description": "K&R style return on error"},// ===== 内存管理 ====="K&R malloc": {"prefix": "malloc","body": ["${1:type} *${2:ptr} = malloc(sizeof(${1}) * ${3:count});","if (${2} == NULL) {","\t${4:/* Handle allocation error */}","\t${0}","}"],"description": "K&R style malloc with error check"},// ===== 文件操作 ====="K&R file open": {"prefix": "fopen","body": ["FILE *${1:fp} = fopen(${2:\"filename\"}, ${3:\"r\"});","if (${1} == NULL) {","\tperror(\"Error opening file\");","\t${0}","}"],"description": "K&R style file open with error check"},// ===== 其他常用结构 ====="K&R ternary": {"prefix": "tern","body": ["(${1:condition}) ? ${2:true_value} : ${3:false_value}"],"description": "K&R style ternary operator"},"K&R function pointer": {"prefix": "fptr","body": ["${1:return_type} (*${2:func_ptr})(${3:params}) = ${4:function};"],"description": "K&R style function pointer"}
}

效果如下:
请添加图片描述

http://www.xdnf.cn/news/1441855.html

相关文章:

  • Leetcode—695. 岛屿的最大面积【中等】
  • Docker实战指南:从安装到架构解析
  • 【Linux】网络(中)
  • 数据结构:栈和队列(上)
  • 数据结构从青铜到王者第十九话---Map和Set(2)
  • 下载必要软件
  • Qt读写Excel--QXlsx基本使用
  • 基于-轻量级文档搜索系统的测试报告
  • 【GM3568JHF】FPGA+ARM异构开发板 使用指南:WIFI
  • SQLite3 操作指南:SQL 语句与 ORM 方法对比解析​
  • 存算一体:重构AI计算的革命性技术(1)
  • K8s Pod CrashLoopBackOff:从镜像构建到探针配置的排查过程
  • react-android-0.80.2-debug.aar下载很慢
  • GitHub 宕机自救指南技术文章大纲
  • Flutter Android真机器调式,虚拟机调试以及在Vscode中开发Flutter应用
  • 充电座结构设计点-经验总结
  • 10.2 工程学中的矩阵(2)
  • Android/Java 异常捕获
  • 电子病历空缺句的语言学特征描述与自动分类探析(以GPT-5为例)(中)
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘isort’问题
  • MCP模型库哪个好?2025年收录12万+服务的AI智能体工具集成平台推荐
  • AI创业公司:来牟科技-智能割草机器人
  • 如何高效记单词之:抓住首字母——以find、fund、fond、font为例
  • 股指期货放开后,市场会发生什么变化?
  • 数据结构:顺序栈与链栈的原理、实现及应用
  • 解析SWOT分析和PV/UV这两个在产品与运营领域至关重要的知识点。
  • 前端性能优化:请求和响应优化(HTTP缓存与CDN缓存)
  • Redis初阶学习
  • 宋红康 JVM 笔记 Day12|执行引擎
  • 《SVA断言系统学习之路》【03】关于布尔表达式