0

When we perform the following code:

char p = 0 ;
cout << p << endl ;

Does this mean that p stores the symbol whose ASCII code is 0? (Which is NULL Character, and therefore nothing gets printed?)

The range of character data type is -128 to 127. And ASCII 0 to 256. So, how those ASCII symbols (code > 127) get printed?

From the commented dupe links, I cant understand, the above part of the question.

  • `NULL` refers to *pointer values*. A character of value zero isn’t a null pointer. – Konrad Rudolph Sep 04 '22 at 08:11
  • See the explanation given [here](https://stackoverflow.com/a/71428075/12002570). – Jason Sep 04 '22 at 08:11
  • Related/Dupe: https://stackoverflow.com/a/71276292/12002570 – Jason Sep 04 '22 at 08:12
  • @JasonLiam: While your answer to that other question may partly address the question being asked here, the questions seem very different. I don't think it's fair to say a question is a dupe of another one just because one answer can be used for both. – John Zwinck Sep 04 '22 at 08:15
  • @JohnZwinck That is why i didn't close this question as a dupe of [this](https://stackoverflow.com/a/71276292/12002570). Still [this one](https://stackoverflow.com/a/71428075/12002570) is a dupe. Similarly, [this](https://stackoverflow.com/questions/47934127/initialize-vector-char-with-int-values). Here is a more close dupe: [Assigning an integer to a character variable](https://stackoverflow.com/questions/44399226/assigning-an-integer-to-a-character-variable) – Jason Sep 04 '22 at 08:16
  • 3
    Ascii's range is 0-127, outside that range is covered by various ascii extensions – Alan Birtles Sep 04 '22 at 08:17
  • @JohnZwinck I agree the questions are different but the [answer](https://stackoverflow.com/a/71276292/12002570) answers OP's question asked here. Again, since the question are different, i didn't close this. Here is a more close dupe: [Assigning an integer to a character variable](https://stackoverflow.com/questions/44399226/assigning-an-integer-to-a-character-variable) – Jason Sep 04 '22 at 08:19
  • @JasonLiam None of the duplicates really explains the issue here. This is about how output streams interpret insertion of a character as 'true' character and not a numerical value so it needs to be cast to a 'pure' arithmetic type to be interpreted as output of a numerical amount. It relates to the number/character duality of `char` but it's not really the same question because it's really about the design of iostream insertion and overloading. – Persixty Sep 04 '22 at 08:48
  • Thank u @JasonLiam for those associated links, I now understand little bit more about characters. Thank u. I am interested to know how those symbols would get printed whose ASCII equivalent decimal is >127, as range of char is -128 to 127. – Raman Kumar Sep 04 '22 at 09:17
  • Thank u @KonradRudolph for ur comment. Can we safely assume, a character whose value has been set to 0 ( and not '0' ), will represent a null character? (I am new to C++) – Raman Kumar Sep 04 '22 at 09:22
  • @RamanKumar You're welcome :). I usually use the [books listed here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to understand what is going on when decimal is > 127. The books mentioned in the above link are also available in pdf forms(which i prefer over hardcopies). In most of those books, you'll find the explanation you're looking for. – Jason Sep 04 '22 at 09:22
  • @RamanKumar Integer `0` correspond to the NUL character `'\0'`. See the explanation [here](https://stackoverflow.com/a/71276292/12002570). – Jason Sep 04 '22 at 09:23
  • Thank u @Persixty for ur comment. (I am not able to understand complex terms used by u in ur comment. I am new to C++. Anyways thank u very much.) – Raman Kumar Sep 04 '22 at 09:29
  • @RamanKumar Can we safely assume that the numerical value of '0' is 0. Yes. It's the only character value that is specified. Though it is still strongly recommended that you write '\0' for clarity. It is defined as the null character (note no caps) but I follow a school that prefer to call it NUL (one el) again to avoid confusion. This whole question is about how binary representation is not sufficient to understand semantic meaning and it's important to get the two worlds right. – Persixty Sep 04 '22 at 09:32
  • @RamanKumar No worries my comment was direct to someone else. As I just said there's a fundamental topic here. I'm sure you've been told that computers only work on binary and this thing that a `char` represents a unit of text output and a numerical value is one of the places that it comes to the surface. It is (IMHO) a flaw in C inherited by C++ that it doesn't differentiate between smallest unit of memory (byte) and smallest unit of text output (character). `std::byte` in C++17 is aimed at addressing that but is flawed. – Persixty Sep 04 '22 at 09:36
  • Okay @Persixty, I will write '\0' for clarity. Thank u for ur kind comment. I never thought null and NULL were different. Will have to figure things. Thank u. – Raman Kumar Sep 04 '22 at 09:38
  • @RamanKumar They are broadly the same concept but not the same thing! And they get confused. Interestingly while the NULL value is 0. But the bit-pattern of a null pointer may not be! Try to avoid making assumptions about layout unless you need to and you'll write safer clearer code. – Persixty Sep 04 '22 at 10:20

1 Answers1

1

Yes, 0 is ASCII NUL.

char is signed on some platforms and unsigned on others. Standard ASCII only has a range of 0 to 127. The rest, whether they are 128 to 255 or -128 to -1, are sometimes called Extended ASCII and are less consistent across systems.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Less consistent though increasingly interpreted as multi-byte characters in UTF-8. It's not all systems but studies show that 98% of the web is UTF-8. In my professional life working with inter-organisational international data transfer no one is interested in working with anything else. And when people do use something else, something somewhere in the tech stack can't cope or makes a mess of it. – Persixty Sep 04 '22 at 09:12
  • Thank u @JohnZwinck I now understand that range of char is platform dependent! – Raman Kumar Sep 04 '22 at 09:31
  • @JohnZwinck, I wanted to ask, if we are on a platform where characters are signed. and we initialise :: char a = 127; :: and then perform :: a++; :: will a get the value of symbol stored at -128 (negative 128) ? How can we know for sure which symbol is going to be printed? (Since, we are not sure, if null ch exists at 0 or -128 on a signed character platform) Thank you. – Raman Kumar Sep 04 '22 at 13:01
  • To facilitate UTF-8 use with C++, the `u8"text"` was added in C++11, `u8'c'` was added in C++17, and `char8_t` was added in C++20 to represent a UTF-8 encoding unit (a UTF-8 byte). Slowly but surely, C++ is become more and more Unicode savvy. – Eljay Sep 04 '22 at 13:03
  • @RamanKumar: Signed integer overflow is Undefined Behavior in C++, and that means incrementing a signed `char` past 127 can make your program do anything, including crash. – John Zwinck Sep 04 '22 at 13:06