Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Multics is security spelled sideways.


devel / comp.lang.c / Re: a little confuse about "format and convert" in printf function.

SubjectAuthor
* a little confuse about "format and convert" in printf function.hieu hot xac
+- Re: a little confuse about "format and convert" in printf function.Bart
+* Re: a little confuse about "format and convert" in printf function.Barry Schwarz
|`- Re: a little confuse about "format and convert" in printf function.Tim Rentsch
`* Re: a little confuse about "format and convert" in printf function.Tim Rentsch
 `- Re: a little confuse about "format and convert" in printf function.Bart

1
a little confuse about "format and convert" in printf function.

<81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27511&group=comp.lang.c#27511

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:2c85:0:b0:76c:b12c:2fe8 with SMTP id s127-20020a372c85000000b0076cb12c2fe8mr62209qkh.13.1691842059117;
Sat, 12 Aug 2023 05:07:39 -0700 (PDT)
X-Received: by 2002:a17:903:32d2:b0:1b7:f443:c807 with SMTP id
i18-20020a17090332d200b001b7f443c807mr1849035plr.7.1691842058806; Sat, 12 Aug
2023 05:07:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 12 Aug 2023 05:07:38 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=171.250.162.116; posting-account=Dzc0KwoAAAD7Ns3VPtrLDscna7QunECQ
NNTP-Posting-Host: 171.250.162.116
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
Subject: a little confuse about "format and convert" in printf function.
From: hieuhot...@gmail.com (hieu hot xac)
Injection-Date: Sat, 12 Aug 2023 12:07:39 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1327
 by: hieu hot xac - Sat, 12 Aug 2023 12:07 UTC

printf ("Max value of unsigned char type is %u\n", (char)(~0));

Will print :
Max value of unsigned char type is 4294967295

printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));

Will print :
Max value of unsigned char type is 255

I don't understand why the former behaves like that.

Re: a little confuse about "format and convert" in printf function.

<ub7urs$1as6s$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27512&group=comp.lang.c#27512

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: a little confuse about "format and convert" in printf function.
Date: Sat, 12 Aug 2023 13:44:45 +0100
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <ub7urs$1as6s$1@dont-email.me>
References: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 12 Aug 2023 12:44:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8f8ff97ceeaa08bdbf0dfaaf05e17a26";
logging-data="1405148"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CC4VzAaKifkvQMdSVAvYu2az8a/ll0Fw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.14.0
Cancel-Lock: sha1:U4w0F5uHqPyngTxdIcjEsloLZ/U=
In-Reply-To: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
 by: Bart - Sat, 12 Aug 2023 12:44 UTC

On 12/08/2023 13:07, hieu hot xac wrote:
> printf ("Max value of unsigned char type is %u\n", (char)(~0));
>
> Will print :
> Max value of unsigned char type is 4294967295
>
> printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));
>
> Will print :
> Max value of unsigned char type is 255
>
> I don't understand why the former behaves like that.

The first casts 0xFFFFFFFF to char which is 0xFF, but then widens to the
/signed/ 8-bit result (as it usually is) to a full int, so 0xFFFFFFFF,
which is the result printed using %u.

The second casts 0xFFFFFFFF to 0xFF, but this now widens the
/unsigned/result to a full int, which is 0x000000FF.

Re: a little confuse about "format and convert" in printf function.

<ps9fdilcip9tl0otdjdc50da1mbgppgvk3@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27515&group=comp.lang.c#27515

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: a little confuse about "format and convert" in printf function.
Date: Sat, 12 Aug 2023 08:44:29 -0700
Message-ID: <ps9fdilcip9tl0otdjdc50da1mbgppgvk3@4ax.com>
References: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
X-Newsreader: Forte Agent 4.2/32.1118
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 26
Organization: SunSITE.dk - Supporting Open source
NNTP-Posting-Host: d2798a08.news.sunsite.dk
X-Trace: 1691855073 news.sunsite.dk 703 schwarzb@q.com/97.126.69.237:64402
X-Complaints-To: staff@sunsite.dk
 by: Barry Schwarz - Sat, 12 Aug 2023 15:44 UTC

On Sat, 12 Aug 2023 05:07:38 -0700 (PDT), hieu hot xac
<hieuhotxac@gmail.com> wrote:

>printf ("Max value of unsigned char type is %u\n", (char)(~0));
>
>Will print :
>Max value of unsigned char type is 4294967295
>
>printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));
>
>Will print :
>Max value of unsigned char type is 255
>
>I don't understand why the former behaves like that.

One thing that may help your understanding: printf is a variadic
function. Any argument of an integer type with a rank less than int
will be promoted to int or unsigned int, as appropriate. On most home
systems, char is signed. So the argument is promoted to int.
The second example has type unsigned char which is promoted to
unsigned int.

By the way, sending a signed int for %u is a violation.

--
Remove del for email

Re: a little confuse about "format and convert" in printf function.

<86il9k84km.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27520&group=comp.lang.c#27520

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: a little confuse about "format and convert" in printf function.
Date: Sat, 12 Aug 2023 12:04:57 -0700
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <86il9k84km.fsf@linuxsc.com>
References: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="744b704e16faf359564fcd4001f85eba";
logging-data="1554083"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+f0OXOrssdi8EYJIdtRr5jkviF52so7kM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:j0A98Qva1CBUUc5sDmRkzpIyFD8=
sha1:R+bVi02VruE05essvjyLKBeduqU=
 by: Tim Rentsch - Sat, 12 Aug 2023 19:04 UTC

hieu hot xac <hieuhotxac@gmail.com> writes:

> printf ("Max value of unsigned char type is %u\n", (char)(~0));
>
> Will print :
> Max value of unsigned char type is 4294967295
>
> printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));
>
> Will print :
> Max value of unsigned char type is 255
>
> I don't understand why the former behaves like that.

The expression 0 has type int.

The expression ~0 also has type int. Most likely you
are running on a two's complement machine, where ~0
has the value -1 (also with type int).

Converting -1 to type unsigned char gives the maximum
value for unsigned char, which on an 8-bit machine is
255.

That argument has type unsigned char, but because it is
being passed as a variadic argument it gets promoted
to type int. The int argument is read as type unsigned,
because of the %u format specifier, but that's okay in
this case because the value falls in the range that is
common to the types int and unsigned int. Result: 255.

In the first case, ~0 still has type int with value -1,
which when converted to char is still -1 when the type
char is a signed type, which apparently it is in your
environment.

The value -1 with type char is promoted to int, and so
still has the value -1, but now with type int.

The -1 argument of type int is read as an unsigned int
because of the %u specifier. Technically that results
in undefined behavior, because -1 is NOT in the common
range of int and unsigned int. In practice what results
is the int bits (all ones) are reinterpreted as an
unsigned int, where all ones gives the maximum value for
type unsigned int. Result: 4294967295.

Does all that make sense?

By the way, it would be better to ignore the response
that was posted by Bart, it has errors in it.

Re: a little confuse about "format and convert" in printf function.

<86edk884a1.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27521&group=comp.lang.c#27521

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: a little confuse about "format and convert" in printf function.
Date: Sat, 12 Aug 2023 12:11:18 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <86edk884a1.fsf@linuxsc.com>
References: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com> <ps9fdilcip9tl0otdjdc50da1mbgppgvk3@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="744b704e16faf359564fcd4001f85eba";
logging-data="1554083"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+SNJGCPadvvjOZXUhMwCR/mBxKrNmO3U="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:0fq+MJ+GEHM9CIJtoLA5IN0ogpA=
sha1:inwimfg31nqFjh10+1xwGgX02uQ=
 by: Tim Rentsch - Sat, 12 Aug 2023 19:11 UTC

Barry Schwarz <schwarzb@delq.com> writes:

> On Sat, 12 Aug 2023 05:07:38 -0700 (PDT), hieu hot xac
> <hieuhotxac@gmail.com> wrote:
>
>> printf ("Max value of unsigned char type is %u\n", (char)(~0));
>>
>> Will print :
>> Max value of unsigned char type is 4294967295
>>
>> printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));
>>
>> Will print :
>> Max value of unsigned char type is 255
>>
>> I don't understand why the former behaves like that.
>
> One thing that may help your understanding: printf is a variadic
> function. Any argument of an integer type with a rank less than int
> will be promoted to int or unsigned int, as appropriate. On most home
> systems, char is signed. So the argument is promoted to int.

Right.

> The second example has type unsigned char which is promoted to
> unsigned int.

Because unsigned char only goes up to 255 here, the argument
with type unsigned char gets promoted to type int, not to
type unsigned int.

> By the way, sending a signed int for %u is a violation.

It can be but it isn't always. In particular, giving an int
argument whose value is greater than or equal to zero is always
okay to give to printf() for a %u conversion sequence. It is
only when the int argument has a value less than zero that it
is (technically) undefined behavior for %u.

Re: a little confuse about "format and convert" in printf function.

<ub8p5e$1fv77$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=27523&group=comp.lang.c#27523

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: a little confuse about "format and convert" in printf function.
Date: Sat, 12 Aug 2023 21:13:35 +0100
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <ub8p5e$1fv77$1@dont-email.me>
References: <81f5a22c-ed13-4f9d-addf-32a78f5a608dn@googlegroups.com>
<86il9k84km.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 12 Aug 2023 20:13:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8f8ff97ceeaa08bdbf0dfaaf05e17a26";
logging-data="1572071"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18U/ASsdWPQ7zpcVhYDOMBCWjpcVKqjlKg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.14.0
Cancel-Lock: sha1:QeylEbj30zeJV94YzoCx47HXRd8=
In-Reply-To: <86il9k84km.fsf@linuxsc.com>
 by: Bart - Sat, 12 Aug 2023 20:13 UTC

On 12/08/2023 20:04, Tim Rentsch wrote:
> hieu hot xac <hieuhotxac@gmail.com> writes:
>
>> printf ("Max value of unsigned char type is %u\n", (char)(~0));
>>
>> Will print :
>> Max value of unsigned char type is 4294967295
>>
>> printf ("Max value of unsigned char type is %u\n", (unsigned char)(~0));
>>
>> Will print :
>> Max value of unsigned char type is 255
>>
>> I don't understand why the former behaves like that.
>
> The expression 0 has type int.
>
> The expression ~0 also has type int. Most likely you
> are running on a two's complement machine, where ~0
> has the value -1 (also with type int).
>
> Converting -1 to type unsigned char gives the maximum
> value for unsigned char, which on an 8-bit machine is
> 255.
>
> That argument has type unsigned char, but because it is
> being passed as a variadic argument it gets promoted
> to type int. The int argument is read as type unsigned,
> because of the %u format specifier, but that's okay in
> this case because the value falls in the range that is
> common to the types int and unsigned int. Result: 255.
>
> In the first case, ~0 still has type int with value -1,
> which when converted to char is still -1 when the type
> char is a signed type, which apparently it is in your
> environment.
>
> The value -1 with type char is promoted to int, and so
> still has the value -1, but now with type int.
>
> The -1 argument of type int is read as an unsigned int
> because of the %u specifier. Technically that results
> in undefined behavior, because -1 is NOT in the common
> range of int and unsigned int. In practice what results
> is the int bits (all ones) are reinterpreted as an
> unsigned int, where all ones gives the maximum value for
> type unsigned int. Result: 4294967295.
>
> Does all that make sense?
>
> By the way, it would be better to ignore the response
> that was posted by Bart, it has errors in it.

What are they? On second thoughts, never mind.

Your long-winded explanation with the two cases in the wrong order is
definitely the best.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor