Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Your computer account is overdrawn. Please reauthorize.


devel / comp.lang.c / Beginner....Decimal/Octal converter help

SubjectAuthor
* Beginner....Decimal/Octal converter helpManyBeers
+- Re: Beginner....Decimal/Octal converter helpKeith Thompson
+- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
+- Re: Beginner....Decimal/Octal converter helpBarry Schwarz
+- Re: Beginner....Decimal/Octal converter helpJoe Pfeiffer
+* Re: Beginner....Decimal/Octal converter helpÖö Tiib
|`- Re: Beginner....Decimal/Octal converter helpKenny McCormack
+* Re: Beginner....Decimal/Octal converter helpTim Rentsch
|`* Re: Beginner....Decimal/Octal converter helpScott Lurndal
| +* Re: Beginner....Decimal/Octal converter helpDavid Brown
| |+* Re: Beginner....Decimal/Octal converter helpBart
| ||+- Re: Beginner....Decimal/Octal converter helpDavid Brown
| ||`* Re: Beginner....Decimal/Octal converter helpScott Lurndal
| || `* Re: Beginner....Decimal/Octal converter helpBart
| ||  `- Re: Beginner....Decimal/Octal converter helpDavid Brown
| |`* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | +* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | |`* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | +* Re: Beginner....Decimal/Octal converter helpBart
| | | |+- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | |`* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | +* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | |+* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||`* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | || +* Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | || |`* Re: Beginner....Decimal/Octal converter helpRichard Damon
| | | | || | +- Re: Beginner....Decimal/Octal converter helpScott Lurndal
| | | | || | `* Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | || |  `* Re: Beginner....Decimal/Octal converter helpRichard Damon
| | | | || |   `- Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | || `* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||  `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   +* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |`* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   | +- Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   | `* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  +* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |`* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  | +- Re: Beginner....Decimal/Octal converter helpAnton Shepelev
| | | | ||   |  | `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |  +* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  |  |`- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |  `* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |   +* Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||   |  |   |+* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  |   ||+* Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |  |   |||`* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  |   ||| +* Re: Beginner....Decimal/Octal converter helpScott Lurndal
| | | | ||   |  |   ||| |`- Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |  |   ||| +* Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |  |   ||| |`* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  |   ||| | `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |   ||| |  `- Re: Beginner....Decimal/Octal converter helpRichard Harnden
| | | | ||   |  |   ||| `* Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||   |  |   |||  `* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |   |||   `* Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |  |   |||    +- Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |   |||    +* Re: Beginner....Decimal/Octal converter helpBart
| | | | ||   |  |   |||    |`* Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||   |  |   |||    | +- Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |   |||    | `- Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | ||   |  |   |||    `- Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||   |  |   ||`* Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||   |  |   || `- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |   |`- Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |   `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |    `* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |  |     `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |      +- Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | ||   |  |      `* Re: Beginner....Decimal/Octal converter helpÖö Tiib
| | | | ||   |  |       `* Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  |        `* Re: Beginner....Decimal/Octal converter helpÖö Tiib
| | | | ||   |  |         `- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | ||   |  `* Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |   +* Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | | ||   |   |`* Re: Beginner....Decimal/Octal converter helpScott Lurndal
| | | | ||   |   | `- Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   |   `* Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | | | ||   |    `- Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | ||   `* Re: Beginner....Decimal/Octal converter helpScott Lurndal
| | | | ||    +- Re: Beginner....Decimal/Octal converter helpKaz Kylheku
| | | | ||    `- Re: Beginner....Decimal/Octal converter helpBen Bacarisse
| | | | |`- Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | | `* Re: Beginner....Decimal/Octal converter helpKeith Thompson
| | | |  `- Re: Beginner....Decimal/Octal converter helpDavid Brown
| | | `- Re: Beginner....Decimal/Octal converter helpTim Rentsch
| | `- Re: Beginner....Decimal/Octal converter helpTim Rentsch
| `* Re: Beginner....Decimal/Octal converter helpTim Rentsch
|  +- Neologism (Was: Beginner....Decimal/Octal converter help)Kenny McCormack
|  `* Re: Beginner....Decimal/Octal converter helpScott Lurndal
|   `- Re: Beginner....Decimal/Octal converter helpTim Rentsch
`* Re: Beginner....Decimal/Octal converter helpStefan Ram
 `* Re: Beginner....Decimal/Octal converter helpScott Lurndal
  `* Re: Beginner....Decimal/Octal converter helpTim Rentsch
   `* Re: Beginner....Decimal/Octal converter helpScott Lurndal
    `- Re: Beginner....Decimal/Octal converter helpTim Rentsch

Pages:1234
Beginner....Decimal/Octal converter help

<8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:15e7:b0:6cb:e3bc:5844 with SMTP id p7-20020a05620a15e700b006cbe3bc5844mr1232937qkm.774.1662662045324;
Thu, 08 Sep 2022 11:34:05 -0700 (PDT)
X-Received: by 2002:a05:6870:65a1:b0:127:663e:bc62 with SMTP id
fp33-20020a05687065a100b00127663ebc62mr2660675oab.145.1662662045069; Thu, 08
Sep 2022 11:34:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.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: Thu, 8 Sep 2022 11:34:04 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=76.72.148.11; posting-account=1rkDZAoAAAC_Az1Xgq5XNQjc17mh9pKh
NNTP-Posting-Host: 76.72.148.11
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
Subject: Beginner....Decimal/Octal converter help
From: markzu...@yahoo.com (ManyBeers)
Injection-Date: Thu, 08 Sep 2022 18:34:05 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 3312
 by: ManyBeers - Thu, 8 Sep 2022 18:34 UTC

Hello I am teaching myself C and I need some help with a problem.
After a search for dec/oct convert I found this code:
(295600127)
#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);
int main()
{ int decimalNumber;

printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

return 0;
}

int convertDecimalToOctal(int decimalNumber)
{ int octalNumber = 0, i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
} As you can see I posted it has limit of 295600127. why is that.
but what I really want is could somebody explain exactly what
the code is doing below the 1st... return 0;.... which I think is the
int convertDecimaltoOctal(int decimalNumber)function and it's
implementation.
I want to fix the program so it will do at least a full 32bitnumber (4294967295)
So here octalNumber += (decimalNumber % 8) * i;s how I see it: the code defined 2 variables octalnumber=0
and i=1 then it goes into a whileloop ...while decimalNumber is not 0...
do this:
octalNumber += (decimalNumber % 8) * i; modulus the decNum(say 1000) multiply that by i (which equals 1 for now) increment and then assign to
octNum? The doing something and then assigning is tricky. Anyways if you know how could you explain exactly what the function does and in which order.
This is my own oct/dec converter:
/*decimal_octal_converter*/

#include <stdio.h>
/*---------------*/

unsigned int dec, oct;

int main(void)

{
printf("Enter a number between 0 and 4294967295: ");
scanf ("%d", &dec);

printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d",
dec/(8*8*8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8),
dec%(8*8*8*8*8*8)/(8*8*8*8*8),
dec%(8*8*8*8*8)/(8*8*8*8),
dec%(8*8*8*8)/(8*8*8),
dec%(8*8*8)/(8*8),
dec%(8*8*8)%(8*8)/8, dec%8);

return 0;
}
it is the only way I could figure out how to do it.

Re: Beginner....Decimal/Octal converter help

<87leqtk83o.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Thu, 08 Sep 2022 11:56:59 -0700
Organization: None to speak of
Lines: 79
Message-ID: <87leqtk83o.fsf@nosuchdomain.example.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="77f516b5b1c83037fedc8e3f516da01c";
logging-data="840666"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/30M54GWotl2fPYzpedw8k"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:m9B/aPRmNiHkUaJxmc2ueIVl0uo=
sha1:wTaeZImXSk33/06mlcWjqkt0AUk=
 by: Keith Thompson - Thu, 8 Sep 2022 18:56 UTC

ManyBeers <markzuffi@yahoo.com> writes:
> Hello I am teaching myself C and I need some help with a problem.
> After a search for dec/oct convert I found this code:
> (295600127)
> #include <stdio.h>
> #include <math.h>
>
> int convertDecimalToOctal(int decimalNumber);
> int main()
> {
> int decimalNumber;
>
> printf("Enter a decimal number: ");
> scanf("%d", &decimalNumber);
>
> printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));
>
> return 0;
> }
>
> int convertDecimalToOctal(int decimalNumber)
> {
> int octalNumber = 0, i = 1;
>
> while (decimalNumber != 0)
> {
> octalNumber += (decimalNumber % 8) * i;
> decimalNumber /= 8;
> i *= 10;
> }
>
> return octalNumber;
> }

The `#include <math.h>` is unnecessary. The program doesn't use any
functions that are declared in that header.

The program does a very strange kind of conversion. Integers are stored
in *binary*, and may be *displayed* in decimal, octal, or hexadecimal
(or any other base if you write the code to do it).

This convertDecimalToOctal function takes an integer argument (which,
again, is represented in binary) and returns an integer whose decimal
representation is the same as the octal representation of the argument.

In general, that's really not a useful thing to do. The resulting value
is not meaningful.

Decimal and octal representations are *strings*, not integers. Take a
look at the printf call in main():

printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

The 'd' in "%d" stands for "decimal".

A more useful thing to do is to create a *string" containing the octal
representation of the argument. And printf's "%o" format already does
exactly that. You can use sprintf or snprintf to store the result in a
string.

