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

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

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

bash 复制代码
#settings.json
{
    "workbench.settings.applyToAllProfiles": [
        "C_Cpp.suggestSnippets",
        "editor.snippetSuggestions"
    ],
    "[c]": {
    "editor.snippetSuggestions": "top"
	},
	"C_Cpp.suggestSnippets": false
}
bash 复制代码
#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"
  }
}

效果如下:

相关推荐
傻乐u兔6 分钟前
C语言进阶————指针3
c语言·开发语言
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
I'mChloe2 小时前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
2的n次方_2 小时前
Runtime 内存管理深化:推理批处理下的内存复用与生命周期精细控制
c语言·网络·架构
嵌入小生0072 小时前
标准IO---核心函数接口延续(嵌入式Linux)
c语言·vscode·vim·嵌入式·小白·标准io·函数接口
LYOBOYI1233 小时前
vscode界面美化
ide·vscode·编辑器
历程里程碑3 小时前
Linux20 : IO
linux·c语言·开发语言·数据结构·c++·算法
智者知已应修善业5 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法
小龙报6 小时前
【51单片机】从 0 到 1 玩转 51 蜂鸣器:分清有源无源,轻松驱动它奏响新年旋律
c语言·数据结构·c++·stm32·单片机·嵌入式硬件·51单片机
小武编程7 小时前
基于JL700N可视化SDK的MAC地址应用
c语言·tws耳机·杰理jl700n