Re: Re: int8_t outputs char via <iostream>

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux