cgccgnudd

What does `_` do, in `printf (_("abc"));`, present in gnu-dd's code, but gcc giving me errors, and warning


Im reading the source code of the gnu's dd command, but noticed a weird symbol.

NOTE: all below mentioned code, is compiled on latest gcc, on arch GNU/Linux on physical(not VM) amd64,

At, the statement printf (_("abc"));, is giving

warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]

along with

undefined reference to `_'`

error, and program returning status 1, on my computer, same for fputss below that statement


Solution

  • That is defined in system.h:179,

    #define _(msgid) gettext (msgid)
    

    (how did I find this: open dd.c in neovim, move cursor to the _ in question, run :lua vim.lsp.buf.definition())

    So, that puts the message through the GNU gettext string translator.

    (two cents on coding style: Generally, try to avoid using such extremely short macros as _, and use proper functions if possible. All that _ being a macro does is save typing 6 characters at the cost of obscuring what happens. IMHO, a bad terseness/readability tradeoff. When you look at GNU coreutils mailing lists, you'll find me contradict coreutil's approach to macros more widely. I'll say that it's a very mature code base, going back to at least 1992, and you definitely notice that in the coding style. I probably wouldn't recommend writing C in 2023 as most of coreutils looks like)