From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 9491B3858C98 for ; Sat, 23 Dec 2023 10:10:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9491B3858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9491B3858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703326219; cv=none; b=QRLBsr5XjTl5SHspKY53WoPlewN/X4iierilFh2mOO+Ksh7PF2XxBL8r3dXXUDcbonZdgasOmnQVsi+MAGB9BoMlDxAT/HR037pGT+EbXvAiJIYGdNHlZLh9EyXHbYA4Fq83xTXi0fmSwqakW9te2qBzUEctnFSGpLwu9bRM0O8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703326219; c=relaxed/simple; bh=Qqvt1p5/YQmTUu/VDB6vuwxJ6MLVdDVgyCf/2tvImvI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=mY3TtIJdF8+5PVXbmQfLo3LlYLch1QUWt5GK9IIpHKOhQExaxxJZ7S+R4F11O8awhBTPcE63hPFLAietcAEr1SQPUG+uqqDGZo+PI1jOzXQ5FA718yoSDObaY7bdgo9dE630AF53uCD8Vyt5CtvRelPfaLdC5KQxnVlBLJv2QWU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-a1915034144so318423966b.0 for ; Sat, 23 Dec 2023 02:10:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703326216; x=1703931016; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hd/m5kG9wzsS+8DoHRKXWYRdO98L/m6dUZIOANEvozY=; b=XOvKBicSaU3vYSCa/ThQk4lPcahCsD/UxUssiNH9gXR7OO4TwqeKOsqWwhmBPDbBQk DtyG8pQSPYENOZsigBgmtxYKVkyZKGnKf3eFMoJYu4KHloseGLfg4v/tQwEULYs0f+Tz ddNSp/pjCrJBMm3f3w3+iBX5umwvsGwPRuszj4zB4oQftNnaE4J+vydIMhd748Af/b2u ni8Q97J1u/szxhl9Gjb476OP9ON62Yrvfskn6hGeKRQ+aC82WirhgP1511F2v3hSXps0 OF2ic29s99nhvqz6aK/MTiDp+PQ6683DOHJlNeVg3L3GS06Xs5oP8+QnW0Aoxbk66dRO MWPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703326216; x=1703931016; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hd/m5kG9wzsS+8DoHRKXWYRdO98L/m6dUZIOANEvozY=; b=lzYJhPG7hCxRKax9UrtfoVEptL2SFAnnOUYdpIpjmUdNPn7n51FreA0/Xgu2/KXS5z gfc7eMquwRzvkHZInYmJmVSnQAOq4E45HQ7iRk6pDZt36EoqEHyWhH8SHo1w6W0Q000h d6rShrf9bmpiWfXbtsBxK0OjD0BRHXtEU6gb8raayAqlnG5g68EB7fwA2yv+T/esyFac KSzU90pDwIy9oWyDM9UN30xKQL1bIyJemQb/STIkLD0rENsbJt4Awb0vCqqtDFNuzv/1 1C5CbttZgDozN2HaXigw6Cha1686c7nLjpl4vOgrk7XCTWDo3bUyhJ9ZTe32XvaL+p7G RbtA== X-Gm-Message-State: AOJu0YyybZrq+lVf55dC4vGKy3yK28zynh3MptsqeaWMGzrL8FiJKJ73 b9OO6H3bC4xjWBC30k7vOuEpG8NN1+aeJ1qpNzg= X-Google-Smtp-Source: AGHT+IELOYptRyiNPWp8dFfnwi3Nlz6VmAEpb+pgvQFU42PFxGm/V4UbqIZq+qy4u/Hvi0gIvDY16TxMhaMhBAlKPz8= X-Received: by 2002:a17:906:eb5a:b0:a26:878e:df7f with SMTP id mc26-20020a170906eb5a00b00a26878edf7fmr1529798ejb.126.1703326215833; Sat, 23 Dec 2023 02:10:15 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Sat, 23 Dec 2023 10:10:03 +0000 Message-ID: Subject: Re: int8_t outputs char via To: Olavi Esker Cc: gcc-help Content-Type: multipart/alternative; boundary="000000000000080b28060d2a8a58" X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000080b28060d2a8a58 Content-Type: text/plain; charset="UTF-8" On Sat, 23 Dec 2023, 00:15 Olavi Esker via Gcc-help, wrote: > Hello, > With 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 is used with scanf %hhd and > printf %d it works perfectly. Is this really the intended > functioning? In Rust i8 and C int8 works fine, too. > #include > #include > > 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 > 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. --000000000000080b28060d2a8a58--