Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: 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= Xref: csiph.com comp.lang.c:167554 ManyBeers 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 > #include > > 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 ` 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 */