2

Both the C11 and C17 standards use terms “conversion specifier” and “format specifier”. Are they synonyms? If yes, then why introducing the synonyms? If no, then what is the difference between them?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
pmor
  • 5,392
  • 4
  • 17
  • 36
  • 1
    My take is that format specifier is the broader term, meaning either a conversion or length specifier etc. This is all kind of sloppily written. – Lundin Jun 02 '21 at 12:55

3 Answers3

3

"Format specifier" appears to just be a semi-formal term. The only normative text using the term is the inttypes.h headline 7.8.1, with no explanation provided of what a "format specifier" is. Though inttypes.h mixes the size and type/signedness into a single term, because of the nature of the stdint.h types. It says that d in PRId32 is a conversion specifier and then PRId32 as whole is supposedly a format specifier? While fprintf/fscanf separates for example "length modifiers" and "conversion specifiers", where l in %ld would be the length modifier and d the conversion specifier.

The fprintf and fscanf functions define a term conversion specification, for example C17 7.21.6.1, emphasis mine:

Each conversion specification is introduced by the character %. After the %, the following appear in sequence:

  • Zero or more flags...
  • An optional minimum field width...
  • An optional precision
  • An optional length modifier
  • A conversion specifier character that specifies the type of conversion to be applied.

The above is also the formal definition of the term conversion specifier. Then these functions list valid conversion specifiers as d, i, f and so on.

It would appear that "format specifier" and conversion specification mean the same thing.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • "conversion specifier" seems to describe the native type (int, char, double) whereas the "format specifier" seems to describe/includes the presentation (field length, padding). – Paul Ogilvie Jun 02 '21 at 13:24
  • @PaulOgilvie The inttypes.h listing of format specifiers lists for example `PRId32` as a "format specifier". Now what is what in `printf("%.3" PRId32, (int32_t){1});`. The whole `%.3" PRId32` ought to be a conversion specification. The `PRId32` perhaps a "format specifier". And the `d` a conversion specifier. – Lundin Jun 02 '21 at 13:28
  • _where `l` in `%ld` would be the length specifier_: `length specifier` or `length modifier`? C11: _If no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written._ – pmor Jun 02 '21 at 14:17
  • In which section `length specifier` is mentioned? – pmor Jun 02 '21 at 14:19
  • @pmor That would be a typo, it should say modifier. – Lundin Jun 02 '21 at 14:22
2

Independent of the standard, which is confusing looking at other answers, so just looking at the meaning of these terms in the English language, I am inclined to say

"Conversion specifier" describes the native type (int, char, double) from or to which data is converted, whereas the "format specifier" describes or includes the presentation (field length, padding).

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    For those trying to implement compilers, that's not good enough though. The standard shouldn't sloppily use terms that aren't defined. Sloppily designed goes for stdio.h as whole though. – Lundin Jun 02 '21 at 13:31
  • @Lundin, I think my definition is pretty accurate. But there is some mixing, such as `%d` and `%x` which are both conversion (int) and presentation (hex). A proper specifier could be `%xd` which means an int (conversion) to be printed hex (presentation). – Paul Ogilvie Jun 02 '21 at 13:36
  • @Lundin _Sloppily designed goes for stdio.h as whole though._ What other parts of stdio.h are sloppily designed? Can you provide examples? – pmor Jun 02 '21 at 20:57
  • @pmor It would be easier to mention the parts that were correctly designed. I think maybe the `remove`, `rename` and `puts` functions are properly designed and without problems. The rest of stdio.h either has design flaws, known pitfalls or is just plain irrational. The printf/scanf families of functions are probably holding the world record in monetary damage caused to mankind. I can't think of any other function in any programming language that comes close. Sure there are countless of other broken standard functions in C, but none used as frequently. – Lundin Jun 03 '21 at 06:50
  • @Lundin I faced with the issue with `rename`. The behavior of `rename` depends on the host file system (FS), and, hence, on the host OS. Under Linux it is possible to `rename` already opened file [1], while on Windows, it isn't. So, I was wondering why the standard `rename` function has different behaviors across implementations. [1] Even if it was either opened or renamed by mistake / usage error / bug. – pmor Jun 23 '21 at 17:27
  • @Lundin I was surprised as well that `(f)scanf` [may lead to UB](https://stackoverflow.com/a/44740571/1778275). So, multiple recommendations say ["do not use (f)scanf because it may lead to UB"](https://wiki.sei.cmu.edu/confluence/display/c/INT05-C.+Do+not+use+input+functions+to+convert+character+data+if+they+cannot+handle+all+possible+inputs) – pmor Jun 23 '21 at 17:33
-1

The Format Specifier only specifies the format of the conversion (ie 'f') where the Conversion Specifier means the whole Specifier (ie %*13lf).

I took the example from this answer: What is the difference between conversion specification and format specifier in C?

ELIZA
  • 51
  • 1
  • 9
  • 1
    The term conversion specifier has a formal definition: (7.21.6/4) "A conversion specifier character that specifies the type of conversion to be applied." And then it says "The conversion specifiers and their meanings are: d, i" and so on. So I'm not sure if your answer or the one you link to are correct. – Lundin Jun 02 '21 at 12:58
  • 1
    Ah wait, now read the link again. He's not saying that a conversion _specifier_ is the whole specifier. He is saying that the conversion _specification_ is the whole specifier. – Lundin Jun 02 '21 at 13:04