r/cprogramming • • Sep 10 '26

Formatting call arguments

By default, clang-format does this:

  SDL_Window *window =
      SDL_CreateWindow("SDL2 test", SDL_WINDOWPOS_UNDEFINED,
                       SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_RESIZABLE);

and with some tweaks it does this:

    SDL_Window *window = SDL_CreateWindow(
        "SDL2 test",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        500,
        500,
        SDL_WINDOW_RESIZABLE);

Second one has some practical advantages - it easier to read diff on arg change and easier to add comments:

    SDL_Window *window = SDL_CreateWindow(
        "SDL2 test",
        SDL_WINDOWPOS_UNDEFINED, // just some comment explaining why
        SDL_WINDOWPOS_UNDEFINED,
-       400,
+       500,
        SDL_WINDOW_RESIZABLE);

While the first one seems to have mainly historical reasons to keep around. Perhaps, there is something I don't see? Because it feels like the first way is still used the most. And its the default one for clang-format.

I'm genuinely curious.

4 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/side2k Sep 10 '26

Thats what I was missing - the eyesight. Thanks for the perspective! And even with the good eyesight and small (e.g. laptop) display, aligning second line of args with the opening parenthesis must be useful when you scroll editor to left and see all of the args at once.

And that other formatter - did you mean prettier? I dont know whether it can format C.

2

u/jason-reddit-public Sep 10 '26

I get maybe 50 lines of text but lots of columns. If I had better font smoothing (long story...), I'd probably get to 60. C functions run a bit large versus code I write in other languages so even 60 is sometimes not enough. Your example is a good example why: Extra long names because of zero namespaces, no default arguments, etc. I should probably write more inline functions...

The other tool is calked uncrustify. It's not prettier but does have different options and quite a few. I had it spit out the defauly config and did a quick pass setting up stuff for my language roci.

Probably worth a look but I still use clang-format for my C code (base of LLVM with some tweaks).

It would be cool if this was a solved problem and I could just point emacs at my options! (Or VSCode or neovim, etc.)

1

u/side2k Sep 11 '26

"I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion..." © 8)

Yeah, legacy code names (like SDL in my examples) seem to be a bit of pain.