首页 > 编程语言 >VScode格式化C语言程序时,让左大括号不换行的解决方案

VScode格式化C语言程序时,让左大括号不换行的解决方案

时间:2022-10-15 10:33:42浏览次数:46  
标签:return VScode C语言 int num 数组 printf 大括号

前言

继上次用VScode写C语言之后,感觉舒服了不少,可是还是有一点让我觉得美中不足的地方……
那就是!每次格式化C语言程序的时候,都会把我故意不换行的左大括号给换行了!
后来找到了解决方案,在此记录。

解决方案

打开VScode-文件-设置,搜索设置项:C_Cpp: Clang_format_style
此项默认值为:

file

将其改为:

{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Linux, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 } 

测试效果

那么就来写一个小程序测试一下效果:

输入一个正整数n(1<n<=10),再输入n个整数并存入数组,然后将数组中的这n个数逆序存放并输出存放结果。试编写相应程序

源代码(设置前):

#include <stdio.h>
int main(void)
{
    int n;
    printf("Please enter an integer N (1-10):");
    scanf("%d", &n);
    if (n < 1 || n > 10)
    {
        printf("ERROR!\n");
        return -1;
    }
    //定义并初始化数组
    int num[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    //逆序输入数据进入数组
    for (int i = n - 1; i >= 0; i--)
    {
        printf("Please enter the %dth number:", n - i);
        scanf("%d", &num[i]);
    }
    //输出数组内容
    printf("\n\nThe contents in the array are:");
    for (int i = 0; i < n; i++)
    {
        printf("%d ", num[i]);
    }
    printf("\n");
    return 0;
}

设置后,格式化效果:

#include <stdio.h>
int main(void)
{
    int n;
    printf("Please enter an integer N (1-10):");
    scanf("%d", &n);
    if (n < 1 || n > 10) {
        printf("ERROR!\n");
        return -1;
    }
    //定义并初始化数组
    int num[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    //逆序输入数据进入数组
    for (int i = n - 1; i >= 0; i--) {
        printf("Please enter the %dth number:", n - i);
        scanf("%d", &num[i]);
    }
    //输出数组内容
    printf("\n\nThe contents in the array are:");
    for (int i = 0; i < n; i++) {
        printf("%d ", num[i]);
    }
    printf("\n");
    return 0;
}

标签:return,VScode,C语言,int,num,数组,printf,大括号
From: https://www.cnblogs.com/CoronaZero/p/16793660.html

相关文章