C Programming (Important Questions Set 1)
Summary
TLDRThis video script discusses various C programming concepts, including the behavior of printf functions, character data types, and integer arithmetic. It explains how printf can print strings and return the number of characters printed, the impact of using %10s for formatting strings, and the arithmetic modulo operation with character overflow. Additionally, it covers the declaration of integers with modifiers like signed and unsigned, and how the internal representation of integers in computers affects the output of printf statements, emphasizing the importance of careful code analysis for exams like GATE.
Takeaways
- 💻 The script discusses a C program fragment involving nested printf functions and explains their output.
- 🔢 The outer printf function prints the number of characters printed by the inner printf function, which prints 'Hello World!'.
- 📏 The script explains the use of format specifiers like %d for integers and %s for strings in printf functions.
- 📑 It highlights the importance of including the null character when counting the total number of characters in a string.
- 📐 The script demonstrates how printf can be used with padding (%10s) to control the width of the output.
- 🔄 The script explores the concept of data types and arithmetic operations, particularly with character variables.
- 🔒 It explains how character variables can only hold values up to 255 and uses modulo arithmetic to show how values exceeding this are handled.
- 📝 The script discusses the declaration of integer variables in C, including signed and unsigned integers.
- 🔄 It clarifies that omitting the data type for a variable defaults to int, which is implicitly assumed by the compiler.
- 🔢 The script explains the internal representation of negative numbers in computers using 2's complement form.
- 💡 It emphasizes the importance of carefully reading each line of code, especially for understanding questions in competitive exams like GATE.
Q & A
Question 1: What does the first printf function in the program do?
-The first printf function prints 'Hello World!' on the screen, followed by the number of characters in the string, which is 12. Hence, the output is 'Hello World!12'.
Question 2: Why does the second printf function use '%10s' instead of '%s'?
-The '%10s' format specifier ensures that the string is printed within 10 characters wide. Since 'Hello' has 5 characters, 5 white spaces are printed first, followed by 'Hello'. This is used to align the text in a wider field.
Question 3: How does the character variable behave when assigned a value exceeding its range?
-When a character variable is assigned a value exceeding its range (which is 255 for 8 bits), it follows arithmetic modulo 256. For example, assigning 265 results in 265 mod 256, which equals 9. Thus, the value 9 is printed.
Question 4: How does a signed integer differ from an unsigned integer in the context of printf?
-A signed integer allows negative values, while an unsigned integer only handles positive values. When using '%u', the printf function interprets the value as unsigned, potentially printing a large number instead of a negative value, as it interprets the binary form differently.
Question 5: Why does the program print '4294967293' when using '%u'?
-The value -3 is represented in two's complement form, and when printed as an unsigned integer using '%u', the value is interpreted as 4294967293 due to how unsigned integers handle binary representation on a 4-byte machine.
Question 6: What is the significance of arithmetic modulo 2 raised to the power n in the context of character data types?
-For a character variable, which is 8 bits long, the maximum value it can hold is 255 (2^8 - 1). If a value exceeds this range, it wraps around according to modulo arithmetic (value mod 256).
Question 7: Why is 'signed i;' considered a correct definition?
-Even though 'int' is not explicitly mentioned, the compiler implicitly assumes the 'int' data type. Therefore, 'signed i;' is equivalent to 'signed int i;', making it a valid declaration.
Question 8: What is the internal representation of negative integers like -3 in computers?
-Negative integers are represented using two's complement. To get the two's complement of a number, first find the one's complement (invert the bits) and then add 1 to the result. This allows negative numbers to be handled in binary form.
Question 9: What happens if you mix signed and unsigned integers in arithmetic operations?
-When you mix signed and unsigned integers, the result may be unexpected because the unsigned integer takes precedence in most cases, leading to a potential misinterpretation of the result as a large positive number instead of a negative one.
Question 10: Why is it important to carefully read format specifiers like '%d' and '%u' in C programming?
-The format specifier controls how values are printed. '%d' prints signed integers, while '%u' prints unsigned integers. Misusing these specifiers can lead to incorrect output, such as interpreting a negative value as a large positive number, which can lead to confusion.
Outlines
💻 C Program Output Analysis
The paragraph discusses the behavior of C program fragments involving the printf function. It starts with an introduction to the lesson, emphasizing that the content is a review of previous topics. The instructor invites viewers to pause the video and try to understand the code written on the screen before the explanation. The code in question involves a printf function within another printf function, which is unusual. The inner printf uses the format specifier %s to print the string 'Hello World!'. The instructor explains that printf returns the number of characters printed, including the space, totaling 12 characters. The expected output is 'Hello World!12'. The next part of the lesson involves another printf function with the format specifier %10s, which prints the string 'Hello' followed by spaces to fill a field of 10 characters wide. The final part of the paragraph discusses what happens when a character variable is assigned a value beyond its capacity, such as 255, and then incremented. It explains that character variables can hold values up to 255 due to their 8-bit size, and exceeding this results in a modulo operation with 2^8 (256), leading to a remainder of 9, which is what gets printed.
🔢 Understanding Data Types in C
This section delves into the correct definitions of integers in C programming. It starts with a multiple-choice question about which statements correctly define an integer. The instructor explains that the first statement, defining a signed integer, is correct. It clarifies that omitting the 'int' keyword and just using 'signed' is also valid due to compiler assumptions. The paragraph also covers the definitions of 'unsigned', 'long', and 'long long' integers, confirming that all the given options are correct. The discussion then shifts to a program that prints the result of adding an unsigned integer to a negative integer. The instructor explains that the output is not -3 as one might expect, but rather depends on the machine due to the use of the %u format specifier, which interprets the result as an unsigned integer. The maximum value for an unsigned integer is 4294967295 for a 4-byte integer, and the actual output is one less than this maximum value due to the initial value of -4 being added to 1.
📚 Recap and Importance of Careful Reading
The final paragraph serves as a recap of the lesson and emphasizes the importance of careful reading and understanding of each line of code, especially for exams like GATE. It highlights that such questions test the ability to comprehend every detail within a code snippet. The instructor reminds viewers to pay close attention to every line and concludes the lecture with a farewell, indicating the next lecture will continue the discussion.
Mindmap
Keywords
💡printf function
💡format specifier
💡character
💡arithmetic modulo
💡integer
💡unsigned integer
💡2's complement
💡ASCII table
💡GATE examination
💡data type
Highlights
The lesson reviews previous topics and does not introduce new material.
Students are encouraged to pause the video and try to understand the written C program fragment.
A printf function within another printf function is explained, which is unusual.
The inner printf function returns the number of characters printed.
The number of characters in a string includes spaces and should be counted accurately.
The output of the nested printf functions is expected to be 'Hello World !12'.
The concept of format specifiers in printf is explained with %10s.
The %10s format specifier prints a string with leading spaces to fill up to 10 characters wide.
The output of the modified printf with %10s is 'Hello' followed by spaces.
A character variable can hold integer values, and exceeding the range results in modulo arithmetic.
The value 265 assigned to a character variable results in a modulo operation with 256.
The result of 265 mod 256 is 9, which is printed on the screen.
The definition of integers is discussed, including signed and unsigned modifiers.
Compiler assumptions about data types for variables like 'signed i;' are explained.
All the given statements about integer definitions are correct.
The output of a program with unsigned and signed integers is machine-dependent.
The internal representation of -3 in computer using 2's complement form is explained.
The difference between printing with %d and %u in printf changes the output significantly.
Careful reading of each line is emphasized for understanding and answering questions correctly.
The importance of such questions in GATE examinations is highlighted.
Transcripts
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.
تصفح المزيد من مقاطع الفيديو ذات الصلة
01 13 Formatting with Printf
Go (Golang) Tutorial #4 - Printing & Formatting Strings
C_64 Strings in C- part 3 | printf and puts function in C
Data Types Variables And Operators In Python | Python Fundamentals | Great Learning
Golang Tutorial - Simple Values - 5
C# Primitive Types and Variables | Datatype Literals and Variables | C# Tutorial | Simplilearn
5.0 / 5 (0 votes)