Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #64953

Re: printf format specifier changes

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: printf format specifier changes
Date 2015-07-08 11:27 -0700
Organization None to speak of
Message-ID <lnd202a4iq.fsf@kst-u.example.com> (permalink)
References <d9147e9f-77b1-435a-b037-24041fd6974a@googlegroups.com> <mnebi4$p4v$1@dont-email.me> <87mvz6d0an.fsf@bazspaz.fatphil.org>

Show all headers | View raw


Phil Carmody <pc+usenet@asdf.org> writes:
> Bartc <bc@freeuk.com> writes:
>> On 06/07/2015 17:04, Rick C. Hodgin wrote:
>> > In 2015, what suggestions would you have for changing anything about
>> > the format specifiers used in the printf functions?  Leave them as
>> > they are?  Or change this or that about them?
>> 
>> In 2015, the whole notion of having to tell a compiler what it already
>> knows - the type of the values it's printing - is outdated.
>
> You're confusing the compiler and the runtime. The compiler
> knows the types at compile time, but it does not tell the
> runtime that information. So it has to come from somewhere
> else, namely encoded in one or more parameters.
>
> One thing that makes me laugh is that despite apparently everyone's
> superficial hate of it, it gets inherited into so many other languages.
>
> I don't believe anyone's hate of the *f functions can match my
> hate of C++'s '>>' and '<<'. There, I said it.

It would be interesting to have a C extension (perhaps becoming a
standard feature in a future version) that provides a new form of
variadic function.

This is off the top of my head, and doubtless has numerous flaws.

Existing variadic functions would remain as they are, for backward
compatibility.

A new syntax such as

    int func(int arg1, ... other_args);

where "other_args" can be any arbitrary identifier, would permit calls
just like existing calls to variadic functions.  In this case, a call
must provide at least one argument, and the first argument must be of
type int (or implicitly convertible to int).  The remaining arguments,
if any, can be of any type.  Unlike for varaidic functions, no fixed
arguments would be required.

Inside the function, other_args would be of some new type, say
"arg_list" defined in a new header.  It would encode, somehow, the
number and types of the remaining arguments.  The compiler would be
responsible for generating code that passes an argument of the correct
type.  Perhaps this would be limited to certain types (say, predefined
numeric types and pointers to them).

The function might still use the <stdarg.h> macros to retrieve the
argument values, but it would *know* the actual types passed by the
caller.

A simplified version of printf might be:

    int print(... args);

so that this:

    int n = 42;
    print("n = ", n, ", &n = ", &n, "\n");

would behave like:

    printf("n = %d, &n = %p\n", n, (void*)&n);

The implementation of print() would be moderately complicated (it would
have to interpert the type information in the "args" parameter and use
something like a switch statement to print the values), but that
wouldn't matter when you're calling it.  Unlike with printf(), there
would be no opportunity to use a format string that's incompatible with
the actual arguments.

