I primarily work on Linux, so I put this in my Emacs config:
; Linux mode for C (setq c-default-style '((c-mode . "linux") (other . "gnu")))
However, other projects like QEMU have their own style preferences. So here’s what I added to use a different style for that. First, I found the qemu C style defined here. Then, to only use this on some C code, we attach a hook that only overrides the default C style if the filename contains “qemu”, an imperfect but decent-enough test.
(defconst qemu-c-style '((indent-tabs-mode . nil) (c-basic-offset . 4) (tab-width . 8) (c-comment-only-line-offset . 0) (c-hanging-braces-alist . ((substatement-open before after))) (c-offsets-alist . ((statement-block-intro . +) (substatement-open . 0) (label . 0) (statement-cont . +) (innamespace . 0) (inline-open . 0) )) (c-hanging-braces-alist . ((brace-list-open) (brace-list-intro) (brace-list-entry) (brace-list-close) (brace-entry-open) (block-close . c-snug-do-while) ;; structs have hanging braces on open (class-open . (after)) ;; ditto if statements (substatement-open . (after)) ;; and no auto newline at the end (class-close) )) ) "QEMU C Programming Style") (c-add-style "qemu" qemu-c-style) (defun maybe-qemu-style () (when (and buffer-file-name (string-match "qemu" buffer-file-name)) (c-set-style "qemu"))) (add-hook 'c-mode-hook 'maybe-qemu-style)