Or you can write your own function to generate an octal string.
(Returning strings from C functions is tricky. See
https://www.c-faq.com/ .)

> As you can see I posted it has limit of 295600127. why is that.

int is apparently 32 bits on your system (that's very common). The
maximum 32-bit signed integer value is 2147483647.

The function returns 2147477777 for an argument of 295600127. That's
very close to the maximum 32-bit signed integer value, which is 2**31-1
or 2147483647.

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Beginner....Decimal/Octal converter help

<875yhx65ec.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Thu, 08 Sep 2022 20:19:07 +0100
Organization: A noiseless patient Spider
Lines: 142
Message-ID: <875yhx65ec.fsf@bsb.me.uk>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="002e938bc6c483d125698f0bc1bdeda3";
logging-data="848914"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QeuiZaZkNpzZAxCUJS3wCJqFi/zfnKUU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:sYuOncALAm5krx5qOH5IB+o8Ryg=
sha1:drpzknAmNdhkphAKrk4dRX3xVwM=
X-BSB-Auth: 1.b924c078732ab37b1f30.20220908201907BST.875yhx65ec.fsf@bsb.me.uk
 by: Ben Bacarisse - Thu, 8 Sep 2022 19:19 UTC

ManyBeers <markzuffi@yahoo.com> writes:

> Hello I am teaching myself C and I need some help with a problem.

I think (though I am not sure) that you are also teaching yourself
programming at the same time. That makes it hard. First, because most
of the problems will to do with how you write code -- any code -- but
your be fooled into thinking the problem is you don't know C. Second, C
is a rather fussy language for a beginner to learn. It's small (that's
good) but it is full of odd corners. Third, there are a boat load of
very bad tutorials out there that use C. Don't know why, but the
language seems to attract them.

> After a search for dec/oct convert I found this code:
> (295600127)
> #include <stdio.h>
> #include <math.h>
>
> int convertDecimalToOctal(int decimalNumber);
> int main()
> {
> int decimalNumber;

The number is just a number. It's not decimal and it's not octal. It
may be binary inside the machine, but C lets you treat it like a number.

> printf("Enter a decimal number: ");

This message is OK because you are asking for a number to entered using
decimal digits.

> scanf("%d", &decimalNumber);

Always check that input has been successful. It's one of the most
important habits to get into.

> printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));
>
> return 0;
> }

printf can do what you want:

int main(void)
{ int number;
printf("Enter a decimal number: ");
if (scanf("%d", &number) == 1)
printf("%d in decimal = %d in octal = %o\n", number, (unsigned)number);
return 0;
}

And here we hit one of C's corners. The %o format expected an unsigned
int.

> int convertDecimalToOctal(int decimalNumber)
> {
> int octalNumber = 0, i = 1;
>
> while (decimalNumber != 0)
> {
> octalNumber += (decimalNumber % 8) * i;
> decimalNumber /= 8;
> i *= 10;
> }
>
> return octalNumber;
> }

This is weird. Decimal and octal are about representations so one would
expect a function like this generate a string of digits. What this is
doing is computing a number (note neither decimal nor octal, just a
number) that when printed as a decimal, shows the octal digits that
correspond to the original number.

This is a bad idea for all sorts of reasons. I'll go into them if you
want to know, but it's best to forget you ever saw this function!

> As you can see I posted it has limit of 295600127. why is that.

That's one of the problems. The number which, when printed as a
decimal, shows the digits of the original in octal has to be bigger than
than the original.

> but what I really want is could somebody explain exactly what
> the code is doing below the 1st... return 0;.... which I think is the
> int convertDecimaltoOctal(int decimalNumber)function and it's
> implementation.
> I want to fix the program so it will do at least a full 32bitnumber (4294967295)
> So here octalNumber += (decimalNumber % 8) * i;s how I see it: the code defined 2 variables octalnumber=0
> and i=1 then it goes into a whileloop ...while decimalNumber is not 0...
> do this:
> octalNumber += (decimalNumber % 8) * i; modulus the decNum(say 1000) multiply that by i (which equals 1 for now) increment and then assign to
> octNum? The doing something and then assigning is tricky. Anyways if
> you know how could you explain exactly what the function does and in
> which order.

Ditch the whole idea of this function. Decimal and octal are about
representations as strings of digits. If you don't want to use %o, the
function you need should write digits into a string buffer:

void octal_digits(int n, char *buf, size_t len);

You could, of course, return an allocated string, but already you are
hitting C issues that have nothing to do with programming.

> This is my own oct/dec converter:
> /*decimal_octal_converter*/
>
> #include <stdio.h>
> /*---------------*/
>
> unsigned int dec, oct;
>
> int main(void)
>
> {
> printf("Enter a number between 0 and 4294967295: ");
> scanf ("%d", &dec);
>
>
> printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d",
> dec/(8*8*8*8*8*8*8*8*8*8),
> dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8),
> dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8),
> dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8),
> dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8),
> dec%(8*8*8*8*8*8)/(8*8*8*8*8),
> dec%(8*8*8*8*8)/(8*8*8*8),
> dec%(8*8*8*8)/(8*8*8),
> dec%(8*8*8)/(8*8),
> dec%(8*8*8)%(8*8)/8, dec%8);
>
> return 0;
> }
> it is the only way I could figure out how to do it.

You need exercises to learn about recursive functions and/or loops.
What resources are you using to learn?

--
Ben.

Re: Beginner....Decimal/Octal converter help

<7lekhht1hn819db1q906nqlcc1qkjq18pm@4ax.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: schwa...@delq.com (Barry Schwarz)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Thu, 08 Sep 2022 12:34:44 -0700
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <7lekhht1hn819db1q906nqlcc1qkjq18pm@4ax.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Info: reader01.eternal-september.org; posting-host="8954d75c0b3a3bd1ea0737316411f2e7";
logging-data="851869"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187Ids25efdtjjrBThOWiSRAfsUlXZYlFw="
Cancel-Lock: sha1:WJmKewvftH7LQSlBnyvx0T8AEOw=
X-Newsreader: Forte Agent 4.2/32.1118
 by: Barry Schwarz - Thu, 8 Sep 2022 19:34 UTC

On Thu, 8 Sep 2022 11:34:04 -0700 (PDT), ManyBeers
<markzuffi@yahoo.com> wrote:

>Hello I am teaching myself C and I need some help with a problem.
>After a search for dec/oct convert I found this code:
>(295600127)
>#include <stdio.h>
>#include <math.h>
>
>int convertDecimalToOctal(int decimalNumber);
>int main()
>{
> int decimalNumber;
>
> printf("Enter a decimal number: ");
> scanf("%d", &decimalNumber);
>
> printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));
>
> return 0;
>}
>
>int convertDecimalToOctal(int decimalNumber)
>{
> int octalNumber = 0, i = 1;
>
> while (decimalNumber != 0)
> {
> octalNumber += (decimalNumber % 8) * i;
> decimalNumber /= 8;
> i *= 10;
> }
>
> return octalNumber;
>}
>As you can see I posted it has limit of 295600127. why is that.

The program is storing the octal value in an int, not in a string of
characters that happen to be in range of 0 to 7. The octal value of
295600127 is 02147477777. Adding 1 to this value yields 02147500000
which is a perfectly valid string of characters. However, the decimal
value 2147500000 is too large to be expressed as a 32-bit signed int.

> but what I really want is could somebody explain exactly what
>the code is doing below the 1st... return 0;.... which I think is the
>int convertDecimaltoOctal(int decimalNumber)function and it's
>implementation.

Yes, that is exactly what it is. Take a piece of paper and play
computer. Pick a modest size initial value for decimalNumber and
compute the values of all the variables until the loop terminates.
Also read the Wikipedia article on the octal base numbering system.

>I want to fix the program so it will do at least a full 32bitnumber (4294967295)

The maximum 32 bit number has 9 decimal digits. The octal
representation has at least 10 octal digits. Since the function
treats the octal digits as decimal digits, you cannot fit 10 of them
in a 32 bit int.

There are at least two ways to do what you want:
1 - Change the function to use long long int instead of int.
2 - Change the function to store the octal digits as characters in
an array of characters.

Both options require changes to areas outside the function also.

<snip>

--
Remove del for email

Re: Beginner....Decimal/Octal converter help

<1billx2rwu.fsf@pfeifferfamily.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: pfeif...@cs.nmsu.edu (Joe Pfeiffer)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Thu, 08 Sep 2022 20:38:41 -0600
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <1billx2rwu.fsf@pfeifferfamily.net>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1f9765648cbec0599f46755c641aed8f";
logging-data="1007783"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Lczlr3SnZGG/V4RDMe7HDObz6Oajfyk0="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:Ol/u/Cy+Zm2gP10Mg3/ayZ5a964=
sha1:F6JiFukQfRnYgss51GGHqxNLG5Q=
 by: Joe Pfeiffer - Fri, 9 Sep 2022 02:38 UTC

