:: Re: [DNG] "Common knowledge?"-quest…
Etusivu
Poista viesti
Vastaa
Lähettäjä: Dr. Nikolaus Klepp
Päiväys:  
Vastaanottaja: dng
Aihe: Re: [DNG] "Common knowledge?"-question
Am Montag, 25. Januar 2016 schrieb KatolaZ:
> On Mon, Jan 25, 2016 at 09:27:15AM +0100, Didier Kryn wrote:
>
> [cut]
>
> >
> >     The following works in plain old C:

> >
> > #include <stdio.h>
> > #include <string.h>
> > static void print_start(char const *name, char const *what)
> > {
> >      unsigned name_len, what_len, total;

> >
> >      name_len = strlen(name);
> >      what_len = strlen(what);
> >      total = name_len + what_len + 3;
> >      {
> >        char buf[total], *p=buf;
> >        memcpy(p, name, name_len);
> >        p += name_len;
> >        *p++ = ' ';
> >        memcpy(p, what, what_len);
> >        p += what_len;
> >        *p++ = ':';
> >        *p = ' ';
> >        *buf &= ~0x20;

> >
> >        Write(2, buf, total);
> >      }
> > }

> >
> >     Embedded subprograms have other use cases. In long programs,
> > they allow to declare variables with a limited scope, just near
> > where they are used.

>
> I would say that having embedded subprograms in a function is not the
> best thing one can do in C, but that's maybe a matter of preference :)
>
> HND
>
> KatolaZ
>


What about making the source smaller and not the executable?

static void print_start(char const *name, char const *what) {
     if (name[0]) fprintf("%c%s",name[0]&~0x20, name+1);
     fprintf(stderr, " %s: ",what);
}


I know this is not slim, but as maintenance time rises with pow(lines-of-code,something>1.0), it pays quite soon :-)

--
Please do not email me anything that you are not comfortable also sharing with the NSA.