public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* int8_t outputs char via <iostream>
@ 2023-12-23  0:14 Olavi Esker
  2023-12-23 10:10 ` Jonathan Wakely
  2023-12-23 10:31 ` Jonathan Wakely
  0 siblings, 2 replies; 3+ messages in thread
From: Olavi Esker @ 2023-12-23  0:14 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1511 bytes --]

Hello,
With <iostream> int8_t prints out char according to ascii number. Similarly
it reads a single char, which cannot be static converted. The compiler
gives no warning whatsoever. But when <cstdio> is used with scanf %hhd and
printf %d it works perfectly. Is this really the intended <iostream>
functioning? In Rust i8 and C <stdio.h> int8 works fine, too.
#include <iostream>
#include <cstdint>

int main()
{
std::int8_t myInt{65};
myInt += 1;
std::cout << myInt;
}

Guess what this returns?
Character "B".

int main()
{
std::int8_t myInt{};
std::cin >> myInt;
std::cout << myInt;
}
This will also read a character, and
print the characters ascii value.
So if I give it 3, it read it as '3', and prints out 51.


The compiler gives no warning of this whatsoever with the flags:
                "-std=c++20",
                "-pedantic-errors",
                "-Wall",
                "-Wpedantic",
                "-Wshadow",
                "-Wcast-align",
                "-Wlogical-op",
                "-Wno-unused-parameter",
                "-Weffc++",
                "-Wextra",
                "-Wconversion",
                "-Wsign-conversion".


It does seem like a mistake to have `signed char` and `unsigned char`
display as characters rather than numbers, since `char` is a distinct type.
And so `char` could display as a character and the other two as integers.

Wish you can change this, or at least have a warning, because <iostream>
functions fine for higher bitted integers.

Thanks.
OE

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: int8_t outputs char via <iostream>
  2023-12-23  0:14 int8_t outputs char via <iostream> Olavi Esker
@ 2023-12-23 10:10 ` Jonathan Wakely
  2023-12-23 10:31 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2023-12-23 10:10 UTC (permalink / raw)
  To: Olavi Esker; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2253 bytes --]

On Sat, 23 Dec 2023, 00:15 Olavi Esker via Gcc-help, <gcc-help@gcc.gnu.org>
wrote:

> Hello,
> With <iostream> int8_t prints out char according to ascii number. Similarly
> it reads a single char, which cannot be static converted. The compiler
> gives no warning whatsoever. But when <cstdio> is used with scanf %hhd and
> printf %d it works perfectly. Is this really the intended <iostream>
> functioning? In Rust i8 and C <stdio.h> int8 works fine, too.
> #include <iostream>
> #include <cstdint>
>
> int main()
> {
> std::int8_t myInt{65};
> myInt += 1;
> std::cout << myInt;
> }
>
> Guess what this returns?
> Character "B".
>
> int main()
> {
> std::int8_t myInt{};
> std::cin >> myInt;
> std::cout << myInt;
> }
> This will also read a character, and
> print the characters ascii value.
> So if I give it 3, it read it as '3', and prints out 51.
>
>
> The compiler gives no warning of this whatsoever with the flags:
>                 "-std=c++20",
>                 "-pedantic-errors",
>                 "-Wall",
>                 "-Wpedantic",
>                 "-Wshadow",
>                 "-Wcast-align",
>                 "-Wlogical-op",
>                 "-Wno-unused-parameter",
>                 "-Weffc++",
>                 "-Wextra",
>                 "-Wconversion",
>                 "-Wsign-conversion".
>
>
> It does seem like a mistake to have `signed char` and `unsigned char`
> display as characters rather than numbers, since `char` is a distinct type.
>

No, it's not a mistake. There are intentionally designed functions for
writing both signed char and unsigned char to an ostream. This is the
intended behaviour. Changing it now would break code that expects the
behaviour that has existed for more than 20 years.

And so `char` could display as a character and the other two as integers.
>
> Wish you can change this, or at least have a warning, because <iostream>
> functions fine for higher bitted integers.
>

A warning doesn't seem appropriate for code that works perfectly correctly
according to the standard. What if this is exactly the behaviour you want?
Why should it warn?

This is something that surprises beginners, then they learn how it works,
and it's not a problem.

Just cast to int to get what you want.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: int8_t outputs char via <iostream>
  2023-12-23  0:14 int8_t outputs char via <iostream> Olavi Esker
  2023-12-23 10:10 ` Jonathan Wakely
@ 2023-12-23 10:31 ` Jonathan Wakely
  1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2023-12-23 10:31 UTC (permalink / raw)
  To: Olavi Esker; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1960 bytes --]

On Sat, 23 Dec 2023, 00:15 Olavi Esker via Gcc-help, <gcc-help@gcc.gnu.org>
wrote:

> Hello,
> With <iostream> int8_t prints out char according to ascii number. Similarly
> it reads a single char, which cannot be static converted. The compiler
> gives no warning whatsoever. But when <cstdio> is used with scanf %hhd and
> printf %d it works perfectly.


It's a bit misleading to say that printf %d works perfectly, when it does
an implicit cast to int which behaves exactly the same as using char with
%d.


Is this really the intended <iostream>
> functioning? In Rust i8



In Rust isn't it a distinct integral type, not just a typedef for a
character type? So not comparable.


and C <stdio.h> int8 works fine, too.
> #include <iostream>
> #include <cstdint>
>
> int main()
> {
> std::int8_t myInt{65};
> myInt += 1;
> std::cout << myInt;
> }
>
> Guess what this returns?
> Character "B".
>
> int main()
> {
> std::int8_t myInt{};
> std::cin >> myInt;
> std::cout << myInt;
> }
> This will also read a character, and
> print the characters ascii value.
> So if I give it 3, it read it as '3', and prints out 51.
>
>
> The compiler gives no warning of this whatsoever with the flags:
>                 "-std=c++20",
>                 "-pedantic-errors",
>                 "-Wall",
>                 "-Wpedantic",
>                 "-Wshadow",
>                 "-Wcast-align",
>                 "-Wlogical-op",
>                 "-Wno-unused-parameter",
>                 "-Weffc++",
>                 "-Wextra",
>                 "-Wconversion",
>                 "-Wsign-conversion".
>
>
> It does seem like a mistake to have `signed char` and `unsigned char`
> display as characters rather than numbers, since `char` is a distinct type.
> And so `char` could display as a character and the other two as integers.
>
> Wish you can change this, or at least have a warning, because <iostream>
> functions fine for higher bitted integers.
>
> Thanks.
> OE
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-23 10:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-23  0:14 int8_t outputs char via <iostream> Olavi Esker
2023-12-23 10:10 ` Jonathan Wakely
2023-12-23 10:31 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).