Search code examples
cemacsflycheck

Emacs flycheck C99 mode


I'm new to Emacs. How can I set the Flycheck package to use C standard 99 (C99 mode)? For the following code:

for(int i = 0; i < n; i++) ...

Flycheck will throw me: for loop initial declarations are only allowed in C99 mode.


Solution

  • Set flycheck-clang-language-standard or flycheck-gcc-language-standard (depending on what tool you installed) to "gnu99", via Directory Variables or File Variables.

    For the former, type M-x add-dir-local-variable RET c-mode RET flycheck-clang-language-standard RET "gnu99".

    To set the standard globally, use a C mode hook to set the value in each buffer:

    (defun my-flycheck-c-setup ()
      (setq flycheck-clang-language-standard "gnu99"))
    
    (add-hook 'c-mode-hook #'my-flycheck-c-setup)
    

    Do not use setq-default to change the global value. That will break if you edit a C++ file.