(Character literals are of type int, so that could be a source of bugs,
but using "\n" rather than '\n' avoid that problem.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:04 -0700
  Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 17:50 +0100
    Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 11:34 -0700
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 19:47 +0100
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 19:53 +0100
          Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 11:57 -0700
      Re: printf format specifier changes "James Harris" <james.harris.1@gmail.com> - 2015-09-06 10:53 +0100
        Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-06 04:21 -0700
        Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-06 13:41 +0100
    Re: printf format specifier changes Richard Heathfield <rjh@cpax.org.uk> - 2015-07-06 19:44 +0100
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-06 22:14 +0100
        Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-07-06 22:43 -0500
        Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-06 23:01 -0700
          Re: printf format specifier changes glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-07 06:43 +0000
          Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 10:52 +0300
            Re: printf format specifier changes Richard Heathfield <rjh@cpax.org.uk> - 2015-07-09 09:26 +0100
              Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 12:56 +0300
                Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-12 06:12 -0700
        Re: printf format specifier changes Rosario19 <Ros@invalid.invalid> - 2015-07-07 10:22 +0200
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 10:10 +0100
      Re: printf format specifier changes Philip Lantz <prl@canterey.us> - 2015-07-07 00:54 -0700
    Re: printf format specifier changes Nobody <nobody@nowhere.invalid> - 2015-07-07 15:08 +0100
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 16:12 +0100
        Re: printf format specifier changes raltbos@xs4all.nl (Richard Bos) - 2015-07-09 10:34 +0000
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 12:25 +0100
            Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 13:02 +0300
              Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-12 11:23 +0100
    Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-07 11:50 -0700
      Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-07 12:14 -0700
    Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-07 21:52 -0500
      Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-07 20:42 -0700
        Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:12 +0300
          Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-09 05:03 -0700
            Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 13:10 -0500
          Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-09 08:58 -0400
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 08:10 -0700
            Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-12 12:58 +0300
              Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-12 12:57 -0400
              Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-12 11:14 -0700
                Re: printf format specifier changes gazelle@shell.xmission.com (Kenny McCormack) - 2015-07-12 18:26 +0000
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-08 13:09 +0100
      Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-08 08:10 -0400
        Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-08 08:12 -0500
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-08 08:38 -0700
          Re: printf format specifier changes James Kuyper <jameskuyper@verizon.net> - 2015-07-08 12:00 -0400
            Re: printf format specifier changes Les Cargill <lcargill99@comcast.com> - 2015-07-08 17:20 -0500
      Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:08 +0300
    Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-08 20:30 +0300
      Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-08 18:56 +0100
        Re: printf format specifier changes gordonb.lmwiv@burditt.org (Gordon Burditt) - 2015-07-09 11:09 -0500
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 14:05 -0500
      Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-08 11:27 -0700
        Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-08 15:07 -0500
  Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-06 23:39 -0500
    Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 10:19 +0100
      Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 07:57 -0500
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-07 14:15 +0100
          Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 14:46 +0100
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 09:37 -0500
        Re: printf format specifier changes Phil Carmody <pc+usenet@asdf.org> - 2015-07-09 11:24 +0300
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 09:55 -0500
            Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-09 11:53 -0700
              Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-09 13:48 -0700
              Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 17:01 -0500
    Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 10:59 +0100
      Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 07:48 -0500
        Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 14:30 +0100
          Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 10:05 -0500
            Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 23:16 +0100
              Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-07 20:29 -0500
              Re: printf format specifier changes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-08 00:43 -0700
              Re: printf format specifier changes Robert Wessel <robertwessel2@yahoo.com> - 2015-07-09 02:29 -0500
                Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-09 10:34 +0100
                Re: printf format specifier changes BGB <cr88192@hotmail.com> - 2015-07-09 09:35 -0500
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-07 08:37 -0700
            Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-07 08:45 -0700
          Re: printf format specifier changes <william@wilbur.25thandClement.com> - 2015-07-07 12:12 -0700
  Re: printf format specifier changes pooja deshpande <namitadeshpande25@gmail.com> - 2015-07-09 09:29 -0700
    Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 09:34 -0700
      Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 09:59 -0700
        Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 22:33 +0100
          Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 15:17 -0700
            Re: printf format specifier changes glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-10 00:30 +0000
              Re: printf format specifier changes Reinhardt Behm <rbehm@hushmail.com> - 2015-07-10 09:17 +0800
      Re: printf format specifier changes David Kleinecke <dkleinecke@gmail.com> - 2015-07-09 10:02 -0700
        Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 10:19 -0700
      Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-09 21:50 +0100
        Re: printf format specifier changes Keith Thompson <kst-u@mib.org> - 2015-07-09 15:05 -0700
          Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:09 -0700
          Re: printf format specifier changes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 00:28 +0100
            Re: printf format specifier changes raltbos@xs4all.nl (Richard Bos) - 2015-07-13 20:39 +0000
        Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:07 -0700
          Re: printf format specifier changes Bartc <bc@freeuk.com> - 2015-07-09 23:16 +0100
            Re: printf format specifier changes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-09 15:25 -0700

csiph-web