 Hello and welcome to the second week first lecture sequence of the NPTEL course on an introduction to programming through C++. What I am going to do today is something like the following. I am going to talk about how to perform some basic operations that are needed in all programs. So the first amongst these is using the memory of the computer. How do you store numbers into the memory of a computer? Then how do you read numbers from the keyboard into the memory? How do you print the numbers in memory onto the screen? How do you perform arithmetic? Clearly these operations are required in all programs and they sort of form the basis of whatever we are going to study next and we are going to talk about all this today. And during the lecture and towards the end we will see programs based on all of this. So the first task is reserving memory for storing numbers. So before you want to store numbers into the memory you must explicitly reserve space for storing them. And in this course whenever we talk about space we will need we will need the region of memory. And this reservation is done by a variable definition statement. The term variable is used to denote the space that you have reserved. It is the name of the region of memory and you get to decide this name. How? We will tell you in a minute. The value of a variable likewise means the value stored in that region of memory that you just reserved. And in addition to defining the variable while giving its name you also must state what kind of values will be stored in the variable. So basically these values are indicative of the type, the type of the data and we will see this in a minute. So a statement for creating variables or defining variables. So this is the form, data type name and name of the variable. We have already done this in chapter 1 or in lecture 1 and the example that we had there was int insights, int here is the data type name and it is short for integer. It indicates that the values that we are talking about storing are going to be integers. So this statement is going to reserve space for storing integer values positive or negative of a standard size and the standard size these days tends to be 32 bits on most computers and the values that are being stored in this variable are going to be interpreted as 2s complement values. So this is sort of like having a sign bit and then a magnitude but it is not quite but if you think about it as sign bit and magnitude that is okay. Insights is the name to be given to this region of memory that has been reserved for us or the region of memory is also called the variable that we just created and so insights is the name of that variable. Now names in C++ including names of variables are called identifiers. So an identifier can be a sequence of one or more letters, digits and the underscore character. Now a name cannot begin with a digit and some words such as int cannot be used as variable names. So these terms int and so on are reserved by C++ for its own use and so they cannot be used as identifiers. In C++ case is important. So whether you write something in the lower case or upper case does make a difference. So I can have capital A, B, C as an identifier or I can have little A, B, C as an identifier and these will be considered different identifiers and the only characters used to make up a name are the ones mentioned above letters, digits and underscore. So this means for example that you cannot have space inside the name of a character or once you give a name to a character you cannot break it up. If you break it up it might mean something else. You cannot break it up by putting spaces. So here are some examples. Insights is an example we have already seen. You can have a name called telephone underscore number. You can have a name called X, you can have a name called X123. You could have a name or an identifier called third underscore cousin. What is not allowed however as identifiers or names of variables are something like hash sites. This is not allowed because hash is not allowed in identifiers. Third underscore cousin looks okay but it begins with a digit and we have to abide by the rule that names of identifiers cannot begin with digits. And three space already space cousin is not a valid identifier because it contains spaces. Now this gives you a lot of choice in what to select as names and you are urged to use meaningful names. So basically the name should describe the purpose for which the variable is going to be used. Besides the int data type C++ has several other data types. So for example there is the unsigned int data type. This is used for storing integers which are guaranteed to be positive or alternately whatever is stored in such a variable will be of course a bit sequence as always but it will be interpreted as a non-negative value and typically one word will be allocated or 32 bits will be allocated and in these 32 bits whatever is stored will be interpreted as an ordinary binary number. Car is also a kind of integer but it is a small variable and it is used for storing characters or small integers. So it only has one byte width so it only consists of 8 bits. And often the value which is stored in it will be interpreted as the ASCII code of some character. So we will see this in a minute. Float is a data type which is used for storing real numbers and typically one word will be allocated and this word will be used as per the IEEE floating point representation using 8 bits from the world as exponent and 24 bits for the significant. The double is another data type of C++ and this is also for storing real numbers. In this case 2 words will be allocated and these will be in floating point and 11 bits will be used for exponent and 53 bits will be used for a significant. So here for example is a name, telephone number which I can give for an unsigned int variable. I might want to have variables to store mass and acceleration and these clearly will be real numbers and I could define them by writing float mass, acceleration. So here in a single statement I am defining several variables of the same type though. Now along with these definitions I can attach a keyword long and long basically says that I really want a bigger variable or I want more precise numbers or something like that. So give me more than the usual space. So I could have long unsigned int cryptographic password and this might very likely give me 64 words of storage. So it is unsigned int so I really get to store 64 bit binary numbers in such a variable. Long double more precise acceleration defines a variable which I have just decided to call more precise acceleration. And since double is already 64 bits long double will probably give me something like 96 bits. So the exact values of this you will know by looking at the manual for the language or really the manual for the compiler that you are using or the standard. Now along with define defining a variable you can also ask that a certain value be stored in it. So this value will be stored right at the time the variable is created. So for example I could write int i equal to 0 and result in this the variable i I have chosen to initialize whereas result is uninitialized. I can also write float vx 1.0 v by 2.0 e5 and v8. So in this vx and vy have been initialized and note that 2.0 e5 is how you write the floating point number or the number 2.0 into 10 to the power 5 of scientific notation. So that value will get stored in vy, weight will not get an initial value. Now as you can see although inside the computer the binary representation is used when you communicate with the computer when you tell what to store you are just using ordinary decimal notation scientific notation or whatever it is. Here is another initialization am I set a variable called command to the character f while defining it. So here I am defining it as a type as of type character and inside it I am storing the character f. Now this quote f quote is something called a character constant and these three characters together this string of characters really simply means the ASCII value of the quoted character. So this happens to be 102 and so really what is going to be stored this ASCII value there is also keyword called const and I can attach it to a variable definition and here for example I am saying const double avogadro 6.022 e23. So this says that I do want space of type double to store avogadro but I am not going to modify this value once I have stored in it. So normally the value stored in a variable can be changed but here I am just telling I am just declaring beforehand that look I do not intend to change this value and therefore if my mistake I change it then the compiler will bond. I can read values into variables and you have seen one example in chapter 1. So I can write c in insides so this will wait for the user to type in a value from the keyboard and the type value will be stored into insides and I do not have to just read one value I can read several values in the same statement. So I could write c in vx vy so this will expect that the user will type in two values the first of those values will go into vx the second will go into vy. Now when the user types something when the computer is waiting for vx or vy or the computer is waiting for a floating point number it is expected that the user will type a floating point number or the user will type an integer well when a computer is waiting for a number it really does not matter what kind of number a user types it is so long as the user types a number but the user should not type other characters. So the user should not type a because a does not mean anything as a number so it is important that when you issue such a command when such a command executes the user types in a number of a value of the current of the correct type while typing in values the user can put in characters which are often called white space characters and these are simply the space character tabs new lines and these serve to delimit two values. So after typing in the value for vx the user can type a space or the user should type a space before typing in vy but the user could type as many spaces as he or she wants or the user could type in a new line or several new lines so how many new lines how many spaces are typed is ignored but some white space has to be there between two values inside the typing of a single value of course you cannot put in any white space and this is a little bit of a tricky point for beginners initially just because I typed 1.0 does not mean that that 1.0 value is accepted to make the computer accept that value you have to hit the new line after this if you are typing in several values the new the new line could be hit after typing in the last one that is perfectly fine. And finally if you are reading values into a care type variable then it is acceptable to type a character and what goes into the variable is the ASCII value of that character. So if I define a care variable called command and if I execute the statement c in greater than greater than command then c++ will wait for the user to type in a character even here if the user types white space that will be ignored but anything other than these white space characters if the user types that value that character the ASCII value of that character will go into this variable called command. So for example if you type the character f its ASCII value 102 will get stored into f printing variables on the screen is reasonably simple again we have seen an example of it in chapter 1. My general form is c out less than less than and the name of a variable or it could be a message as well and if I want a new line to be printed I use the reserved keyword and l e and d l and I can print a message and I just have to enclose the text inside quotes if I want to print several things I can do that I just have to put this less than less than between those things that I want to print the less than less than as well as the greater than greater than is actually some kind of an operator. So I might say later on that the less than less than operator or the greater than greater than operator. So here for example is a statement which is going to cause the message position to be printed and of course the quotes will not be printed after that the values of the variables x and y will be printed with a comma in between them and then a new line will be printed. If you print a char variable then the content is interpreted as an ASCII code not as a number and the corresponding character is printed. So here for example I am creating a variable command in which I am storing letter g and a second character variable called command 2 into which I am storing the number 97 but if you remember 97 is the ASCII code of the little case letter a. So I could have written command 2 equal to quote a quote as well. In any case if I print out these two characters by writing c out less than less than command less than less than command 2 then whatever is the content of these variables that content will be interpreted as an ASCII code and the corresponding letter will be printed. So of course command contains the ASCII code of g and so the letter g will be printed. Command 2 contains the ASCII code of letter little a and therefore g little a will be printed on the screen. Now when you want output to appear on the screen you really need to put an endel and only an endel forces the output to appear until then C++ may just keep collecting whatever you are asking you to print but C++ is forced to print it if you print a new line so that is sort of the signal to C++ saying that look whatever you have been collecting print now or C++ will print whatever it is collecting if you decide to read something. So if you are giving a message and then reading something yes the message will appear and then you can type after the message. So here is a quick exercise for you. So create double variables temperature pressure and initialize pressure to 0. Create a constant double variable pi initialize to 3.141592 and the idea here is that from then on you can use capital PI without having to remember what the value of pi is. Create a care variable y or no which is initialized to the character y and here is something that you need to think about often what is what name should I give and here the question is what name would you give to a variable in variable which is meant to store the number of students in a class. So what have we discussed in this segment of this lecture sequence? So we first discussed how to define variables, we discussed how to initialize variables, we discussed how to read a value into a variable and print the value of a variable. So we will take a break here.