Introduction to printf Function with Nested Calls
- In C, the printf function prints formatted output and returns the number of characters printed.
- When printf is used as an argument to another printf, the inner call executes first, printing its content and returning the character count.
- Example:
printf("%d", printf("%s", "Hello World!"));- Inner printf prints "Hello World!" (12 characters) and returns 12.
- Outer printf then prints "12" as the integer.
- Resulting output:
Hello World!12.
String Width Specifiers in printf
%sprints a string as-is.%10sprints the string right-aligned within a 10-character field.- If the string is shorter, spaces are added on the left.
- Example:
printf("%s", "Hello");printsHello.printf("%10s", "Hello");printsHello(5 spaces before "Hello").
- Width specifiers help align outputs in columns for better readability.
Character Variable Overflow and Modulo Arithmetic
- Character variables in C typically hold 8 bits (values 0-255).
- Assigning a value beyond 255 causes overflow by modulo 256.
- Example:
char c = 255;- Increment by 10:
c = c + 10;(value becomes 265). - Actual stored value:
265 % 256 = 9. - Printing
cresults in the character with ASCII code 9.
- Understanding overflow behavior is crucial to avoid unexpected results.
Integer Variable Declarations and Modifiers
int i;andsigned int i;are equivalent;signedis a modifier indicating the integer can store negative values.- Omitting
intas insigned i;is still valid; the compiler assumesintby default. - Similarly,
unsigned i;,long i;, andlong long i;are shorthand forunsigned int i;,long int i;andlong long int i;respectively. - Multiple valid ways exist to declare integer types, allowing flexibility. For deeper insights, see Comprehensive Guide to Integer Data Types and Modifiers in C Programming.
Signed vs Unsigned Integer Arithmetic and Printing
- Unsigned integers cannot represent negative numbers.
- When adding signed and unsigned integers, implicit type conversions occur.
- Example:
unsigned int i = 1;int j = -4;printf("%u", i + j);
- Here,
i + jequals-3mathematically, but printed as unsigned leads to large positive number due to two's complement representation. - Two's complement of -3 in 32-bit is
4294967293. - If
%dformatter was used instead of%u, output would be-3. - This highlights the importance of matching data types and format specifiers exactly. For a detailed explanation, refer to Understanding Integer Range Overflow in Signed and Unsigned Types and Mastering printf in C: Using Placeholders for Variable Output.
Practical Tips for Exam Preparation and Coding
- Carefully read code and format specifiers when analysing output, especially in exams like GATE.
- Recognize that format specifiers affect both output and interpretation of values.
- Understand type promotion rules and overflow behavior for reliable C programming. For foundational knowledge, review Understanding Variables in C Programming: Declaration, Initialization, and Usage.
This summary clarifies advanced aspects of the C programming language related to printing functions, data type behaviour, and arithmetic operations, providing helpful insights for learners and exam candidates alike.
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.
When you nest printf calls in C, the inner printf executes first, printing its content and returning the number of characters printed. The outer printf then prints this returned integer. For example, in printf("%d", printf("%s", "Hello World!")); the inner printf prints "Hello World!" (12 characters), and returns 12, which the outer printf prints as "12", resulting in the output "Hello World!12".
String width specifiers in printf, such as '%10s', ensure that the string is printed right-aligned within a specified field width (10 characters here). If the string is shorter than the width, spaces are added to the left to fill the field. For example, printf("%10s", "Hello"); outputs " Hello" with five leading spaces, useful for aligning text output in columns.
Char variables in C typically store 8-bit values (0-255). Assigning a value beyond 255 causes overflow through modulo 256 arithmetic. For example, if char c = 255; then c = c + 10; results in 265, but the stored value is 265 % 256 = 9. This overflow leads to potentially unexpected characters when printing, so understanding this modulo behavior is essential to avoid bugs.
In C, integer declarations can use modifiers flexibly. For instance, 'int i;' and 'signed int i;' are equivalent because 'signed' explicitly states the integer can be negative. Even 'signed i;' without 'int' is valid as 'int' is implied. Similarly, 'unsigned i;' means 'unsigned int i;'; 'long i;' is 'long int i;'; and 'long long i;' is 'long long int i;'. This flexibility allows programmers to declare integers with different sizes and signedness clearly.
When adding signed and unsigned integers in C, implicit conversions promote the signed integer to unsigned if mixed, causing unexpected results. For example, with 'unsigned int i = 1;' and 'int j = -4;', the expression 'i + j' mathematically equals -3, but printing it with '%u' (unsigned) interprets it as a large positive number like 4294967293 due to two's complement wraparound. Using '%d' for signed integers would print the correct negative value. Therefore, matching format specifiers to data types is crucial to avoid confusion.
To master printf outputs and integer behaviour, carefully analyse format specifiers as they influence both output format and how values are interpreted. Understand type promotion rules, especially in mixed signed and unsigned arithmetic, and be mindful of overflow effects in variables like char. Practising reading and predicting outputs helps in exams like GATE and ensures reliable C programming by preventing common pitfalls related to data types and printing.
Heads up!
This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.
Generate a summary for freeRelated Summaries
Mastering printf in C: Using Placeholders for Variable Output
This lesson explores the printf function in C programming, focusing on how placeholders like %d enable dynamic variable output. Learn how to correctly match placeholders with arguments to display arithmetic expressions and their results effectively.
Comprehensive Guide to Integer Data Types and Modifiers in C Programming
This article explores integer data type modifiers in C, including short, long, signed, and unsigned. Learn about memory size differences, value ranges, and how to use symbolic constants and printf specifiers to work effectively with these data types.
Understanding Octal Values and Macro String Replacement in C Programming
Explore key concepts in C programming including how leading zeros convert numbers to octal and how macros with string values are replaced during preprocessing. Learn why octal values print differently and how to correctly use format specifiers and macros in printf statements.
Understanding Variable Data Types and Operators in C++
Learn about variable data types and operators in C++. Discover syntax, examples, and functions for programming in C++.
Understanding Variables in C Programming: Declaration, Initialization, and Usage
This lesson introduces the concept of variables in C programming, explaining their declaration, definition, initialization, and how to use them effectively. Learn how variables act as memory storage with simple examples and best practices to manage variable values within your programs.
Most Viewed Summaries
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakarang kolonyal ng mga Espanyol sa Pilipinas at ang mga epekto nito sa mga Pilipino.

