2016 version Lecture Week 2 continued...
What's the difference between
0? I have watched the lectures, but it didn't help.
Thanks in advance!
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.Sign up to join this community
Characters in C Language are in ASCII encoding. For now, you need to know that there is a unique numerical value associated with any* character that you can think of. Lets have a look.
I will be using C++ for simply displaying that numerical value by typecasting it to integer.
cout<<(int)('a'); //outputs 97 cout<<(int)('b'); //outputs 98, as expected cout<<(int)('A'); //outputs 65 cout<<(int)('Z'); //outputs 90 cout<<(int)('0'); //anything inside a single quote is treated //as a character, this outputs 48 cout<<(int)('9'); //outputs 57, as expected
This numerical value is known as ASCII value. There are 256 characters in this set. Well, I said any character that you can think of, so here are some more.
cout<<(int)('!'); //outputs 33 cout<<(int)('@'); //outputs 64 cout<<(int)('#'); //outputs 35 cout<<(int)('$'); //outputs 36 cout<<(int)('%'); //outputs 37 cout<<(int)('/'); //outputs 47 cout<<(int)('\'); //Compilation error!!!
But why? After all,
\ is also a character!
\) is a special character that is used to represent an escape sequence. Now what are escape sequences? Escape sequences are a group of characters used to convey special meaning i.e. to attain a special functionality. An escape sequence is a combination of a backslash followed by a character. This gives it a special meaning. One more thing to be noted is that the whole combination is treated as a character(which you don't observe traditionally).
cout<<(int)('an'); //Compilation error, 'a' doesn't mark the //starting of an escape sequence cout<<(int)('\n'); //outputs 10 cout<<(int)('\b'); //outputs 8 cout<<(int)('\0'); //outputs 0
The meaning conveyed by these special characters is listed on Wikipedia.
Another question that may arise in your mind, is that how do we represent
\? This is done by prepending another
\ infront of it, afterall
\ conveys a special meaning.
cout<<"I \\ am backslash"; //outputs 'I \ am backslash'
You can also use the same technique to display other troublesome characters like
", by using
Coming specifically to your question, what's the difference between
\0 is an escape sequence used to represent nothing, really nothing, not even zero! It used to represent Null terminator. On contrast
0, is as it is, a simple zero.
Lets see this in practice. We know that
\0 marks the end of a string. So lets try it.
cout<<"Hello,\0 World"; //outputs 'Hello,', as \0 //marks the end of the string. cout<<"Hello,0 World"; //outputs 'Hello,0 World' simply, no twist here
Please note that when we talk about
\0, we mean all the bits are set to 0, unlike when we are talking about
0. The eight-bit representation looks like
'\0' 0 0b00000000 0x00 '0' 48 0b00110000 0x30
Hope that helps.
*I cheated you there, not all characters you can think of are covered in ASCII, you should instead look at UNICODE.
PS : I haven't watched 2016 lecture.
Characters are represented and stored in the computer using a numeric value called an ASCII value. (ASCII stands for American Standard Code for Information Interchange.) A '0' character is stored in a computer as a character with the numeric decimal value of 48 (or a hexadecimal value of 30). The end of string marker,
\0 , is actually stored as all binary zeros in the computer. (Doesn't matter if you are talking decimal, binary, or anything else, it's all zeros.)
There has to be a way to differentiate between characters being stored and numeric data. ASCII is how this is accomplished. For more info, you can google ASCII, or visit http://www.asciitable.com/
If this answers your question, please click on the check mark to accept, so that your question is removed from the unanswered question pool. Let's keep up on forum maintenance. ;-)
ultimately, computers use numbers to represent data of any kind (e.g., individual characters, text strings, images, media files, numbers, etc).
you might be wondering how can a computer be capable of representing all these different kinds of data with just numbers? how is it able to understand whether a particular numeric value represent something or another? for example, how is it able to understand that
'A' and not the numeric value
65 as we know it?
and the short answer to that is that it actually doesn't. computers think of numbers as numbers all the way. we, programmers, teach computers to interpret numbers differently in different contexts.
for example, we could teach a computer to interpret the value
65 as a normal integer value (not a character, for example) in some cases. and teach it to interpret it as
'A' in other cases. we do that by specifying a datatype for the value (e.g.,
believe it or not, but I could teach a computer to interpret the value
65 as a spaceship in a game or pretty much anything that you can think of.
so just to answer the original question, there is no difference between
'\0' except that they are different notations used in different contexts to denote conceptually different things even though both are stored as just
0 underneath the hood (in memory).
0, by itself, is typically used to denote a simple fixed integer value (aka an integer/int literal),
'\0' is used to denote a simple fixed character value (aka a char literal).
'0' on the other hand is a char literal stored as
49 in memory and it represents the digit
'0' (textually — think of it as part of a string of text).