0

What's the difference between lc (C) and c / ls (S) and s in printf() function? Why does ls (S) conversion return -1?

Exemple :

printf("%C", 'ͳʹ); // -1
printf("%c", 'ͳʹ); // PRINT
printf("%S", "ͳ ans T"); // -1
printf("%s", "ͳ and T"); // PRINT

On mac..

potheo
  • 17
  • 1
  • 5
  • can you phrase your question more clearly - not sure what (C) and (S) are supposed to represent – M.M Jun 02 '16 at 09:42
  • C for lc, but it's obsolet like S for ls – potheo Jun 02 '16 at 09:48
  • Downvoted, because question doesn't show any code that would return such value. Please read on [mcve]. – user694733 Jun 02 '16 at 09:54
  • also include which compiler and library you are using and what compilation switches; before C99 there were various incompatible implementations of wide string printing; and after C99 at least one major compiler stuck with what they had instead of switching to the standard, or offered standard as a switch – M.M Jun 02 '16 at 09:56
  • Add sample code to illustrate what you mean. I think you are talking about format specifiers, but I'm not sure. – Klas Lindbäck Jun 02 '16 at 10:22
  • @potheo Do you have a cite about the obsoluteness of `S`? – harper Jun 02 '16 at 11:51
  • Note that **conversion specifier** `%S` is not standardized (!) so you won't find it in the newer *ISO C draft* (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf) and consequently neither on a well known page like *Creference* (https://en.cppreference.com/w/c/io/fprintf). So my advice to all is to use standardized way which is using a **conversion specifier** `%s` prefixed with a **length modifier** `l` and you get `%ls` which does exactly the same as `%S` that I can't find documented nowhere (!). – 71GA Feb 06 '20 at 06:20

1 Answers1

5

From the manual page:

l

(ell) [...] or a following c conversion corresponds to a wint_t argument, or a following s conversion corresponds to a pointer to wchar_t argument.

So the change is in which type is expected:

  • %c- int which is internally converted to unsigned char
  • %lc - wint_t
  • %s - pointer to zero-terminated array of char
  • %ls - pointer to zero-terminated array of wchar_t

A negative return value, such as -1, from printf() indicates that an error occured. It's hard to pinpoint further since you're not showing any code.

unwind
  • 391,730
  • 64
  • 469
  • 606