1# C
2
3C support is available natively in Zed.
4
5- Tree-sitter: [tree-sitter/tree-sitter-c](https://github.com/tree-sitter/tree-sitter-c)
6- Language Server: [clangd/clangd](https://github.com/clangd/clangd)
7
8## Clangd: Force detect as C
9
10Clangd out of the box assumes mixed C++/C projects. If you have a C-only project you may wish to instruct clangd to all files as C using the `-xc` flag. To do this, create a `.clangd` file in the root of your project with the following:
11
12```yaml
13CompileFlags:
14 Add: [-xc]
15```
16
17By default clang and gcc by will recognize `*.C` and `*.H` (uppercase extensions) as C++ and not C and so Zed too follows this convention. If you are working with a C-only project (perhaps one with legacy uppercase pathing like `FILENAME.C`) you can override this behavior by adding this to your settings:
18
19```json
20{
21 "file_types": {
22 "C": ["C", "H"]
23 }
24}
25```
26
27## Formatting
28
29By default Zed will use the `clangd` language server for formatting C code. The Clangd is the same as the `clang-format` CLI tool. To configure this you can add a `.clang-format` file. For example:
30
31```yaml
32---
33BasedOnStyle: GNU
34IndentWidth: 2
35---
36```
37
38See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
39
40You can trigger formatting via {#kb editor::Format} or the `editor: format` action from the command palette or by adding `format_on_save` to your Zed settings:
41
42```json
43 "languages": {
44 "C": {
45 "format_on_save": "on",
46 "tab_size": 2
47 }
48 }
49```
50
51See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for a complete list of options.
52
53## Compile Commands
54
55For some projects Clangd requires a `compile_commands.json` file to properly analyze your project. This file contains the compilation database that tells clangd how your project should be built.
56
57### CMake Compile Commands
58
59With CMake, you can generate `compile_commands.json` automatically by adding the following line to your `CMakeLists.txt`:
60
61```cmake
62set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
63```
64
65After building your project, CMake will generate the `compile_commands.json` file in the build directory and clangd will automatically pick it up.