Today we are going to consider some questions on the topics that we had already studied in our previous lessons. These questions are related to the previous topics that we had already studied. Therefore there is nothing new, if something is new, I will talk about that in this lesson. 1. What is the output of the following C program fragment: Before I will explain the program, I would recommend to you to please pause the video for a while, and try to understand whatever I have written over here. Then come back again and see what I explained in this particular session.
OK, Here you see, I have written a printf function and inside this printf function I have written two arguments. One is %d, as we all know, this is a format specifier used to print an integer and another argument is printf function. Now this is something weird. In this argument we usually write a variable or some integer value but here, instead of that I have written printf function. Now what does this printf function do Inside this printf function, I have provided the argument as %s. Now this is something new to you. %s means, we need to print string of characters on to the screen.
%s is used to print "string of characters." Here I have provided, a string of characters as "Hello World !" If you want to print this "Hello World !" on the screen, then we have to use %s. OK, what this printf function will do is that it prints "Hello World !" on screen. This is OK. But what does this printf function do? What does it print? printf not only prints the content on the screen. It also returns the number of characters that it successfully prints on the screen. Now this is quite important for us to know this printf function will not only print "Hello World !" on the screen, but it also returns the value which is equivalent to the number of characters it successfully prints on the screen.
Here you can see, the number of characters in this string is 1 2 3 4 5 6 You have to include that blank character as well. Don't forget to include the blank character. OK, up till now there are total 6 characters. After this, 7 8 9 10 11 and then 12. There are total 12 character that we need to print on the screen. Right? So this printf function will also return 12 to this particular printf function as second argument. So this printf function will print 12 and this printf function prints "Hello World !" on the screen. Therefore the output is "Hello World !12" That is what is expected.
Let's consider the next question. What is the output of the following C program fragment: Kindly pause the video for a while and try to understand what this printf is going to print on the screen. OK here, I have provided the first argument as %10s instead of %s. And second argument is "Hello" Now what would be the output provided by this printf function Let's try to understand by executing it on Code blocks. Here I changed the code a little bit to actually differentiate between these two statements. Let's execute the code. First printf prints Hello simply on the screen. while the second printf prints Hello but after some white spaces. This is because of this 10. This 10 means print the characters up to 10 characters wide. This means, here in our example we just have 5 characters to print. Now because of the shortage of characters, it will print first white spaces and then the rest of the 5 characters. 5 white spaces printed and after that 5 characters gets printed. That is the reason why %10s is used.
If you want to print string up to 10 characters wide, then we use %10s. If we want 15 characters wide, then we write 15 instead of 10. That is the difference between this one and this one. Let's consider the next question. What is the output of the following C program fragment: Here is this program, let's try to understand what does it really do. Here I have provided a character variable c and assigned it a value 255. You can assign integer values to a character variable.
As I already told you, in the lesson when I taught you about the character variable. Here you can see, I have provided an integer value to this character variable and after that I increment that value to 10 and store it again in this variable. Here after the increment this will be 265. and I simply print it on to the screen. Can you guess what would be the output of this printf statement? Is it 265 Is it some character according to ASCII table Is it 7 or is it 9.
As we know this is 255, character is capable of holding the information up to 1 byte only which is equal to 8 bits. 8 bits means the maximum value that it can hold is 255. And cannot be more than that. If we try to exceed the range, like in this case we are exceeding the range because we are assigning to this c variable 265. If we try to exceed the range, then what would happen is that, as we know this thing that all the data types like character, integer, float, long, double they all follow or you can say, they obey the laws of arithmetic modulo 2 raised to the power n where n is equal to 8 in this case. As character data type is of 8 bits long, therefore n is equal to 8. Here in this case, the value is 265 therefore we will calculate 265 mod 2 raised to the power 8. 2 raised to power 8 is equal to 256. Therefore it is 265 mod 256 which is equal to 9.
When we divide 265 by 256 we get the remainder as 9, that is why, 9 is printed on the screen. And therefore the answer is option d. Let's consider this question. Which of the following statement/ statements is/ are correct corresponding to the definition of the integer: As we know how to define or declare an integer. Here are some statements written. We have to determine that which of the following statements or statement is correct corresponding to the definition of integer. Is it only I and V ? Is it just I ? Are all correct? Or just IV V and VI are correct? Take you time and try to interpret this one by one.
OK. Let's understand them one by one. The first interpretation is correct. This is the actual definition of a signed integer. You can also write int i; But instead of int i you can also write signed int i;. Both are one and the same thing. Here writing signed int i; int is a data type and signed is our modifier for integer. Therefore this is a correct definition. Apart from that, in this second statement, I have written signed i; instead of signed int i; Now this is something weird. I have not written anything like int data type or some character data type or any other data type here in this case. Therefore it seems like a incorrect definition but I would like to tell you that this is not an incorrect definition.
It is correct. Because compiler implicitly assume integer data type. If you have not written any data type over here, then compiler will assume this data type is integer over here. Therefore signed i; is equivalent to signed int i; Similarly unsigned i; is also correct. Because compiler implicitly assumes as integer therefore it automatically puts int in front of this variable. And unsigned i; is also a correct definition. On the other hand long i; is also a correct definition, that is equivalent to long int i; and long long i; is equivalent to long long int i; Therefore we can say, option c is the correct answer. Because all of them are actually correct.
Therefore C is the correct answer. Now let us see what this program prints. Does it print garbage value? Does it print -3? Or does it print integer value that depends from machine to machine? Or is it none of the above? Ok, in the first statement itself, I have written unsigned i equal to 1; that is equivalent to unsigned int i; Automatically compiler puts int in front of this variable. Therefore there is no problem. unsigned int i is equal to 1 means that this integer variable is going to have a value equal to 1. in the next statement, I have declared a variable j and assigned it a value -4. Inside this printf function I have written i+j. This is an arithmetic expression, and the result is getting printed. using this printf function. i+j is equal to 1-4 which is equal to -3, and that is getting printed. You might think the answer is -3 but it is not. Let's try to understand what is the reason. But before that, we must have to know what would be the internal representation of -3 in computer.
Internally -3 is represented in 2's complement form. Here I have written certain steps, to know how we can represent -3 internally in computer. You first have to take 1's complement of 3. That is, This is the binary representation of 3 as this value is set to 1 and this is also set to 1 which is (2 raised to power 1) + (2 raised to power 0) that is equal to 3. And then we perform 1's complement of 3. which is nothing but changing the 0's to 1's and 1's to 0's. Here in this case I change all the 0's to 1's and 1's to 0's. And this gives me the answer which is 1's complement of 3. Then in order to produce the 2's complement of 3, we need to add 1 to the result of this 1's complement, which gives us this answer. Now, this would be the binary representation of -3. You can say internally that is represented in our modern computers. OK, now if I have written %d instead of %u this will print the answer -3. Now because I have written %u this will be interpreted as an unsigned integer. And we know this thing that unsigned integers' maximum value is 4294967295 in the case when integer is of 4 bytes long.
We know this thing. Here you can say not all the bits are equal to 1. This bit is not equal to 1. If this is 1, then this would be 4294967295. Because this is not set and its place value is 2 raised to the power 1 which is equal to 2. Therefore we need to subtract, 2 from the maximum value 4294967295. This gives us the answer as 4294967293. This is on my computer. Basically in my computer, because the size of integer is of 4 bytes, Therefore the maximum value is 4294967295 and subtracting 2 from it will give me the answer 4294967293.
May be this is possible in your computer size of integer is of 2 bytes, then maximum value would be different and subtracting 2 from it would give something else. Note down this thing. Because of %u, the output printed is, integer value that depends from machine to machine. And therefore the answer is c. But if it is %d, the output would be -3. And that is the major difference between these things From this example we have to know this thing, and we have to understand this thing, that we need to actually read each and every line very carefully. when we are answering the questions like this In GATE examination, such things are very critical and very frequent. They are checking your ability of understanding each and every line. Therefore such kinds of questions are frequent in GATE examinations.
OK friends, this is it for now. See you in the next lecture. Bye..