+ 1

What will be the output in both printf?

Char name=" Anil Kumar Gupta"; Printf("%5s\n",name); Printf ("%.5s\n",name); We have define the width of block i.e. 5 so what should it print in both..

23rd Aug 2019, 6:50 AM
Chetan Mude
Chetan Mude - avatar
13 Respuestas
+ 2
Chetan Mude Yes, you are right..let me give you an example.. Consider a string “Hello".. Length is 5 Now what basically compiler do when you specify width is that it leaves spaces equal to the width specified..And will start writing in right justified manner (if width is negative then left justified)..So there will be No change in output when width is equal to or less than string..The output will change when width is greater than string length.. When you write %10s -----Hello This will happen.. it will leave 10 spaces and will write in right justified manner.. (not like ----------Hello) If you specify width 3..then there will be no space for “Space" so it shows output as it is..
23rd Aug 2019, 12:36 PM
Alaska
Alaska - avatar
+ 1
Based on your code provided, Char name=" Anil Kumar Gupta" is wrong because when you declare the char, you have to assign array to store each of the character. So, Char name[100]=" Anil Kumar Gupta", declare the size of array, Then the output will be same with name.
23rd Aug 2019, 7:12 AM
Uchiha Itachi
Uchiha Itachi - avatar
+ 1
If printf("%5s\n",name) it should be printing whole Anil Kumar Gupta. Correct.
23rd Aug 2019, 7:23 AM
Uchiha Itachi
Uchiha Itachi - avatar
+ 1
printf("%s\n",name); this is to output all the character of name. %s is for string and character. if %5s, the number 5 is for spacing, it wont affect the words
23rd Aug 2019, 7:30 AM
Uchiha Itachi
Uchiha Itachi - avatar
+ 1
This are the concepts of formatted output.. Lets talk about 1st printf..In first print you have written %5s..This will leave 5 spaces from left and will write whole string(In your case it will give error or null bcoz you have declared character).. Now for 2nd printf..You have written %.5s This will tell the compiler that width is of only 5 characters..so it will only print Anil and 1 space ..So basically only number is used for spacing and using number with decimal point is for width..
23rd Aug 2019, 10:02 AM
Alaska
Alaska - avatar
0
Here can help you https://code.sololearn.com/#c Good Luck~
23rd Aug 2019, 7:00 AM
Uchiha Itachi
Uchiha Itachi - avatar
0
Actually i want explaination
23rd Aug 2019, 7:01 AM
Chetan Mude
Chetan Mude - avatar
0
Let declare size of array as 20 or 100 as u declare but we customize size as 5 so why it print Anil Kumar Gupta instead only Anil?Uchiha Itachi
23rd Aug 2019, 7:15 AM
Chetan Mude
Chetan Mude - avatar
0
For the 1st printf, it will output all. For the 2nd printf, it only show Anil because of %.5s that decimal
23rd Aug 2019, 7:16 AM
Uchiha Itachi
Uchiha Itachi - avatar
0
But its printing whole Anil Kumar Gupta.. check the code Uchiha Itachi
23rd Aug 2019, 7:18 AM
Chetan Mude
Chetan Mude - avatar
0
I mean to say printf("%5s\n",name) Not .5 Uchiha Itachi
23rd Aug 2019, 7:21 AM
Chetan Mude
Chetan Mude - avatar
0
Phew can you explain why? (Thats the real question?)😂 Uchiha Itachi
23rd Aug 2019, 7:28 AM
Chetan Mude
Chetan Mude - avatar
0
But when you write code it prints whole but leaves no space when u write 17 instead of 5 (size of character is 16)then its leaves space... I just checked now keeping 1 to 16 its shows same result and same distance..AnonymousGuy
23rd Aug 2019, 11:11 AM
Chetan Mude
Chetan Mude - avatar