public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Re: int8_t outputs char via <iostream>
@ 2023-12-25 19:47 Olavi Esker
  2023-12-26  8:39 ` Xi Ruoyao
  0 siblings, 1 reply; 2+ messages in thread
From: Olavi Esker @ 2023-12-25 19:47 UTC (permalink / raw)
  To: gcc-help, jwakely.gcc

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

Hello,
I would argue the way it works currently is dangerous and asks for issues.

Let me further demonstrate the problem with examples;

1. For <iostream> demonstration 1
/*-------------------------------------------------*/

#include <cstdint>
#include <iostream>

int main()
{
    std::int8_t myInt{65};
    std::cout << "Takes ASCII char " << myInt << '\n';
    std::cout << "(static_cast): " << static_cast<int>(myInt) << '\n';
    std::cout << "(int): " << (int)myInt << '\n';

    return 0;
}
// First prints the ASCII character for 65, which is "A"
// (int) and static_cast print 65 correctly.
// The programmer learns to use (int) to print out int8_t

/*-------------------------------------------------*/
2. <iostream> demonstration 2
/*-------------------------------------------------*/

#include <cstdint>
#include <iostream>

int main()
{
    std::cout << "Enter a number between 0 and 127: ";
    std::int8_t myInt{};
    std::cin >> myInt;
    std::cout << "You entered (no cast): " << myInt << '\n';
    std::cout << "You entered (static_cast): " << static_cast<int>(myInt)
<< '\n';
    std::cout << "You entered ( (int) ): " << (int)myInt << '\n';
    return 0;
}

// *User input:* 35
// You entered: 3 // this is the first char given
onlly. The second one is ignored.
// You entered (static_cast): 51 // this is when the programmer assumes
(int) or static_cast<int> should be used
// You entered ( (int )): 51 // 51 is the ASCII number for '3' .


/*-------------------------------------------------*/
3. <cstdio> demonstration 3
/*-------------------------------------------------*/

#include <iostream>
#include <cstdint>
#include <cstdio>
int main()
{
    std::int8_t num{};
    std::scanf("%hhd", &num); // Read the number and
store it in num
    std::printf("%d this for cstdio.h\n", num); // Print the value of num
    std::cout << sizeof(num) * 8;
    return 0;
}
//*User input*: 100
// prints correctly 100
// size is still int8_t.

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

* Re: Re: int8_t outputs char via <iostream>
  2023-12-25 19:47 Re: int8_t outputs char via <iostream> Olavi Esker
@ 2023-12-26  8:39 ` Xi Ruoyao
  0 siblings, 0 replies; 2+ messages in thread
From: Xi Ruoyao @ 2023-12-26  8:39 UTC (permalink / raw)
  To: Olavi Esker, gcc-help, jwakely.gcc

On Mon, 2023-12-25 at 21:47 +0200, Olavi Esker via Gcc-help wrote:
> Hello,
> I would argue the way it works currently is dangerous and asks for
> issues.

This is not a correct list to argue if a standard behavior is dangerous
or not.  Send a paper to WG21 if you really want to change it.

> Let me further demonstrate the problem with examples;
> 
> 1. For <iostream> demonstration 1
> /*-------------------------------------------------*/
> 
> #include <cstdint>
> #include <iostream>
> 
> int main()
> {
>     std::int8_t myInt{65};
>     std::cout << "Takes ASCII char " << myInt << '\n';
>     std::cout << "(static_cast): " << static_cast<int>(myInt) << '\n';
>     std::cout << "(int): " << (int)myInt << '\n';
> 
>     return 0;
> }
> // First prints the ASCII character for 65, which is "A"
> // (int) and static_cast print 65 correctly.
> // The programmer learns to use (int) to print out int8_t
> 
> /*-------------------------------------------------*/
> 2. <iostream> demonstration 2
> /*-------------------------------------------------*/
> 
> #include <cstdint>
> #include <iostream>
> 
> int main()
> {
>     std::cout << "Enter a number between 0 and 127: ";
>     std::int8_t myInt{};
>     std::cin >> myInt;
>     std::cout << "You entered (no cast): " << myInt << '\n';
>     std::cout << "You entered (static_cast): " << static_cast<int>(myInt)
> << '\n';
>     std::cout << "You entered ( (int) ): " << (int)myInt << '\n';
>     return 0;
> }
> 
> // *User input:* 35
> // You entered: 3 // this is the first char given
> onlly. The second one is ignored.
> // You entered (static_cast): 51 // this is when the programmer assumes
> (int) or static_cast<int> should be used
> // You entered ( (int )): 51 // 51 is the ASCII number for '3' .
> 
> 
> /*-------------------------------------------------*/
> 3. <cstdio> demonstration 3
> /*-------------------------------------------------*/
> 
> #include <iostream>
> #include <cstdint>
> #include <cstdio>
> int main()
> {
>     std::int8_t num{};
>     std::scanf("%hhd", &num); // Read the number and
> store it in num
>     std::printf("%d this for cstdio.h\n", num); // Print the value of num
>     std::cout << sizeof(num) * 8;
>     return 0;
> }
> //*User input*: 100
> // prints correctly 100
> // size is still int8_t.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

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

end of thread, other threads:[~2023-12-26  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-25 19:47 Re: int8_t outputs char via <iostream> Olavi Esker
2023-12-26  8:39 ` Xi Ruoyao

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).