 Hello and welcome. In this lecture we are going to see how characters, strings and booleans are represented inside a computer. There is a quick recap of relevant topics that we have already discussed in earlier lectures. We have seen the architecture of a simple computer and we have seen how integers and floating point numbers are represented inside the computer. In this lecture we are going to see how characters, strings and booleans are internally represented in a computer. We are going to see how you can declare the above data types in C++ and we will also put things together and take a glimpse at our first simple C++ program. Now from an earlier lecture you would recall this diagram where we had shown the basic structure of a simple computer and all the components here basically have information stored as sequences of 0s and 1s. So the question we want to ask now is how do we represent characters or strings using 0s and 1s? Now when we try to represent a character in a computer a character is going to be represented by a byte that is 8 bits. Since you have 8 bits and each bit can take on either 0 or 1 these 2 possible values. So you can represent 2 raised to 8 or 256 different characters and the valuations of the corresponding bytes would range from all 0s to all 1s and we have already seen how to read a binary sequence of 0s and 1s as a decimal integer. So the all 0s is the decimal 0 and the all 1s is the decimal 255 where there are exactly 8 bits in each one of them. So each character that you see on your keyboard or that you want displayed on your screen like a 0 or p is going to be treated as an unsigned 8 bit integer. It is going to be represented by a byte and therefore it is an 8 bit integer without a sign and we use this encoding of characters as unsigned 8 bit integers which is also called the ASCII encoding the American Standard Code for information interchange. For example, upper case A is denoted by the unsigned 8 bit integer 65 in decimal, upper case B is 66 in decimal. The number 0 or rather the character 0 is represented by the decimal number 48. Note that the character 0 is not represented by the decimal number 0. The character 0 is encoded using this 8 bit integer whose value is 48. The character dot is encoded as 46 and you can even have the blank space character which is also a character and it is encoded by the decimal 8 bit integer 32. So since characters are going to be represented as unsigned 8 bit integers, it is not surprising that characters can be compared with each other and also sorted like integers. In C++ the corresponding data type is denoted by this keyword CHAR or CARE. Unsigned CARE really means an unsigned 8 bit integer and so this is how you can also do integer operations on very small unsigned integer values. How do you declare CARE or unsigned CARE in C++? This is the usual way we declare variables. We put the type declaration in this case CARE here followed by the variable name or we can say unsigned CARE followed by the variable name. As I said an unsigned CARE is really an unsigned 8 bit integer and you can even do integer operations with it like addition or multiplication. Constant characters can either be specified as unsigned integers in 0 through 255. So for example you could say const CARE user input is the decimal 89 basically the character which is encoded by the decimal number 89 or const CARE default output is the character which is encoded by the hexadecimal number 4E. We have already studied about hexadecimal representation in an earlier lecture or constant characters can also be specified as a character symbol within single codes. For example I could say const CARE user input is Y, capital Y within single codes and const CARE default output is capital N within single codes. It turns out that the decimal 89 is indeed the decimal code for uppercase Y and hexadecimal 4E is indeed the code the 8 bit unsigned integer code for uppercase N. Now how do we represent strings in a computer? A string is really a sequence or array of characters that is terminated by a special character which we will call the end of string character. So for example if you consider the string give input colon followed by a space remember even a blank space is a character this really represents something like this where each box here represents one character. So you have uppercase G lowercase I lowercase V lowercase E then a space which is a character then lowercase I and PUT colon and then there is a space which is also a character in the string and at the end of it you have a special end of string character which is the character denoted by the all 0 unsigned 8 bit integer. We also represent it using backslash 0 when we use it in programming. Now given a string individual characters in the string can be accessed. So you could say what is the third character in the string and you would get V lowercase V or you could also access and use the string as a whole. That in Dumbo's program we had instructions like output a message string like give input colon. So in C++ program the corresponding thing would be something like C out this redirection operator and then the same string give input. We will see more about what the C out represents and what the statement exactly in C++ what the statement does in C++ but this is the analog of what we would do in Dumbo's program. In Dumbo's program we would say output this string in C++ we will say to C out feed this string as a stream of bytes. The strings in C++ can be declared as an array of characters. So I could say character is an array name and this is the size of the array. So they said that this is the name of the array and that is the size of the array that is how you declare an array of a given size where each element in that array is going to be of this data type char here. And now suppose you look at this array of characters and suppose I ask you can we store this string give input colon space which has 12 characters in all in this array. So what do you think it would be? The answer is no because there are only 12 spaces in this array this string has 12 characters and we saw that every string needs to be terminated by a special character. So in this case we will not be able to store this 12 characters in this array of size 12 because the string needs to be terminated by an additional character and we do not have space for that. So to really store a 12 character string the array size must be 13 or more. A string can also be declared as a string data type and in fact in C++ programming that is the preferred choice. For example I could say this variable is of data type string and it is initialized to this value give input colon space that is the value of the string. Now this string data type is certainly an array of characters but it is a bit beyond that it also has several attributes and we will see later that it is very useful to use these attributes of objects of type string of data type string when we do C++ programming. We can also have constant strings in C++ these are usually sequences of characters and closed in double quotes. So I could have give input colon space the output is colon space hello world exclamation all of these exclamation characters are also parts of the string. I could also say const this is the keyword that we have seen earlier string my message hello world. So this defines this to be a constant string and this is the value of the string and because it is a constant string its value cannot be changed during program execution. Now how do we represent booleans inside a computer? The booleans are just like integers where we are only interested in whether they have the value 0 or non-zero. So if it has the value 0 we say that the boolean has the value false if it has a non-zero value we say that it is a true value this 37 could mean true and also 103 could mean true any non-zero value would mean true. Now earlier versions of C++ really used int to store booleans but modern versions of C++ actually implement a separate data type which is called boole and really this would be the preferred choice when programming in C++ internally it uses a form of int to store 0 and non-zero values. How do you declare it in C++? You say boole and then the name of the boolean variable in this case the name of the variable is flag. The boolean constants in C++ there are just two boolean constants true and false. False is guaranteed to be 0 true is guaranteed to be something other than 0 it may not be 1 and I could define constant boolean values like constant boole true value. So this is the constant boolean value which has the value true and because I have this keyword constant here I cannot change the value of true value in my program. Now if you were to put all of this together all the different data types that we have studied we could try to write a C++ program that reads two numbers from the keyboard adds them and displays the result on console. Now remember we had done something similar with Dumbo and this is how Dumbo's program would look like. We would say that use locations A, B, C and then we would ask it to output this message give two numbers then we would ask it to input these two values A and B add them up output the message sum is and then output the value of the sum after that and the report the job is done. In C++ we are going to do something similar a similar sequence of instructions except we are going to use the syntax of C++ so this is how our C++ program would look like. We would have int main we will look at what this mean later on this is really saying that this is the main program that I am asking the computer to execute. Instead of use locations A, B, C I would say that I need three variables of type integer instead of saying output give two numbers I would say C out and then to it I would redirect the constant string give two numbers instead of saying input A input B I would use C in this is the common input which is our keyboard and from the common input I want to read A and B then I want to sum up A and B and store the value in C and instead of saying output sum is and then outputting C I would say in C out please print the string sum is and then print the value of C and then return 0. So we will of course see more about each of these parts of the program later but this is just to give you a glimpse of our very first C++ program. So in summary what did we study today we saw the representation of characters and strings and their declaration in C++ we saw representation of Booleans and their declaration C++ and we saw an example of how from Dumbo's program we can go to a C++ program. Thank you.