This is a not-uncommon algorithm for performing decimal to octal
conversions by hand: it will convert a decimal number (like 20) to a
string of digits that is that looks like the same value in octal
(there's a lot of handwaving going on in that description). So if you
give it 20, it will give you back 24.

As others have pointed out, doing a straight transliteration of that
by-hand algorithm to C is really sort of nonsensical, because the number
you're looking at is really in the machine's binary internal storage
representation.

As for what it's doing... the basic idea is that the modulus
operator is being used to grab the rightmost octal digit from your
input, and then the multiplication by i (which is a power of ten) puts
it at the left end of the number you're creating. Dividing the input by
8 is getting rid of the least significant octal digit.

So look at an input of 20

20 % 8 is 4, so we've got the least significant digit.
20 / 8 is 2, so that's what we've got left from our input.

2 % 8 is 2; multiply that by ten and add to get 24
2 / 8 is 0, so we're done.

Input 20, output 24. 24 is a decimal number that has the same digits as
if it were the octal representation of 20.

But the best advice I can give: find another source for your coding
examples.

Re: Beginner....Decimal/Octal converter help

<d52d6c72-0ec1-49a5-a2c0-7a00b3f357fan@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:ef4f:0:b0:6cb:d294:3333 with SMTP id d76-20020ae9ef4f000000b006cbd2943333mr4434035qkg.511.1662718930857;
Fri, 09 Sep 2022 03:22:10 -0700 (PDT)
X-Received: by 2002:a05:6870:4413:b0:127:96e8:220 with SMTP id
u19-20020a056870441300b0012796e80220mr4366565oah.19.1662718930584; Fri, 09
Sep 2022 03:22:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 9 Sep 2022 03:22:10 -0700 (PDT)
In-Reply-To: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=84.50.190.130; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 84.50.190.130
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d52d6c72-0ec1-49a5-a2c0-7a00b3f357fan@googlegroups.com>
Subject: Re: Beginner....Decimal/Octal converter help
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Fri, 09 Sep 2022 10:22:10 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 16
 by: Öö Tiib - Fri, 9 Sep 2022 10:22 UTC

On Thursday, 8 September 2022 at 21:34:12 UTC+3, ManyBeers wrote:
> Hello I am teaching myself C and I need some help with a problem.
> After a search for dec/oct convert I found this code:

You are deeply confused yourself and so are using confusing example.
The octal, decimal, hexadecimal and binary are textual representations
of number.

The int in C is however binary, not textual. It is always signed, always
required to consist of bytes and on vast majority of platforms it consists
of 4 bytes of 8 bits (so 32 bits) in two's complement representation in
little-endian order of bytes, but C standard is not requiring that.

So if you want to achieve that the binary bits of your type are laid out in
some different manner than these are in int on platform that you use
then you need to specify how. Bit-by-bit. Dim words "octal" and "decimal"
are from entirely other universe, from universe of texts.

Re: Beginner....Decimal/Octal converter help

<tfff51$18inq$1@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Fri, 9 Sep 2022 13:30:09 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tfff51$18inq$1@news.xmission.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <d52d6c72-0ec1-49a5-a2c0-7a00b3f357fan@googlegroups.com>
Injection-Date: Fri, 9 Sep 2022 13:30:09 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1329914"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Fri, 9 Sep 2022 13:30 UTC

In article <d52d6c72-0ec1-49a5-a2c0-7a00b3f357fan@googlegroups.com>,
Tiib <ootiib@hot.ee> wrote:
>On Thursday, 8 September 2022 at 21:34:12 UTC+3, ManyBeers wrote:
>> Hello I am teaching myself C and I need some help with a problem.
>> After a search for dec/oct convert I found this code:
>
>You are deeply confused yourself and so are using confusing example.
>The octal, decimal, hexadecimal and binary are textual representations
>of number.
>
>The int in C is however binary, not textual. It is always signed, always
>required to consist of bytes and on vast majority of platforms it consists
>of 4 bytes of 8 bits (so 32 bits) in two's complement representation in
>little-endian order of bytes, but C standard is not requiring that.
>
>So if you want to achieve that the binary bits of your type are laid out
>in some different manner than these are in int on platform that you use
>then you need to specify how. Bit-by-bit. Dim words "octal" and "decimal"
>are from entirely other universe, from universe of texts.

I love this post! So lyrical. So Shakespearian.

So other-worldly.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

Re: Beginner....Decimal/Octal converter help

<86leqsr091.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Fri, 09 Sep 2022 09:16:58 -0700
Organization: A noiseless patient Spider
Lines: 196
Message-ID: <86leqsr091.fsf@linuxsc.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="79e2a8cba320b635c1b4a8520dc5642b";
logging-data="1151435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/M5HGkVaqYCNGZTAoFeMROVmtaTpvW9II="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:4yZ8NeBIZPBh8btiXTWokJpwYw4=
sha1:D9BN/x+uo18qPnO/wWlYijY77P8=
 by: Tim Rentsch - Fri, 9 Sep 2022 16:16 UTC

ManyBeers <markzuffi@yahoo.com> writes:

> Hello I am teaching myself C and I need some help with a problem.
> After a search for dec/oct convert I found this code:
>
> [..code..]

I would like to offer a different kind of answer to your
questions. First a suggestion: for this kind of problem I think
you will find it helpful to use unsigned types exclusively. In
the code below we will need (besides a plain 'unsigned' here and
there) two types, one for 8-bit values and one for 32-bit values:

typedef unsigned char UC;
typedef unsigned int UI;

Next we need to talk about what you're doing, which is base
conversion. That is to say, you have a number in one base, and
you want to convert it to a different base. Properly speaking
the word digit usually means a decimal digit, or a value between
0 and 9 inclusive. However, we are working in various bases, so
I will use "digit" to mean a value in the appropriate range for
the base in question. Now, what is a number in base B? It is a
sequence of "digits" in base B, that is, values between 0 and
B-1. A natural way to represent a number in base B is as an
array of elements for the "digits", where each element can hold a
value in the relevant range. For bases up to 256, these elements
can be of type UC - an unsigned character can hold all values
between 0 and 255, so it can hold all values between 0 and 9 (for
decimal), or all values between 0 and 7 (for octal), or all
values between 0 and 15 (for hexadecimal), etc. Here are some
sample holders of numbers in different bases:

UC decimal_197[] = { 1, 9, 7 };
UC octal_1777[] = { 1, 7, 7, 7 };
UC hex_FF[] = { 15, 15 };

(Slight digression: Notice that there is nothing marking the
"end" of a number. To use these variables we need to know how
many "digits" are held, which is the same as the number of
elements in each array. There is a well-known idiom in C that
computes what this is, given the variable name. That idiom is
typically written as a macro, as for example

#define NUMBER_OF( a ) (sizeof (a) / sizeof *(a))

We will see uses of this macro in the code below.)

Okay, so we have some array variables holding numbers in
different bases. To make use of these numbers we need to convert
them into C's native numeric type, which here means the type UI.
What is a value of type UI? Because a UI has 32 bits, a variable
of type UI is a single "digit" in base 2**32, or 4294967296.
Here is a function to convert a base-10 number to a UI, which is
to say to a base-4294967296 number:

UI
base_10_to_base_4294967296( UC digits[], unsigned n ){
unsigned i;
UI r = 0;
for( i = 0; i < n; i++ ){
r = r * 10 + digits[i];
}
return r;
}

Code to convert a base-8 number to a UI is almost identical:

UI
base_8_to_base_4294967296( UC digits[], unsigned n ){
unsigned i;
UI r = 0;
for( i = 0; i < n; i++ ){
r = r * 8 + digits[i];
}
return r;
}

And it's easy to see how to generalize these functions into a
single function that will handle any base between 2 and 256:

UI
base_b_to_base_4294967296( unsigned b, UC digits[], unsigned n ){
unsigned i;
UI r = 0;
for( i = 0; i < n; i++ ){
r = r * b + digits[i];
}
return r;
}

Having written these, we are now ready to write a simple test
driver:

#include <stdio.h>

#define NUMBER_OF( a ) (sizeof (a) / sizeof *(a))

int
main(){
UC decimal_197[] = { 1, 9, 7 };
UC octal_1777[] = { 1, 7, 7, 7 };
UC hex_FF[] = { 15, 15 };

unsigned decimal_197_digits = NUMBER_OF( decimal_197 );
unsigned octal_1777_digits = NUMBER_OF( octal_1777 );
unsigned hex_FF_digits = NUMBER_OF( hex_FF );

UI u197d = base_10_to_base_4294967296( decimal_197, decimal_197_digits );
UI u1777o = base_8_to_base_4294967296( octal_1777, octal_1777_digits );
UI uFFh = base_b_to_base_4294967296( 16, hex_FF, hex_FF_digits );

printf( " u197d is %7u decimal\n", u197d );
printf( "u1777o is %7u decimal\n", u1777o );
printf( " uFFh is %7u decimal\n", uFFh );
printf( "\n" );

printf( " u197d is %7o octal\n", u197d );
printf( "u1777o is %7o octal\n", u1777o );
printf( " uFFh is %7o octal\n", uFFh );
printf( "\n" );

printf( " u197d is %7x hexidecimal\n", u197d );
printf( "u1777o is %7x hexidecimal\n", u1777o );
printf( " uFFh is %7x hexidecimal\n", uFFh );
printf( "\n" );

return 0;
}

Notice that the UI variables are not inherently either decimal,
octal, or hexadecimal. The printed values come out as decimal,
octal, or hexadecimal, by virtue of what conversion specifier
(u, o, or x) is used in the printf() format string. You see
that?

Now for the next step. The code above allows conversion from an
arbitrary base (up to 256) to the base of C's native unsigned
type. What we're looking for is a function that goes the other
direction: from the base of C's native unsigned type to an array
of "digits" in a smaller base. This function needs different
interface. In particular, it should take a parameter that is a
pointer to an array of UC, and should return a count of the
number of digits in the result. Here is our first try:

unsigned
base_4294967296_to_base_b_X( UI value, unsigned b, UC out[] ){
UI v = value;
unsigned k = 0;

do {
out[k] = v % b;
k++;
} while( v /= b, v != 0 );

return k;
}

This function sort of works, but it has a problem: the generated
"digits" are backwards! An annoying artifact of conversions like
this is they really want to start at "the wrong end".

There are various ways to correct this problem, but probably the
easiest to explain is to put digits into a temporary array, and
then copy them in reverse order to the out[] array. And so,

unsigned
base_4294967296_to_base_b( UI value, unsigned b, UC out[] ){
UI v = value;
unsigned k = 0;
UC digits[100];
unsigned i;

do {
digits[k] = v % b;
k++;
} while( v /= b, v != 0 );

for( i = 0; i < k; i++ ){
out[ i ] = digits[ k-1-i ];
}

return k;
}

Please take a look over the above code and explanations, and see
if things make more sense now. Are there any questions you have?

One more note, on a more advanced topic. In the code above we
have limited the base-4294967296 numbers to only one UI, which is
to say limited those numbers to only one "digit". If we want to
represent very large numbers, we can have an _array_ of UI, with
each element of the array holding a single base-4294967296 digit.
Doing that is possible of course, but it's a lot harder, so while
you're still learning C you might want to stick with the simpler
cases explained above.

Re: Beginner....Decimal/Octal converter help

<S8MSK.400474$iiS8.244727@fx17.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx17.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Beginner....Decimal/Octal converter help
Newsgroups: comp.lang.c
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <86leqsr091.fsf@linuxsc.com>
Lines: 19
Message-ID: <S8MSK.400474$iiS8.244727@fx17.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 09 Sep 2022 19:06:26 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 09 Sep 2022 19:06:26 GMT
X-Received-Bytes: 1353
 by: Scott Lurndal - Fri, 9 Sep 2022 19:06 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>ManyBeers <markzuffi@yahoo.com> writes:
>
>> Hello I am teaching myself C and I need some help with a problem.
>> After a search for dec/oct convert I found this code:
>>
>> [..code..]
>
>I would like to offer a different kind of answer to your
>questions. First a suggestion: for this kind of problem I think
>you will find it helpful to use unsigned types exclusively. In
>the code below we will need (besides a plain 'unsigned' here and
>there) two types, one for 8-bit values and one for 32-bit values:
>
> typedef unsigned char UC;
> typedef unsigned int UI;

There already standard perfectly cromulent typedefs for both,
uint8_t and uint32_t.

Re: Beginner....Decimal/Octal converter help

<base-20220909233040@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: 9 Sep 2022 22:37:18 GMT
Organization: Stefan Ram
Lines: 59
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <base-20220909233040@ram.dialup.fu-berlin.de>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de LPweguvlso/NahvbN5P/WA3N4gZgQ4Q8wSskczXw5gfE93
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: de-DE
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 9 Sep 2022 22:37 UTC

ManyBeers <markzuffi@yahoo.com> writes:
>int convertDecimalToOctal(int decimalNumber);

The following program tries to convert the number given in main
to its octal representation, which is printed to standard output.

I wrote it just to the point where it seems to work for this
special case, but I'm not convinced that it will print the
correct result in all cases or does not overflow any of its
buffers!

#include <stdio.h>
#include <string.h>

/* are there digits above 0 in the buffer? */
int digits_in( char buffer[] )
{ size_t i = 0;
for( ; i < strlen( buffer )&& buffer[ i ]=='0'; ++i );
return buffer[ i ]> '0' && buffer[ i ]<= '9'; }

/* write div to "result" and returns the remainder */
int divrem
( char dividend[], char result[], int base_of_dividend, int divisor )
{ { size_t i = 0;
for( ; i < strlen( dividend ); ++i )
result[ i ]= ' '; result[ i ]= 0; }
int r = 0; int j = 0; int k = 0;
for( size_t i = 0; i < strlen( dividend ); ++i )
{ if( dividend[ i ]!= ' ' )
{ r = r * base_of_dividend + dividend[ i ]- '0';
if( r >= divisor )
{ int const q = r/divisor; r = r%8;
result[ j ]=( char )( q + '0' ); }
else
{ result[ j ]='0'; }} ++j; }
return r; }

/* convert number, which is given in the base_of_dividend into
base_of_result */
void convert
( char number[], char buffer[],
int base_of_dividend,
int base_of_result /* must not be larger than base_of_dividend */ )
{ if( digits_in( number ))
{ int const digit =
divrem( number, buffer, base_of_dividend, base_of_result );
{ convert( buffer, number, base_of_dividend, base_of_result );
putchar( '0' + digit ); }}}

int main( void )
{ /* source in dec */
char a[] = "897528309349537459261193475234783479487034102857389475930";
/* the buffer must at least have a's len */
char b[] = " ";
convert( a, b, 10, 8 );
putchar( '\n' ); }

Re: Beginner....Decimal/Octal converter help

<cIPSK.45075$SMP5.39069@fx05.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx05.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Beginner....Decimal/Octal converter help
Newsgroups: comp.lang.c
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <base-20220909233040@ram.dialup.fu-berlin.de>
Lines: 93
Message-ID: <cIPSK.45075$SMP5.39069@fx05.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 09 Sep 2022 23:08:56 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 09 Sep 2022 23:08:56 GMT
X-Received-Bytes: 3375
 by: Scott Lurndal - Fri, 9 Sep 2022 23:08 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>ManyBeers <markzuffi@yahoo.com> writes:
>>int convertDecimalToOctal(int decimalNumber);
>
> The following program tries to convert the number given in main
> to its octal representation, which is printed to standard output.
>
> I wrote it just to the point where it seems to work for this
> special case, but I'm not convinced that it will print the
> correct result in all cases or does not overflow any of its
> buffers!
>
>#include <stdio.h>
>#include <string.h>
>
>/* are there digits above 0 in the buffer? */
>int digits_in( char buffer[] )
>{ size_t i = 0;
> for( ; i < strlen( buffer )&& buffer[ i ]=='0'; ++i );
> return buffer[ i ]> '0' && buffer[ i ]<= '9'; }
>
>/* write div to "result" and returns the remainder */
>int divrem
>( char dividend[], char result[], int base_of_dividend, int divisor )
>{ { size_t i = 0;
> for( ; i < strlen( dividend ); ++i )
> result[ i ]= ' '; result[ i ]= 0; }
> int r = 0; int j = 0; int k = 0;
> for( size_t i = 0; i < strlen( dividend ); ++i )
> { if( dividend[ i ]!= ' ' )
> { r = r * base_of_dividend + dividend[ i ]- '0';
> if( r >= divisor )
> { int const q = r/divisor; r = r%8;
> result[ j ]=( char )( q + '0' ); }
> else
> { result[ j ]='0'; }} ++j; }
> return r; }
>
>/* convert number, which is given in the base_of_dividend into
>base_of_result */
>void convert
>( char number[], char buffer[],
> int base_of_dividend,
> int base_of_result /* must not be larger than base_of_dividend */ )
>{ if( digits_in( number ))
> { int const digit =
> divrem( number, buffer, base_of_dividend, base_of_result );
> { convert( buffer, number, base_of_dividend, base_of_result );
> putchar( '0' + digit ); }}}
>
>int main( void )
>{ /* source in dec */
> char a[] = "897528309349537459261193475234783479487034102857389475930";
> /* the buffer must at least have a's len */
> char b[] = " ";
> convert( a, b, 10, 8 );
> putchar( '\n' ); }
>
>

Somewhat more concisely:

$ cat /tmp/octal.c
#include <stdint.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, const char **argv, const char **envp)
{ char buf[64];
char *bp = &buf[64];
uint64_t value;

if (argc > 1) value = strtoul(argv[1], NULL, 0);
else value = 0x400;

*--bp = '\0';
while (value > 0) {
*--bp = '0' + (value & 7ul);
value /= 8;
}

printf("Octal = %s\n", bp);
return 0;
}

$ cc -o /tmp/octal /tmp/octal.c
$ /tmp/octal 400
Octal = 620
$ /tmp/octal 1024
Octal = 2000

Re: Beginner....Decimal/Octal converter help

<tfhn50$1c35d$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 11:58:55 +0200
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <tfhn50$1c35d$1@dont-email.me>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Sep 2022 09:58:56 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="01e4e1f77e645049f65ad981f1086b11";
logging-data="1445037"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+o3k1PTWVQWeIcZU1o9AjCZlbLaKkyLxk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:pM3DocvGE/S4eUiBvXwytsRrVdg=
Content-Language: en-GB
In-Reply-To: <S8MSK.400474$iiS8.244727@fx17.iad>
 by: David Brown - Sat, 10 Sep 2022 09:58 UTC

On 09/09/2022 21:06, Scott Lurndal wrote:
> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>> ManyBeers <markzuffi@yahoo.com> writes:
>>
>>> Hello I am teaching myself C and I need some help with a problem.
>>> After a search for dec/oct convert I found this code:
>>>
>>> [..code..]
>>
>> I would like to offer a different kind of answer to your
>> questions. First a suggestion: for this kind of problem I think
>> you will find it helpful to use unsigned types exclusively. In
>> the code below we will need (besides a plain 'unsigned' here and
>> there) two types, one for 8-bit values and one for 32-bit values:
>>
>> typedef unsigned char UC;
>> typedef unsigned int UI;
>
> There already standard perfectly cromulent typedefs for both,
> uint8_t and uint32_t.

And those type names are better in a number of ways. They are clearer,
more precise, and hide the design flaw in C of mixing the concepts of
"character" and "small number". In particular, they specify exactly
what size your numbers are, rather that having hidden assumptions in the
code.

The rest of Tim's post goes downhill rapidly from there. When you need
a calculator to figure out the name of your functions, you have /really/
got things wrong.

Re: Beginner....Decimal/Octal converter help

<tfhpcn$1bb0$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 11:37:12 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tfhpcn$1bb0$1@gioia.aioe.org>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="44384"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.13.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Sat, 10 Sep 2022 10:37 UTC

On 10/09/2022 10:58, David Brown wrote:
> On 09/09/2022 21:06, Scott Lurndal wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>
>>>> Hello I am teaching myself C and I need some help with a problem.
>>>> After a search for dec/oct convert I found this code:
>>>>
>>>> [..code..]
>>>
>>> I would like to offer a different kind of answer to your
>>> questions.  First a suggestion:  for this kind of problem I think
>>> you will find it helpful to use unsigned types exclusively.  In
>>> the code below we will need (besides a plain 'unsigned' here and
>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>
>>>   typedef unsigned char  UC;
>>>   typedef unsigned int   UI;
>>
>> There already standard perfectly cromulent typedefs for both,
>> uint8_t and uint32_t.
>
> And those type names are better in a number of ways.  They are clearer,
> more precise, and hide the design flaw in C

(What, C has design flaws?)

> of mixing the concepts of
> "character" and "small number".  In particular, they specify exactly
> what size your numbers are, rather that having hidden assumptions in the
> code.

I don't care for UC and UI, but I dislike those uint8_t forms so much
that when writing any C code, I much prefer to type:

typedef long long int i64;
typedef unsigned char byte;

then using the more succinct 'i64/byte', than having to write
'int64_t/'uint8_8' in 100 different places. (That was an actual typo; I
left it in to help explain why.)

I don't even want to write them once using this:

#include <stdint.h>
typedef int64_t i64;
typedef uint8_t byte;

> The rest of Tim's post goes downhill rapidly from there.  When you need
> a calculator to figure out the name of your functions, you have /really/
> got things wrong.

Yeah, you just don't hardcode just constants at multiple places in the code.

One of my programs uses base-1000000000, but you won't see it as a
function name, it is defined once at the top.

Re: Beginner....Decimal/Octal converter help

<tfhr2e$1chu8$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 13:05:49 +0200
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <tfhr2e$1chu8$1@dont-email.me>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <tfhpcn$1bb0$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 10 Sep 2022 11:05:50 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="01e4e1f77e645049f65ad981f1086b11";
logging-data="1460168"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ai3JszNtvaEPBYbPhdZ1xZ1pYNuz1Wks="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:PtXq1bjx8Pl0IOHYYH2IgUsgcJo=
In-Reply-To: <tfhpcn$1bb0$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Sat, 10 Sep 2022 11:05 UTC

On 10/09/2022 12:37, Bart wrote:
> On 10/09/2022 10:58, David Brown wrote:
>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>
>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>> After a search for dec/oct convert I found this code:
>>>>>
>>>>> [..code..]
>>>>
>>>> I would like to offer a different kind of answer to your
>>>> questions.  First a suggestion:  for this kind of problem I think
>>>> you will find it helpful to use unsigned types exclusively.  In
>>>> the code below we will need (besides a plain 'unsigned' here and
>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>
>>>>   typedef unsigned char  UC;
>>>>   typedef unsigned int   UI;
>>>
>>> There already standard perfectly cromulent typedefs for both,
>>> uint8_t and uint32_t.
>>
>> And those type names are better in a number of ways.  They are
>> clearer, more precise, and hide the design flaw in C
>
> (What, C has design flaws?)

Yes, haven't you noticed? :-) It's inevitable, of course, given the
difficulty they had in the 1970's of predicting the next 50 years of the
computing world.

>
>> of mixing the concepts of "character" and "small number".  In
>> particular, they specify exactly what size your numbers are, rather
>> that having hidden assumptions in the code.
>
> I don't care for UC and UI, but I dislike those uint8_t forms so much
> that when writing any C code, I much prefer to type:
>
>     typedef long long int i64;
>     typedef unsigned char byte;

I don't think a re-hash of this discussion helps anyone. Suffice to say
that whatever you may think of the standard names for fixed size integer
types, the fact that they are standard goes a very long way to trumping
any alternative name for the same purpose - especially for a beginner.
(However, your choices would be much better than Tim's.)

>
>> The rest of Tim's post goes downhill rapidly from there.  When you
>> need a calculator to figure out the name of your functions, you have
>> /really/ got things wrong.
>
> Yeah, you just don't hardcode just constants at multiple places in the
> code.
> > One of my programs uses base-1000000000, but you won't see it as a
> function name, it is defined once at the top.

Re: Beginner....Decimal/Octal converter help

<875yhv30nu.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 12:54:13 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <875yhv30nu.fsf@bsb.me.uk>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="778f9511127a53bb403b7e920face5eb";
logging-data="1467340"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UTz6P1gzeGS07fGVP0Sbn9eQavzcNDuw="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:ktmOWPSlXc0PfrgttK6QImYjuvA=
sha1:Gz72o917LZxyMawc29fWFSD/xOc=
X-BSB-Auth: 1.fe31f9b397c843e8ebeb.20220910125413BST.875yhv30nu.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 10 Sep 2022 11:54 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 09/09/2022 21:06, Scott Lurndal wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>
>>>> Hello I am teaching myself C and I need some help with a problem.
>>>> After a search for dec/oct convert I found this code:
>>>>
>>>> [..code..]
>>>
>>> I would like to offer a different kind of answer to your
>>> questions. First a suggestion: for this kind of problem I think
>>> you will find it helpful to use unsigned types exclusively. In
>>> the code below we will need (besides a plain 'unsigned' here and
>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>
>>> typedef unsigned char UC;
>>> typedef unsigned int UI;
>> There already standard perfectly cromulent typedefs for both,
>> uint8_t and uint32_t.
>
> And those type names are better in a number of ways. They are
> clearer, more precise, and hide the design flaw in C of mixing the
> concepts of "character" and "small number". In particular, they
> specify exactly what size your numbers are, rather that having hidden
> assumptions in the code.

But they both say too much. The 'source' integer size need not be 32
bits and, given that there are no other obvious requirements, unsigned
int seems to me to be the natural choice. And if the requirements were
that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
more precisely meets the need.

And the uint8_t is also unnecessarily specific. We do want to know that
digits up to 255 can be stored, but nothing goes wrong if the type is
wider. uint_least8_t says exactly what's wanted and no more.

Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
char is effectively uint_least8_t.

--
Ben.

Re: Beginner....Decimal/Octal converter help

<tfi6jk$1f9ct$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 16:22:44 +0200
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <tfi6jk$1f9ct$1@dont-email.me>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 10 Sep 2022 14:22:44 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="01e4e1f77e645049f65ad981f1086b11";
logging-data="1549725"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nClCCXVZILvo7GPnzacuCaYDB3DO5th4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:oySHSDzopsr/8hu0yDdMizF01Pk=
In-Reply-To: <875yhv30nu.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Sat, 10 Sep 2022 14:22 UTC

On 10/09/2022 13:54, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>
>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>> After a search for dec/oct convert I found this code:
>>>>>
>>>>> [..code..]
>>>>
>>>> I would like to offer a different kind of answer to your
>>>> questions. First a suggestion: for this kind of problem I think
>>>> you will find it helpful to use unsigned types exclusively. In
>>>> the code below we will need (besides a plain 'unsigned' here and
>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>
>>>> typedef unsigned char UC;
>>>> typedef unsigned int UI;
>>> There already standard perfectly cromulent typedefs for both,
>>> uint8_t and uint32_t.
>>
>> And those type names are better in a number of ways. They are
>> clearer, more precise, and hide the design flaw in C of mixing the
>> concepts of "character" and "small number". In particular, they
>> specify exactly what size your numbers are, rather that having hidden
>> assumptions in the code.
>
> But they both say too much. The 'source' integer size need not be 32
> bits and, given that there are no other obvious requirements, unsigned
> int seems to me to be the natural choice. And if the requirements were
> that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
> more precisely meets the need.

"unsigned int" might have been a reasonable choice, but Tim's code goes
on to rely on it being 32-bit. (Or to be more accurate, it /says/ it
relies on it being 32-bit.)

>
> And the uint8_t is also unnecessarily specific. We do want to know that
> digits up to 255 can be stored, but nothing goes wrong if the type is
> wider. uint_least8_t says exactly what's wanted and no more.
>
> Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
> char is effectively uint_least8_t.
>

There is such a thing as being /too/ portable. There are some
real-world C implementations that do not have uint8_t and/or uint32_t,
but the overlap in code between these niche systems and "normal" systems
is almost non-existent. So the use of "uint_least8_t", "uint_fast32_t"
and similar types is IMHO needless complication and verbosity, and
certainly not something beginners should need to bother about. You are,
of course, technically correct in everything you write here - but that
still does not make these types a better choice in the circumstances.
(Again, all in my humble opinion.)

An alternative approach would be to have typedefs that are descriptive
and application-specific, and which can be changed later to allow
different sizes to be converted. Especially in light of it being code
for someone relatively unfamiliar with the language, I'd err on the side
of longer names (and certainly not cryptic short abbreviations that are
not particularly relevant to the usage) :

typedef uint8_t digit_type;
typedef uint32_t number_type;

and perhaps :

static const number_type max_number = -1;

if it is needed in the code later.

I'd be quite happy with "unsigned char" and "unsigned int" for these
typedefs, if that is the programmer's preference. But I certainly would
not want the number 4294967296 as part of the function names!

Re: Beginner....Decimal/Octal converter help

<Rn1TK.148355$51Rb.66032@fx45.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx45.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Beginner....Decimal/Octal converter help
Newsgroups: comp.lang.c
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad> <tfhn50$1c35d$1@dont-email.me> <tfhpcn$1bb0$1@gioia.aioe.org>
Lines: 42
Message-ID: <Rn1TK.148355$51Rb.66032@fx45.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 10 Sep 2022 14:42:57 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 10 Sep 2022 14:42:57 GMT
X-Received-Bytes: 2316
 by: Scott Lurndal - Sat, 10 Sep 2022 14:42 UTC

Bart <bc@freeuk.com> writes:
>On 10/09/2022 10:58, David Brown wrote:
>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>
>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>> After a search for dec/oct convert I found this code:
>>>>>
>>>>> [..code..]
>>>>
>>>> I would like to offer a different kind of answer to your
>>>> questions.  First a suggestion:  for this kind of problem I think
>>>> you will find it helpful to use unsigned types exclusively.  In
>>>> the code below we will need (besides a plain 'unsigned' here and
>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>
>>>>   typedef unsigned char  UC;
>>>>   typedef unsigned int   UI;
>>>
>>> There already standard perfectly cromulent typedefs for both,
>>> uint8_t and uint32_t.
>>
>> And those type names are better in a number of ways.  They are clearer,
>> more precise, and hide the design flaw in C
>
>(What, C has design flaws?)
>
>> of mixing the concepts of
>> "character" and "small number".  In particular, they specify exactly
>> what size your numbers are, rather that having hidden assumptions in the
>> code.
>
>I don't care for UC and UI, but I dislike those uint8_t forms so much
>that when writing any C code, I much prefer to type:
>
> typedef long long int i64;
> typedef unsigned char byte;

That's fine when nobody else has to ever work on your source code.

Otherwise, use the standard types.

Re: Beginner....Decimal/Octal converter help

<tfi9df$7tj$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 16:10:40 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tfi9df$7tj$1@gioia.aioe.org>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <tfhpcn$1bb0$1@gioia.aioe.org>
<Rn1TK.148355$51Rb.66032@fx45.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="8115"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.13.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Sat, 10 Sep 2022 15:10 UTC

On 10/09/2022 15:42, Scott Lurndal wrote:
> Bart <bc@freeuk.com> writes:

>> I don't care for UC and UI, but I dislike those uint8_t forms so much
>> that when writing any C code, I much prefer to type:
>>
>> typedef long long int i64;
>> typedef unsigned char byte;
>
> That's fine when nobody else has to ever work on your source code.
>
> Otherwise, use the standard types.

Every other C library and application I used to look at defined its own
types. Even 20 years after C99.

Eg, SDL, GTK, OpenGL use Uint or GLint or Gluint.

SQLite uses sqlite_uint32.

BZip uses UInt32.

STB_image uses stbi__uint32, etc.

On reason might have been in case stdint.h was not available. But I also
believe the uint32_t style is unliked.

In either case, there is still a huge amount of C code about that
doesn't use those standard types.

Re: Beginner....Decimal/Octal converter help

<tfid60$1gv86$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 18:14:56 +0200
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <tfid60$1gv86$1@dont-email.me>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <tfhpcn$1bb0$1@gioia.aioe.org>
<Rn1TK.148355$51Rb.66032@fx45.iad> <tfi9df$7tj$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 10 Sep 2022 16:14:56 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="01e4e1f77e645049f65ad981f1086b11";
logging-data="1604870"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190QAsecXY6wfv2hq5uYgreZDuP9h+JNwk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:sUl25folJuuY8MSVYM8n5Si8xqk=
Content-Language: en-GB
In-Reply-To: <tfi9df$7tj$1@gioia.aioe.org>
 by: David Brown - Sat, 10 Sep 2022 16:14 UTC

On 10/09/2022 17:10, Bart wrote:
> On 10/09/2022 15:42, Scott Lurndal wrote:
>> Bart <bc@freeuk.com> writes:
>
>>> I don't care for UC and UI, but I dislike those uint8_t forms so much
>>> that when writing any C code, I much prefer to type:
>>>
>>>      typedef long long int i64;
>>>      typedef unsigned char byte;
>>
>> That's fine when nobody else has to ever work on your source code.
>>
>> Otherwise, use the standard types.
>
> Every other C library and application I used to look at defined its own
> types. Even 20 years after C99.
>
> Eg, SDL, GTK, OpenGL use Uint or GLint or Gluint.
>
> SQLite uses sqlite_uint32.
>
> BZip uses UInt32.
>
> STB_image uses stbi__uint32, etc.
>
> On reason might have been in case stdint.h was not available. But I also
> believe the uint32_t style is unliked.
>

It might well be the case that some people (other than you) dislike the
style "uint32_t". But you can be very sure that nobody uses
"sqlite_uint32" because they think it is neater or more convenient than
"uint32_t".

We may be 20 years after C99, but it took about 10 years for it to
become commonly supported and used. It's only just recently that the
Linux kernel has moved to a standard newer than C90 (plus gcc extensions).

> In either case, there is still a huge amount of C code about that
> doesn't use those standard types.

Yes. That doesn't mean it is remotely a good idea to make things worse
by adding your own versions that don't even have the advantage of being
sure there are no likely conflicts with other people's versions.

Remember, whether you like it or not - and many people, including me,
don't particularly like it - in an implementation with 32-bit "long",
you could choose either "int" or "long" for your "int32_t" type, and
they would be incompatible. So for libraries that had to work with
pre-C99 implementations, their best choice was library-specific sized
integer types. For code that can rely on at least C99 - that is, most C
code written today - anything other than the <stdint.h> types would be a
terrible idea.

It would have been far better if the <stdint.h> types had been
standardised from K&R days, but that ship has sailed long ago.

Re: Beginner....Decimal/Octal converter help

<87zgf714zc.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 19:03:51 +0100
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <87zgf714zc.fsf@bsb.me.uk>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk>
<tfi6jk$1f9ct$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="778f9511127a53bb403b7e920face5eb";
logging-data="1651723"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lb0elxsMfZk/NuxK7KukA+nMCinP8Z8U="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:kiseCilgte55tK3EJcJo59iHc6w=
sha1:uiVo8uHNv1AYo64oszvvJfhf+qo=
X-BSB-Auth: 1.b07e2e1b382ef59b7672.20220910190351BST.87zgf714zc.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 10 Sep 2022 18:03 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 10/09/2022 13:54, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>>
>>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>>> After a search for dec/oct convert I found this code:
>>>>>>
>>>>>> [..code..]
>>>>>
>>>>> I would like to offer a different kind of answer to your
>>>>> questions. First a suggestion: for this kind of problem I think
>>>>> you will find it helpful to use unsigned types exclusively. In
>>>>> the code below we will need (besides a plain 'unsigned' here and
>>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>>
>>>>> typedef unsigned char UC;
>>>>> typedef unsigned int UI;
>>>> There already standard perfectly cromulent typedefs for both,
>>>> uint8_t and uint32_t.
>>>
>>> And those type names are better in a number of ways. They are
>>> clearer, more precise, and hide the design flaw in C of mixing the
>>> concepts of "character" and "small number". In particular, they
>>> specify exactly what size your numbers are, rather that having hidden
>>> assumptions in the code.
>> But they both say too much. The 'source' integer size need not be 32
>> bits and, given that there are no other obvious requirements, unsigned
>> int seems to me to be the natural choice. And if the requirements were
>> that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
>> more precisely meets the need.
>
> "unsigned int" might have been a reasonable choice, but Tim's code
> goes on to rely on it being 32-bit. (Or to be more accurate, it
> /says/ it relies on it being 32-bit.)
>
>> And the uint8_t is also unnecessarily specific. We do want to know that
>> digits up to 255 can be stored, but nothing goes wrong if the type is
>> wider. uint_least8_t says exactly what's wanted and no more.
>> Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
>> char is effectively uint_least8_t.
>>
>
> There is such a thing as being /too/ portable.

I was not talking about portability. It's handy when types say what's
needed and what isn't. uint8_t says too much. uint_least8_t is better
because it says exactly what's needed and what isn't. The suggestion
had nothing to do with portability.

> There are some real-world C implementations that do not have uint8_t
> and/or uint32_t, but the overlap in code between these niche systems
> and "normal" systems is almost non-existent. So the use of
> "uint_least8_t", "uint_fast32_t" and similar types is IMHO needless
> complication and verbosity, and certainly not something beginners
> should need to bother about.

Really? That's a complication?

Being a beginners is another matter. We don't know what sort of
beginner this is (I think it was a drive-by anyway), but none of the
types is ideal for a beginner. In fact, I imagine that's partly what
motivated TR's choice.

> You are, of course, technically correct
> in everything you write here - but that still does not make these
> types a better choice in the circumstances. (Again, all in my humble
> opinion.)
>
> An alternative approach would be to have typedefs that are descriptive
> and application-specific, and which can be changed later to allow
> different sizes to be converted. Especially in light of it being code
> for someone relatively unfamiliar with the language, I'd err on the
> side of longer names (and certainly not cryptic short abbreviations
> that are not particularly relevant to the usage) :
>
> typedef uint8_t digit_type;
> typedef uint32_t number_type;

But then why not use the more accurate last8 (and least32) types? They
"complicate" just two lines of code and the verbosity criticism
vanishes.

--
Ben.

Re: Beginner....Decimal/Octal converter help

<tfil39$1h9u$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 19:30:02 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tfil39$1h9u$1@gioia.aioe.org>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk>
<tfi6jk$1f9ct$1@dont-email.me> <87zgf714zc.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="50494"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.13.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Sat, 10 Sep 2022 18:30 UTC

On 10/09/2022 19:03, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 10/09/2022 13:54, Ben Bacarisse wrote:
>>> David Brown <david.brown@hesbynett.no> writes:
>>>
>>>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>>>
>>>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>>>> After a search for dec/oct convert I found this code:
>>>>>>>
>>>>>>> [..code..]
>>>>>>
>>>>>> I would like to offer a different kind of answer to your
>>>>>> questions. First a suggestion: for this kind of problem I think
>>>>>> you will find it helpful to use unsigned types exclusively. In
>>>>>> the code below we will need (besides a plain 'unsigned' here and
>>>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>>>
>>>>>> typedef unsigned char UC;
>>>>>> typedef unsigned int UI;
>>>>> There already standard perfectly cromulent typedefs for both,
>>>>> uint8_t and uint32_t.
>>>>
>>>> And those type names are better in a number of ways. They are
>>>> clearer, more precise, and hide the design flaw in C of mixing the
>>>> concepts of "character" and "small number". In particular, they
>>>> specify exactly what size your numbers are, rather that having hidden
>>>> assumptions in the code.
>>> But they both say too much. The 'source' integer size need not be 32
>>> bits and, given that there are no other obvious requirements, unsigned
>>> int seems to me to be the natural choice. And if the requirements were
>>> that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
>>> more precisely meets the need.
>>
>> "unsigned int" might have been a reasonable choice, but Tim's code
>> goes on to rely on it being 32-bit. (Or to be more accurate, it
>> /says/ it relies on it being 32-bit.)
>>
>>> And the uint8_t is also unnecessarily specific. We do want to know that
>>> digits up to 255 can be stored, but nothing goes wrong if the type is
>>> wider. uint_least8_t says exactly what's wanted and no more.
>>> Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
>>> char is effectively uint_least8_t.
>>>
>>
>> There is such a thing as being /too/ portable.
>
> I was not talking about portability. It's handy when types say what's
> needed and what isn't. uint8_t says too much. uint_least8_t is better
> because it says exactly what's needed and what isn't. The suggestion
> had nothing to do with portability.

Surely uint_least8_t specifies even more?

That's not a type I've come across anywhere else. And I'd be interested
in which machine it would map to anything other than 'unsigned char' (as
it does on x64.

I can sort of see the use-case, but it sounds niche: you need a u8 type
but don't want to commit to exactly u8 because on some unusual
architecture you don't want it to jump through hoops to give you u8,
when u16 would do just as well. But specifying u16 anyway would be
wasteful everywhere else.

And then there's uint_fast8_t; I can't even make a guess at that. All I
knows is that the last thing I want to see in source code is
uint_least8_t and uint_fast8_t (maybe 'const' too!) cluttering things up
and obscuring the underlying code. uint8_t is bad enough.

I want to see something like 'byte' or 'u8'. For the unusual situation
above, deal with that in usercode via a special usertype.

Re: Beginner....Decimal/Octal converter help

<87tu5f12ye.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sat, 10 Sep 2022 19:47:37 +0100
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <87tu5f12ye.fsf@bsb.me.uk>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk>
<tfi6jk$1f9ct$1@dont-email.me> <87zgf714zc.fsf@bsb.me.uk>
<tfil39$1h9u$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="778f9511127a53bb403b7e920face5eb";
logging-data="1651723"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sYXA4NiW+2K8ZJCkCPgt9MtkGra6C65E="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:cz3vs2R82XqsU6C8rZlL9ol9UzM=
sha1:JE74PwOMYwVKeoWMLBr4+AotW4k=
X-BSB-Auth: 1.51124a30b33f1e37afea.20220910194737BST.87tu5f12ye.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 10 Sep 2022 18:47 UTC

Bart <bc@freeuk.com> writes:

> On 10/09/2022 19:03, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 10/09/2022 13:54, Ben Bacarisse wrote:
>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>
>>>>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>>>>
>>>>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>>>>> After a search for dec/oct convert I found this code:
>>>>>>>>
>>>>>>>> [..code..]
>>>>>>>
>>>>>>> I would like to offer a different kind of answer to your
>>>>>>> questions. First a suggestion: for this kind of problem I think
>>>>>>> you will find it helpful to use unsigned types exclusively. In
>>>>>>> the code below we will need (besides a plain 'unsigned' here and
>>>>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>>>>
>>>>>>> typedef unsigned char UC;
>>>>>>> typedef unsigned int UI;
>>>>>> There already standard perfectly cromulent typedefs for both,
>>>>>> uint8_t and uint32_t.
>>>>>
>>>>> And those type names are better in a number of ways. They are
>>>>> clearer, more precise, and hide the design flaw in C of mixing the
>>>>> concepts of "character" and "small number". In particular, they
>>>>> specify exactly what size your numbers are, rather that having hidden
>>>>> assumptions in the code.
>>>> But they both say too much. The 'source' integer size need not be 32
>>>> bits and, given that there are no other obvious requirements, unsigned
>>>> int seems to me to be the natural choice. And if the requirements were
>>>> that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
>>>> more precisely meets the need.
>>>
>>> "unsigned int" might have been a reasonable choice, but Tim's code
>>> goes on to rely on it being 32-bit. (Or to be more accurate, it
>>> /says/ it relies on it being 32-bit.)
>>>
>>>> And the uint8_t is also unnecessarily specific. We do want to know that
>>>> digits up to 255 can be stored, but nothing goes wrong if the type is
>>>> wider. uint_least8_t says exactly what's wanted and no more.
>>>> Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
>>>> char is effectively uint_least8_t.
>>>>
>>>
>>> There is such a thing as being /too/ portable.
>> I was not talking about portability. It's handy when types say what's
>> needed and what isn't. uint8_t says too much. uint_least8_t is better
>> because it says exactly what's needed and what isn't. The suggestion
>> had nothing to do with portability.
>
> Surely uint_least8_t specifies even more?

The depends on what you mean by specifies more. Yes, it specifies what
you need and what you don't need, so in some sense more than uint8_t.
But uint8_t specifies a more restricted type.

Anyway, I don't like arguing about words. What matters is that uint8_t
is not, logically, what's wanted here.

> That's not a type I've come across anywhere else.

It should be used more widely. I suspect that some programmers
gravitate to the exact width types because they map to how they think
about machines. Others gravitate towards the leastN types because they
map to how they think about algorithms.

> I can sort of see the use-case, but it sounds niche: you need a u8
> type but don't want to commit to exactly u8 because on some unusual
> architecture you don't want it to jump through hoops to give you u8,
> when u16 would do just as well. But specifying u16 anyway would be
> wasteful everywhere else.

You are in the first camp above. The use-case I see is simply
documenting as accurately as possible what the code is doing.

--
Ben.

Re: Beginner....Decimal/Octal converter help

<tfkj2l$1t7f3$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sun, 11 Sep 2022 14:07:49 +0200
Organization: A noiseless patient Spider
Lines: 95
Message-ID: <tfkj2l$1t7f3$1@dont-email.me>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com>
<86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad>
<tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk>
<tfi6jk$1f9ct$1@dont-email.me> <87zgf714zc.fsf@bsb.me.uk>
<tfil39$1h9u$1@gioia.aioe.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 11 Sep 2022 12:07:49 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="8f3c28c146db814df9693bbec4fd4ccc";
logging-data="2006499"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oD9T6mrNy93ilzxoLfRGzgKhR7VgsWmc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.1
Cancel-Lock: sha1:Mq2fp0BgElroPnML8Lx64nGskdQ=
In-Reply-To: <tfil39$1h9u$1@gioia.aioe.org>
Content-Language: en-GB
 by: David Brown - Sun, 11 Sep 2022 12:07 UTC

On 10/09/2022 20:30, Bart wrote:
> On 10/09/2022 19:03, Ben Bacarisse wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>>
>>> On 10/09/2022 13:54, Ben Bacarisse wrote:
>>>> David Brown <david.brown@hesbynett.no> writes:
>>>>
>>>>> On 09/09/2022 21:06, Scott Lurndal wrote:
>>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>>>> ManyBeers <markzuffi@yahoo.com> writes:
>>>>>>>
>>>>>>>> Hello I am teaching myself C and I need some help with a problem.
>>>>>>>> After a search for dec/oct convert I found this code:
>>>>>>>>
>>>>>>>> [..code..]
>>>>>>>
>>>>>>> I would like to offer a different kind of answer to your
>>>>>>> questions.  First a suggestion:  for this kind of problem I think
>>>>>>> you will find it helpful to use unsigned types exclusively.  In
>>>>>>> the code below we will need (besides a plain 'unsigned' here and
>>>>>>> there) two types, one for 8-bit values and one for 32-bit values:
>>>>>>>
>>>>>>>     typedef unsigned char  UC;
>>>>>>>     typedef unsigned int   UI;
>>>>>> There already standard perfectly cromulent typedefs for both,
>>>>>> uint8_t and uint32_t.
>>>>>
>>>>> And those type names are better in a number of ways.  They are
>>>>> clearer, more precise, and hide the design flaw in C of mixing the
>>>>> concepts of "character" and "small number".  In particular, they
>>>>> specify exactly what size your numbers are, rather that having hidden
>>>>> assumptions in the code.
>>>> But they both say too much.  The 'source' integer size need not be 32
>>>> bits and, given that there are no other obvious requirements, unsigned
>>>> int seems to me to be the natural choice.  And if the requirements were
>>>> that 32-bit numbers could be handled, uint_least32_t (or uint_fast32_t)
>>>> more precisely meets the need.
>>>
>>> "unsigned int" might have been a reasonable choice, but Tim's code
>>> goes on to rely on it being 32-bit.  (Or to be more accurate, it
>>> /says/ it relies on it being 32-bit.)
>>>
>>>> And the uint8_t is also unnecessarily specific.  We do want to know
>>>> that
>>>> digits up to 255 can be stored, but nothing goes wrong if the type is
>>>> wider.  uint_least8_t says exactly what's wanted and no more.
>>>> Mind you, since char is guaranteed to be at least 8 bits wide, unsigned
>>>> char is effectively uint_least8_t.
>>>>
>>>
>>> There is such a thing as being /too/ portable.
>>
>> I was not talking about portability.  It's handy when types say what's
>> needed and what isn't.  uint8_t says too much.  uint_least8_t is better
>> because it says exactly what's needed and what isn't.  The suggestion
>> had nothing to do with portability.
>
> Surely uint_least8_t specifies even more?
>
> That's not a type I've come across anywhere else. And I'd be interested
> in which machine it would map to anything other than 'unsigned char' (as
> it does on x64.
>
> I can sort of see the use-case, but it sounds niche: you need a u8 type
> but don't want to commit to exactly u8 because on some unusual
> architecture you don't want it to jump through hoops to give you u8,
> when u16 would do just as well. But specifying u16 anyway would be
> wasteful everywhere else.
>
> And then there's uint_fast8_t; I can't even make a guess at that. All I
> knows is that the last thing I want to see in source code is
> uint_least8_t and uint_fast8_t (maybe 'const' too!) cluttering things up
> and obscuring the underlying code. uint8_t is bad enough.
>
> I want to see something like 'byte' or 'u8'. For the unusual situation
> above, deal with that in usercode via a special usertype.

The "fast" and "least" types /do/ have their uses. There are not many
targets that don't support 8-bit char and everything else as a power of
8 bits, but there /are/ a few. The main class of exceptions are DSP
processors, and some highly specialised task-specific embedded
processors. With C23, I believe types (u)int8_t, (u)int16_t, (u)int32_t
and (u)int64_t have become mandatory - a target that does not support
these natively must simulate them. This renders the "least" types obsolete.

The "fast" types, on the other hand, can have real use in embedded
systems that span a range of target devices. Some 16-bit, 32-bit and
64-bit processors require extra masking or extending instructions when
dealing with 8-bit or 16-bit data (the original Alpha processor was
particularly bad). So local variables of a larger type are more
efficient. On the other hand, these will be much less efficient if the
code is then compiled for an 8-bit target. The main type of interest
here is "(u)int_fast8_t", since most uses of "int_fast16_t" can be
written as "int".

Re: Beginner....Decimal/Octal converter help

<86czc2qbfo.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sun, 11 Sep 2022 06:37:31 -0700
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <86czc2qbfo.fsf@linuxsc.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <base-20220909233040@ram.dialup.fu-berlin.de> <cIPSK.45075$SMP5.39069@fx05.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="eacead3a04a98f103c487ee10dc191dc";
logging-data="2034517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tMEeB8CgGSgdLF05Alf7oU+Q6ODAnc7U="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:6ecBeNi0G+Gw2skYNvE4tFpjaEQ=
sha1:TdYLSnJ9gXdolVFWT0lzG1mEdtc=
 by: Tim Rentsch - Sun, 11 Sep 2022 13:37 UTC

scott@slp53.sl.home (Scott Lurndal) writes:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> ManyBeers <markzuffi@yahoo.com> writes:
>>
>>> int convertDecimalToOctal(int decimalNumber);
>>
>> The following program tries to convert the number given in main
>> to its octal representation, which is printed to standard output.
>>
>> I wrote it just to the point where it seems to work for this
>> special case, but I'm not convinced that it will print the
>> correct result in all cases or does not overflow any of its
>> buffers!
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> /* are there digits above 0 in the buffer? */
>> int digits_in( char buffer[] )
>> { size_t i = 0;
>> for( ; i < strlen( buffer )&& buffer[ i ]=='0'; ++i );
>> return buffer[ i ]> '0' && buffer[ i ]<= '9'; }
>>
>> /* write div to "result" and returns the remainder */
>> int divrem
>> ( char dividend[], char result[], int base_of_dividend, int divisor )
>> { { size_t i = 0;
>> for( ; i < strlen( dividend ); ++i )
>> result[ i ]= ' '; result[ i ]= 0; }
>> int r = 0; int j = 0; int k = 0;
>> for( size_t i = 0; i < strlen( dividend ); ++i )
>> { if( dividend[ i ]!= ' ' )
>> { r = r * base_of_dividend + dividend[ i ]- '0';
>> if( r >= divisor )
>> { int const q = r/divisor; r = r%8;
>> result[ j ]=( char )( q + '0' ); }
>> else
>> { result[ j ]='0'; }} ++j; }
>> return r; }
>>
>> /* convert number, which is given in the base_of_dividend into
>> base_of_result */
>> void convert
>> ( char number[], char buffer[],
>> int base_of_dividend,
>> int base_of_result /* must not be larger than base_of_dividend */ )
>> { if( digits_in( number ))
>> { int const digit =
>> divrem( number, buffer, base_of_dividend, base_of_result );
>> { convert( buffer, number, base_of_dividend, base_of_result );
>> putchar( '0' + digit ); }}}
>>
>> int main( void )
>> { /* source in dec */
>> char a[] = "897528309349537459261193475234783479487034102857389475930";
>> /* the buffer must at least have a's len */
>> char b[] = " ";
>> convert( a, b, 10, 8 );
>> putchar( '\n' ); }
>
> Somewhat more concisely:
>
> $ cat /tmp/octal.c
> #include <stdint.h>
> #include <stdio.h>
> #include <string.h>
>
>
> int
> main(int argc, const char **argv, const char **envp)
> {
> char buf[64];
> char *bp = &buf[64];
> uint64_t value;
>
> if (argc > 1) value = strtoul(argv[1], NULL, 0);
> else value = 0x400;
>
> *--bp = '\0';
> while (value > 0) {
> *--bp = '0' + (value & 7ul);
> value /= 8;
> }
>
> printf("Octal = %s\n", bp);
> return 0;
> }

This code concisely solves a very different problem. SR's code
is meant to address the problem of converting values that will
not fit in any of C's standard integer types. This code doesn't
even try to do that.

Re: Beginner....Decimal/Octal converter help

<868rmqqb0c.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Beginner....Decimal/Octal converter help
Date: Sun, 11 Sep 2022 06:46:43 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <868rmqqb0c.fsf@linuxsc.com>
References: <8ffd982c-2e82-4caa-9256-ec17be2efe56n@googlegroups.com> <86leqsr091.fsf@linuxsc.com> <S8MSK.400474$iiS8.244727@fx17.iad> <tfhn50$1c35d$1@dont-email.me> <875yhv30nu.fsf@bsb.me.uk> <tfi6jk$1f9ct$1@dont-email.me> <87zgf714zc.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="eacead3a04a98f103c487ee10dc191dc";
logging-data="2034517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OfiUsA+WZoRY5pI3MAvXN+pgtvS/KgXQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:fq2biqTASURAasVFLmsiqwSMVrw=
sha1:rwCDZEEr5Ue/GuB+9IwX2CdlaBw=
 by: Tim Rentsch - Sun, 11 Sep 2022 13:46 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> David Brown <david.brown@hesbynett.no> writes:

[suggestion to use uint8_t and uint32_t in place of (my earlier use
of) unsigned char and unsigned int]

>> There are some real-world C implementations that do not have uint8_t
>> and/or uint32_t, but the overlap in code between these niche systems
>> and "normal" systems is almost non-existent. So the use of
>> "uint_least8_t", "uint_fast32_t" and similar types is IMHO needless
>> complication and verbosity, and certainly not something beginners
>> should need to bother about.
>
> Really? That's a complication?
>
> Being a beginners is another matter. We don't know what sort of
> beginner this is (I think it was a drive-by anyway), but none of the
> types is ideal for a beginner. In fact, I imagine that's partly what
> motivated TR's choice.

Quite so.

Pages:1234
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor