Connecting Tech Pros Worldwide Forums | Help | Site Map

Print a signel octal

laurent.pauloin@techway.fr
Guest
 
Posts: n/a
#1: Dec 18 '07
Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !

Ali Karaali
Guest
 
Posts: n/a
#2: Dec 18 '07

re: Print a signel octal


On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
Quote:
Hello,
>
I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?
>
Tk !
%ho
Richard Heathfield
Guest
 
Posts: n/a
#3: Dec 18 '07

re: Print a signel octal


Ali Karaali said:
Quote:
On 18 Aral?k, 17:06, laurent.paul...@techway.fr wrote:
Quote:
>With a signed short it's printf %hi but what about an octal ?
>>
%ho
%it's %off %to %work %we %go...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Philip Potter
Guest
 
Posts: n/a
#4: Dec 18 '07

re: Print a signel octal


Ali Karaali wrote:
Quote:
On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
Quote:
>Hello,
>>
>I would like to print a signed octal.
>With a signed short it's printf %hi but what about an octal ?
>>
>Tk !
>
%ho
That's the format specifier for an unsigned short to be formatted as an
octal, not for a signed int formatted as an octal.

The *printf() functions don't provide what the OP wants. If you want a
signed int printed as an octal you can do this:

printf("%c%o",octal<0 ? '-' : ' ', octal<0 ? -octal : octal);

...although this may fail for octal < -INT_MAX.

Phil
Martin Ambuhl
Guest
 
Posts: n/a
#5: Dec 18 '07

re: Print a signel octal


laurent.pauloin@techway.fr wrote:
Quote:
Hello,
>
I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?
The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code like
the following. You might also want to think about what things might be
done better. Note that if you start with a signed int value, you should
probably memmove or memcpy it to an unsigned int used as the argument
for show_signed_octal (unless you _know_ your clever pointer trick is
legal and works).

#include <stdio.h>
#include <limits.h>

void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}

int main(void)
{
unsigned int x = 0, increment = UINT_MAX / 13, i;
show_signed_octal(0);
for (i = 1; i < 14; i++) {
x += increment;
show_signed_octal(x);
}
show_signed_octal((unsigned) INT_MIN);
show_signed_octal((unsigned) INT_MAX);
show_signed_octal(UINT_MAX);
return 0;
}


[Output for one implementation]
argument a = 0 is non-negative.

argument a = 02354235423 is non-negative.

argument a = 04730473046 is non-negative.

argument a = 07304730471 is non-negative.

argument a = 011661166114 is non-negative.

argument a = 014235423537 is non-negative.

argument a = 016611661162 is non-negative.

argument a = 021166116605 is negative,
1 + ~a = -016611661173,
1 + ~(1 + ~a) = 021166116605.

argument a = 023542354230 is negative,
1 + ~a = -014235423550,
1 + ~(1 + ~a) = 023542354230.

argument a = 026116611653 is negative,
1 + ~a = -011661166125,
1 + ~(1 + ~a) = 026116611653.

argument a = 030473047276 is negative,
1 + ~a = -07304730502,
1 + ~(1 + ~a) = 030473047276.

argument a = 033047304721 is negative,
1 + ~a = -04730473057,
1 + ~(1 + ~a) = 033047304721.

argument a = 035423542344 is negative,
1 + ~a = -02354235434,
1 + ~(1 + ~a) = 035423542344.

argument a = 037777777767 is negative,
1 + ~a = -011,
1 + ~(1 + ~a) = 037777777767.

argument a = 020000000000 is negative,
1 + ~a = -020000000000,
1 + ~(1 + ~a) = 020000000000.

argument a = 017777777777 is non-negative.

argument a = 037777777777 is negative,
1 + ~a = -01,
1 + ~(1 + ~a) = 037777777777.


Martin Ambuhl
Guest
 
Posts: n/a
#6: Dec 18 '07

re: Print a signel octal


Ali Karaali wrote:
Quote:
On 18 Aralık, 17:06, laurent.paul...@techway.fr wrote:
Quote:
>Hello,
>>
>I would like to print a signed octal.
>With a signed short it's printf %hi but what about an octal ?
>>
>Tk !
>
%ho
"%o" (and "%ho") are *defined* as taking unsigned values to be shown as
unsigned. Your "solution" in no way addresses the question of printing
"signed octal".
Philip Potter
Guest
 
Posts: n/a
#7: Dec 19 '07

re: Print a signel octal


Philip Potter wrote:
Quote:
Ali Karaali wrote:
Quote:
>On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
Quote:
>>Hello,
>>>
>>I would like to print a signed octal.
>>With a signed short it's printf %hi but what about an octal ?
>>>
>>Tk !
>%ho
>
That's the format specifier for an unsigned short to be formatted as an
octal, not for a signed int formatted as an octal.
>
The *printf() functions don't provide what the OP wants. If you want a
signed int printed as an octal you can do this:
>
printf("%c%o",octal<0 ? '-' : ' ', octal<0 ? -octal : octal);
Argh! This should be:

short octal = f(); /* f() gets the value to be printed */
unsigned short value = (octal<0) ? -octal : octal;
printf("%c%o",octal<0 ? '-' : ' ', value);

...because before I was passing a signed value where printf() expected
unsigned.
Quote:
..although this may fail for octal < -INT_MAX.
>
Phil
Tor Rustad
Guest
 
Posts: n/a
#8: Dec 19 '07

re: Print a signel octal


Martin Ambuhl wrote:

[...]
Quote:
The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code like
the following. You might also want to think about what things might be
done better.

I would like to suggest an improvement too. :)

Quote:
#include <stdio.h>
#include <limits.h>
>
void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}
For signed integer types, it's implementation-defined, if the object
representation with sign bit 1 and the value bits equal, is a trap
representation or not.

Not assuming 2's complement machine:


void print_octal(int signed_num)
{
if (signed_num < 0)
{
printf("-%#o\n", (unsigned int)(signed_num * (-1)) );
}
else
{
printf("%#o\n", (unsigned int)signed_num);
}
}

--
Tor <bwzcab@wvtqvm.vw | tr i-za-h a-z>
Philip Potter
Guest
 
Posts: n/a
#9: Dec 20 '07

re: Print a signel octal


Tor Rustad wrote:
Quote:
Martin Ambuhl wrote:
>
[...]
>
Quote:
>The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
>values. If you truly want to print values as signed, consider code like
>the following. You might also want to think about what things might be
>done better.
>
Quote:
>#include <stdio.h>
>#include <limits.h>
>>
>void show_signed_octal(unsigned int a)
>{
> if (a <= INT_MAX)
> printf("argument a = %#o is non-negative.\n\n", a);
> else {
> printf("argument a = %#o is negative,\n ", a);
> a = 1 + ~a;
> printf(" 1 + ~a = -%#o, \n", a);
> a = 1 + ~a;
> printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
> }
>}
>
For signed integer types, it's implementation-defined, if the object
representation with sign bit 1 and the value bits equal, is a trap
representation or not.
It's lucky that Martin didn't deal with signed integer types then.
Quote:
Not assuming 2's complement machine:
Martin's didn't either, though I initially thought it did. Because it
takes an unsigned int argument, any negative numbers will first be
shifted into the range of unsigned int, when the 1 + ~a operation
correctly gets the absolute value of the original negative number:

For example:
show_signed_octal(-3);

With any signed integer representation, -3 when converted to unsigned
int becomes UINT_MAX+1-3, which will be represented as
....111101 (any number of leading ones)
after complement and add one the representation is
....000011 (any number of leading zeroes)
which has value 3. Hey presto!

Phil
Joe Wright
Guest
 
Posts: n/a
#10: Dec 21 '07

re: Print a signel octal


Philip Potter wrote:
Quote:
Tor Rustad wrote:
Quote:
>Martin Ambuhl wrote:
>>
>[...]
>>
Quote:
>>The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
>>values. If you truly want to print values as signed, consider code
>>like the following. You might also want to think about what things
>>might be done better.
>>
Quote:
>>#include <stdio.h>
>>#include <limits.h>
>>>
>>void show_signed_octal(unsigned int a)
>>{
>> if (a <= INT_MAX)
>> printf("argument a = %#o is non-negative.\n\n", a);
>> else {
>> printf("argument a = %#o is negative,\n ", a);
>> a = 1 + ~a;
>> printf(" 1 + ~a = -%#o, \n", a);
>> a = 1 + ~a;
>> printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
>> }
>>}
>>
>For signed integer types, it's implementation-defined, if the object
>representation with sign bit 1 and the value bits equal, is a trap
>representation or not.
>
It's lucky that Martin didn't deal with signed integer types then.
>
Quote:
>Not assuming 2's complement machine:
>
Martin's didn't either, though I initially thought it did. Because it
takes an unsigned int argument, any negative numbers will first be
shifted into the range of unsigned int, when the 1 + ~a operation
correctly gets the absolute value of the original negative number:
>
For example:
show_signed_octal(-3);
>
With any signed integer representation, -3 when converted to unsigned
int becomes UINT_MAX+1-3, which will be represented as
...111101 (any number of leading ones)
after complement and add one the representation is
...000011 (any number of leading zeroes)
which has value 3. Hey presto!
>
Phil
/*
Signed Octal?
*/

#include <stdio.h>

int main(void) {
int a = 123;
int b = -123;
printf("%s%o\n", a < 0 ? "-" : "", a < 0 ? -a : a);
printf("%s%o\n", b < 0 ? "-" : "", b < 0 ? -b : b);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Philip Potter
Guest
 
Posts: n/a
#11: Dec 22 '07

re: Print a signel octal


Joe Wright wrote:
Quote:
/*
Signed Octal?
*/
>
#include <stdio.h>
>
int main(void) {
int a = 123;
int b = -123;
printf("%s%o\n", a < 0 ? "-" : "", a < 0 ? -a : a);
printf("%s%o\n", b < 0 ? "-" : "", b < 0 ? -b : b);
return 0;
}
>
This code is almost identical to code I've already posted elsethread in
message <fk8tsc$7h1$1@aioe.org>. It even has the same bug!

Phil
Closed Thread


Similar C / C++ bytes