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


Groups > comp.lang.c > #147548

Re: memcpy not memcpying?

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c
Subject Re: memcpy not memcpying?
Date 2019-11-03 19:44 +0100
Organization Aioe.org NNTP Server
Message-ID <qpn76m$mt6$1@gioia.aioe.org> (permalink)
References (13 earlier) <EAovF.246198$IsL.142854@fx08.am4> <lnv9s169ev.fsf@kst-u.example.com> <1BAvF.13970$mB7.12243@fx32.am4> <qpn0a2$dn3$1@dont-email.me> <%%DvF.115593$Yl3.480@fx24.am4>

Show all headers | View raw


On 11/3/2019 6:20 PM, Bart wrote:
> On 03/11/2019 16:46, David Brown wrote:
>> On 03/11/2019 14:27, Bart wrote:
> 
>>> The important thing is to have a format where you don't need to know 
>>> the exact type of the thing being printed, just whether it's int, 
>>> float, string or pointer. Or, very often, not even that - just print 
>>> X, whatever type it happens to be, or print X with a given width.))
>>>
>>
>> I usually know the type of my variables and expressions, so don't see 
>> that as an important thing at all.  I /do/, however, agree that the 
>> PRI macros are ugly, even though I understand why they exist.
> 
> (1) A numeric type T exported from a library's header. You want to print
>     it, but now you have to delve into the header to find out what type
>     it is, to work out what format it needs.
> 
>     (Using an IDE to find that is not a solution: see (2).)

This is a complication, but I would say that if this output is part of 
the characteristic program output (i.e. it is part of the results that 
your program is meant to produce) then in a real world case I would be 
unlikely to directly print some deep library-exported value, the result 
of my program would probably be some value that is built by my code - so 
of a type that I know.
(Debug output is a different matter, but so is the required API 
friendliness in this case)

> 
> (2) Even when you know exactly the type T of an expression, you will be
>      embedding the format string in dozens or hundred of sites within the
>      code. Then T changes (eg. the library which exports T is updated.)
> 
> (3) You define a type like this:
> 
>       #if cond
>          int x;
>       #elif
>          double x;
>       #endif
> 
> 
>      Now you want print x; what format to use?
> 
> (4) You have X of type T, and Y of type U, both compatible numeric
>      types. Now you want to print the value of this expression:
> 
>         X*Y
> 
>      What format to use? What about when it changes to X*2+Y*3?. How
>      about when the expression is complex that you don't know whether the
>      type is int or double or unsigned long long int?

As an answer, if you know the range of the value, the easiest solution 
is to cast to the wider type (e.g. double in this case) and format 
accordingly.

However, to me it is fairly well known that printf is not meant to be a 
complete output engine, so it is no surprise that it is less than 
friendly when it comes to composing complex output.
After all, it is for this reason that most other languages (starting 
from C++) have tried hard to improve on it - with mixed results by the way.

I believe it is fair to say that that C does not aim to be friendly at 
building elaborated UIs, so if some fancy user output is needed, 
probably C is not the right tool for the job. The power of the language 
lies elsewhere.

> 
> 
> The "%?" suggestion works with all of these when the type is numeric, 
> and in many cases also when the type is a pointer or string.

If I understand it correctly (unless you are suggesting that the 
compiler would modify the format string), this would require runtime 
type handling (and in fact several interpreted languages do something 
very similar today), which may work well with your interpreted language, 
but not in C.

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


Thread

memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:42 -0400
  Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 08:49 -0400
    Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:24 -0400
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:46 -0400
        Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:49 -0400
        Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:19 -0700
          Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-01 21:14 +0100
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 17:16 -0400
            Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 22:42 +0000
              Re: memcpy not memcpying? Richard Damon <Richard@Damon-Family.org> - 2019-11-01 21:10 -0400
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 11:36 +0000
            Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 02:15 -0700
            Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 13:04 +0100
              Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 13:43 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:24 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 17:00 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 13:06 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 21:05 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 22:40 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 15:59 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-02 23:47 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-02 18:22 -0700
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 13:27 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:46 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:20 +0000
                Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 19:44 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 19:36 +0000
                Re: memcpy not memcpying? Manfred <noname@add.invalid> - 2019-11-03 22:57 +0100
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-03 22:39 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 23:06 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 23:28 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 11:00 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 11:14 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 11:22 +0000
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-04 13:10 +0100
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-04 10:03 -0800
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 19:36 +0000
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-05 10:04 -0800
                Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 20:58 -0800
                Re: memcpy not memcpying? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-04 22:01 -0800
                Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-04 12:56 +0100
                Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-04 09:50 -0500
                Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-04 20:26 +0000
                Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-04 04:34 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 13:05 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 14:09 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 15:11 +0000
                Re: memcpy not memcpying? Melzzzzz <Melzzzzz@zzzzz.com> - 2019-11-04 15:15 +0000
                Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-04 15:43 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 20:20 +0000
                Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-03 14:37 +1300
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 11:01 +0000
                Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 04:03 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 12:52 +0000
                Re: memcpy not memcpying? Öö Tiib <ootiib@hot.ee> - 2019-11-03 23:34 -0800
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 09:36 +0000
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-04 16:20 +0000
                Re: memcpy not memcpying? Ian Collins <ian-news@hotmail.com> - 2019-11-04 07:46 +1300
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:26 -0800
                Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-03 17:31 +0100
                Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-03 17:05 +0000
              Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 09:14 -0800
                Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-03 12:32 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 14:59 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 15:19 -0800
                Re: memcpy not memcpying? already5chosen@yahoo.com - 2019-11-03 17:00 -0800
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-02 11:16 -0400
            Re: memcpy not memcpying? David Brown <david.brown@hesbynett.no> - 2019-11-02 17:26 +0100
    Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 06:39 -0700
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 09:56 -0400
        Re: memcpy not memcpying? Barry Schwarz <schwarzb@delq.com> - 2019-11-01 12:39 -0700
    Re: memcpy not memcpying? guinness.tony@gmail.com - 2019-11-01 06:49 -0700
    Re: memcpy not memcpying? jak <nospam@please.ty> - 2019-11-01 20:40 +0100
  Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:26 +0100
    Re: memcpy not memcpying? Keith Thompson <kst-u@mib.org> - 2019-11-01 10:22 -0700
    Re: memcpy not memcpying? Bart <bc@freeuk.com> - 2019-11-01 19:44 +0000
      Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 21:31 +0100
  Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:39 -0400
    Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:42 +0100
      Re: memcpy not memcpying? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-11-01 09:48 -0400
  Re: memcpy not memcpying? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-01 06:47 -0700
    Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 14:58 +0100
      Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 10:22 -0400
        Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 18:38 +0100
          Re: memcpy not memcpying? scott@slp53.sl.home (Scott Lurndal) - 2019-11-01 17:54 +0000
            Re: memcpy not memcpying? Autist <autist69@gmail.com> - 2019-11-01 20:24 +0100
          Re: memcpy not memcpying? DFS <nospam@dfs.com> - 2019-11-01 15:24 -0400
        Re: memcpy not memcpying? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-01 23:19 +0000

csiph-web