🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #1510

Re: Printf: vaste lengte strings en ints?

#1510
From: moi
Date: Wed, 02 Mar 2005 23:37
78 lines
3730 bytes
Dr.Ruud wrote:
> Wat moi schreef, bleek het
> begin  van iets moois; kijk zelf maar:
> 
> 
>>Ik denk dat:
>>
>>char buff[ 30+ 3 *INT2POS(int) + 2 * FLOAT2POS(double) +1];
>>sprintf(buff, "%d %d : %d : dubbele vlootjes: [%lf %lf %lf %lf]\n"
>>  ,a,b,c, f1,f2,f3,f4);
>>
>>... een veel lastiger soort probleem kan veroorzaken dan
>>
>>char buff[512] ;
>>printf(buff, "%d %d : %d : dubbele vlootjes: [%lf %lf %lf %lf]\n"
>>  ,a,b,c, f1,f2,f3,f4);
> 
> 
> Bedoel je die '2 *' terwijl je 4 long floats wilt weergeven?
> Of die 30 die >=32 moet zijn?

Precies. Vergelijkbaar met het checken van een formatstring is het 
checken van de macroargumenten. Alleen lastiger ...

> Een FLOAT2POS is trouwens wat lastiger te maken.

Draadje kan erg lang worden ...

> 
> Ik heb de preciese syntax niet gecontroleerd, maar zoiets kan dacht ik
> ook:
> 
>   const char fmt = "%d %d : %d : dubbele vlootjes: [%lf %lf %lf %lf]\n";
*--------------^

>   char buff[ sizeof(fmt[]) + 3 * INT2POS(int) + 4 * FLOAT2POS(double)
> +1];
>   sprintf(buff, fmt, a,b,c, f1,f2,f3,f4);
> 
Precies. In prototype is het "const char * fmt". en een stringconstante 
is daar een goede kandidaat voor.

> 
> Ik denk dat het '%Lf' is en de 'l' alleen voor integer-formats mag
> staan.
> Of %lf is double, en %Lf long double, dat kan ook nog.
> 

Toch maar even nagezocht:
        f,F    The double argument is rounded and converted to decimal 
          notation
               in  the  style  [-]ddd.ddd, where the number of digits 
after the
               decimal-point character is equal to the precision 
specification.
               If  the precision is missing, it is taken as 6; if the 
precision
               is explicitly zero, no decimal-point character  appears. 
   If  a
               decimal point appears, at least one digit appears before it.


Default promotie float->double, dus.
De 'l' modifyer blijkt geen betekenis te hebben voor %f ...
Weer wat geleerd.

> 
> 
>>En, nogmaals: als je het echt zeker wilt weten, is vsnprintf() je
>>vriendje.
> 
> 
> Ja, dat is beter.
> 

Zijn we het eens.

AvK
Technical Details
Message-ID: <dK2dnaVglsHc07vfRVnyjw@casema.nl>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.casema.nl!news.casema.nl.POSTED!not-for-mail
References: <M5qdnYvpZutbs7nfRVnyjA@scarlet.biz> <d01i4u.14c.1@news.isolution.nl> <CqudnXnc_MjnCbjfRVnyhg@casema.nl> <d050po.130.1@news.isolution.nl> <FMSdnW58LdbOtrvfRVnyvQ@casema.nl> <d05ins.l8.1@news.isolution.nl>

Show Full Headers
Complete Article Headers
X-Google-Thread: fcea4,f962124bbb04ff22
X-Google-Thread: 107324,f962124bbb04ff22
X-Google-Attributes: gidfcea4,gid107324,public
X-Google-Language: DUTCH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.casema.nl!news.casema.nl.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 02 Mar 2005 17:37:37 -0600
Date: Thu, 03 Mar 2005 00:37:37 +0100
From: moi <avk@localhost>
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040510
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: be.comp.programming,nl.comp.programmeren
Subject: Re: Printf: vaste lengte strings en ints?
References: <M5qdnYvpZutbs7nfRVnyjA@scarlet.biz> <d01i4u.14c.1@news.isolution.nl> <CqudnXnc_MjnCbjfRVnyhg@casema.nl> <d050po.130.1@news.isolution.nl> <FMSdnW58LdbOtrvfRVnyvQ@casema.nl> <d05ins.l8.1@news.isolution.nl>
In-Reply-To: <d05ins.l8.1@news.isolution.nl>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <dK2dnaVglsHc07vfRVnyjw@casema.nl>
Lines: 78
NNTP-Posting-Host: 83.83.6.100
X-Trace: sv3-pzevSzjnB+QYhhDtiE1PYOe2HIjhYTKcZYtWqDV/RMi0Z7F0praAaQEP0+zhlAMWNluTtQVNGY0+VVX!UcGZuIXDpvFkaXp561iGO7MtS40CEheMlrKckMD0/HCP1a5dvwt+lUSxLs03LJQ521Q=
X-Complaints-To: abuse@casema.nl
X-DMCA-Complaints-To: abuse@casema.nl
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32