Vscode writes C language program to output Chinese garbled characters

First of all, we need to be clear about the process of writing a c language program and running it. In fact, we create a xxx.c file, then execute gcc xxx.c to generate a xxx.exe file, and then run the xxx.exe file to see the running effect.

 

 

 

 

Open our command line to view its properties and you will find that the character set it uses is UTF-8

 

At this time, if the encoding format of our c language program is not consistent with the character set of the cmd command line, garbled characters will be output.

The principle of vscode running the c program itself is also the same. It helps us open a shell, execute gcc to generate exe, and run the exe file to see the output effect. However, the encoding format of vscode is utf8 by default. If the encoding is not uniform, the output will be garbled.

 

We can manually modify the encoding format to gbk

 

 At this time, if the code becomes garbled, press Ctrl to undo it, and then you can output Chinese normally.

But in this way, every time we create a C language program, we need to manually modify the code as above. A better solution is to select settings.json in our vscode settings and add the following configuration:

 

 "[cpp]": {
    "files.encoding": "gbk"
  },
  "[c]": {
    "files.encoding": "gbk"
  },

 Tell vscode that when writing c programs and c++ programs, use gbk encoding.

The newly created c file is encoded by gbk by default.

 When we output Chinese again, there will be no garbled characters.

 After knowing these principles, we can solve the problem of garbled characters when we write programs in other languages, which can also be solved by similar methods.

Guess you like

Origin blog.csdn.net/m0_52726759/article/details/123727812