Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,090 --> 00:00:01,800
Welcome back, ladies and gentlemen.
2
00:00:01,830 --> 00:00:07,530
And in this video, we are going to talk about format specification, and that's actually a very important
3
00:00:07,530 --> 00:00:13,110
video, a very important topic, because it's the bases and the core for some of the following sections
4
00:00:13,110 --> 00:00:15,240
that we're about to learning this course.
5
00:00:15,450 --> 00:00:21,660
So in the previous exercises, we've seen that we can use the printf command to display different things
6
00:00:21,660 --> 00:00:26,510
on the screen, different text that was just specified between these double quotes.
7
00:00:26,520 --> 00:00:26,840
Right.
8
00:00:27,030 --> 00:00:33,510
And suppose that you have these following line of code print f I'm 20 years old, a simple line that
9
00:00:33,510 --> 00:00:35,340
displays your age on the screen.
10
00:00:35,580 --> 00:00:39,600
And next year you're going to be, what, like 21 years old?
11
00:00:39,600 --> 00:00:40,010
Right.
12
00:00:40,500 --> 00:00:43,980
Then you could just use print F I'm 21 years old.
13
00:00:44,160 --> 00:00:49,590
And if you would run it, you would simply get the same result just with twenty one years old instead
14
00:00:49,590 --> 00:00:50,670
of 20 years old.
15
00:00:50,850 --> 00:00:53,840
So these two lines are pretty much the same.
16
00:00:53,840 --> 00:00:55,560
We have the print F Command.
17
00:00:55,920 --> 00:01:00,240
And we also have some text, some strain between these double quotes.
18
00:01:00,480 --> 00:01:06,990
And all I want to do is to simply sort of separate this age of these 21 or 20 years old.
19
00:01:07,290 --> 00:01:13,680
This number out of this drink text that buring between these double quotes, something like that.
20
00:01:13,800 --> 00:01:16,830
Simply take a look at this one print f I am.
21
00:01:17,850 --> 00:01:24,660
Resolved and then use a comma and after the comma, just the number that we extracted out of here.
22
00:01:24,840 --> 00:01:31,500
And this percentage deal is just some sort of a place holder which will be simply replaced by the number
23
00:01:31,770 --> 00:01:33,150
in our case, 20.
24
00:01:33,570 --> 00:01:35,610
That comes right after the first comma.
25
00:01:35,790 --> 00:01:41,880
So if you will try to build an round this program, you will see the same vexed, the same resolve displayed
26
00:01:41,880 --> 00:01:44,370
on your screen as you've seen previously.
27
00:01:44,400 --> 00:01:46,110
I am 20 years old.
28
00:01:46,350 --> 00:01:47,100
It's the same.
29
00:01:47,100 --> 00:01:47,610
Right.
30
00:01:47,730 --> 00:01:54,990
So what happened here is just that these percentage, the this place holder was replaced by the number
31
00:01:54,990 --> 00:01:57,540
20, which appears here right after the comma.
32
00:01:57,780 --> 00:02:04,080
And if we want to change this line to support in the next year, then we should do something like this.
33
00:02:04,320 --> 00:02:10,020
We should use print F I am percentage the resolve and just use 21 after the comma.
34
00:02:10,230 --> 00:02:16,140
So the percentage they will be replaced by the first number that comes right up through the double quotes
35
00:02:16,140 --> 00:02:18,030
right after the double quotes, comma.
36
00:02:18,300 --> 00:02:24,780
And then this number, these 21 will be replaced here and it will be printed out to the screen.
37
00:02:24,870 --> 00:02:27,260
I am 21 years old.
38
00:02:27,480 --> 00:02:31,620
We simply tried to print the text that is within these devil quotes.
39
00:02:32,040 --> 00:02:39,180
And we just need to replace these occurrences of percentage something percentage D or a percentage something
40
00:02:39,180 --> 00:02:45,690
else with the corresponding value coming after the right double quotes or right after these first comma.
41
00:02:45,960 --> 00:02:46,710
Good, good, good.
42
00:02:46,740 --> 00:02:48,000
First of all, it works.
43
00:02:48,030 --> 00:02:52,080
And that's also an accomplishment right now in the next section.
44
00:02:52,110 --> 00:02:58,560
This thing, this whole reason why we use the format specification will become much more clearer to
45
00:02:58,560 --> 00:03:04,920
you because we are going to study what is an input in how to read any input from the user.
46
00:03:05,460 --> 00:03:10,980
And in this section, we will also understand the importance and how it's being used.
47
00:03:12,060 --> 00:03:14,550
All of this format specification thing.
48
00:03:14,850 --> 00:03:15,480
All right.
49
00:03:15,510 --> 00:03:18,420
So now let's take a look at a couple of more examples.
50
00:03:18,600 --> 00:03:22,770
So in the first example, we will be using more than just one place holder.
51
00:03:22,980 --> 00:03:29,040
So here we have basically two place holders to percentage there, right here and here.
52
00:03:29,250 --> 00:03:34,290
And both of them are going to be replaced with some values before being displayed on the screen.
53
00:03:34,290 --> 00:03:34,650
Right.
54
00:03:34,890 --> 00:03:41,040
So for the first percentage, the place holder will have its corresponding first number coming right
55
00:03:41,040 --> 00:03:45,360
after these Dabic, these devil quotes and these first comma.
56
00:03:45,840 --> 00:03:46,790
It's 20.
57
00:03:46,860 --> 00:03:47,790
The number is 20.
58
00:03:48,060 --> 00:03:51,710
And 20 is going to replace these first percentage deals.
59
00:03:51,960 --> 00:03:55,940
So when printed out on the screen instead of these first percentages.
60
00:03:56,590 --> 00:03:59,400
We will just see the number twenty.
61
00:03:59,580 --> 00:04:01,620
And that's for the second percentage.
62
00:04:02,550 --> 00:04:06,580
It will be replaced by the second number here, which is 21.
63
00:04:06,900 --> 00:04:07,800
Is that clear?
64
00:04:08,040 --> 00:04:11,220
There was nothing hard there or complicated so far.
65
00:04:11,580 --> 00:04:18,420
Simply printing out some text on the screen by using some new guy that we've just introduced, which
66
00:04:18,420 --> 00:04:20,250
is called a place holder.
67
00:04:20,430 --> 00:04:23,280
So let's try to run it and see what happens.
68
00:04:23,460 --> 00:04:24,060
All right.
69
00:04:24,090 --> 00:04:28,950
So today I'm 20 years old, and next year I'm going to be 21 years old.
70
00:04:29,220 --> 00:04:31,300
So twenty four this percentage the.
71
00:04:31,560 --> 00:04:33,850
And twenty one for these present entity.
72
00:04:34,080 --> 00:04:40,260
And also, please note that we haven't seen a new line here separating between the real text and these
73
00:04:40,590 --> 00:04:41,400
default text.
74
00:04:41,700 --> 00:04:44,910
And the reason is because we didn't use a backslash in here.
75
00:04:45,510 --> 00:04:52,530
So that's just was a quick reminder for you to not forget using Backslash N at the end of the text.
76
00:04:52,650 --> 00:04:58,830
Now, for the second example, let's assume their tower average grade is something like ninety three
77
00:04:58,830 --> 00:04:59,820
point seven.
78
00:05:00,150 --> 00:05:01,700
Well, that's pretty good.
79
00:05:02,160 --> 00:05:04,850
And if that's the case, you're doing pretty great.
80
00:05:05,060 --> 00:05:09,980
So basically now what we would like to do is to print out these grade on the screen.
81
00:05:10,190 --> 00:05:16,940
So basically my average, Grady's percentage, the ninety three point seven, and instead of their percentage,
82
00:05:16,950 --> 00:05:19,910
the place holder would like to bring this number.
83
00:05:20,150 --> 00:05:26,450
And now I want you to open up code blocks and to try to run this line on your own and see what happens.
84
00:05:26,690 --> 00:05:31,460
I guess that ninety three point seven is not exactly what you can see on your screen.
85
00:05:31,460 --> 00:05:31,810
Right.
86
00:05:32,030 --> 00:05:34,190
You've got some strange number, don't you?
87
00:05:34,490 --> 00:05:42,110
And for some of you, the number printed may also be not so strange and will be something like 93 just
88
00:05:42,110 --> 00:05:44,690
without the remainder of point seven.
89
00:05:44,930 --> 00:05:49,740
But clearly, you shouldn't get the expected ninety three point seven on your screen.
90
00:05:50,060 --> 00:05:56,120
So ways that you see guys, these percentage displays holder that we've just used here.
91
00:05:56,480 --> 00:06:02,150
We also use this thing, the previous example for format specification, meaning that these percentage
92
00:06:02,150 --> 00:06:03,200
de was used.
93
00:06:03,350 --> 00:06:09,650
Also, let's say, to replace numbers of integer for math like 20 and 21.
94
00:06:09,860 --> 00:06:17,750
And when you are trying to use these percentage D to re to to replace to be replaced by some sort of
95
00:06:17,750 --> 00:06:25,310
a floating point number, like ninety three point seven, an unexpected behavior and results will probably
96
00:06:25,310 --> 00:06:28,190
be noticed, for example, like you've seen right here.
97
00:06:28,370 --> 00:06:35,000
So if you want to use the place holder and format specification correctly for floating point numbers,
98
00:06:35,480 --> 00:06:39,830
then you should use the percentage F just something like that.
99
00:06:39,860 --> 00:06:44,840
My average grade is percentage F ninety three point seven.
100
00:06:45,090 --> 00:06:50,930
And now if you will try to run it, you will see your grade probably with a lot of vier is with a lot
101
00:06:50,930 --> 00:06:52,250
of zeros at the end.
102
00:06:52,640 --> 00:06:54,200
Which is also K.
103
00:06:54,220 --> 00:06:54,550
Right.
104
00:06:54,610 --> 00:06:57,170
And this is ninety three point seven.
105
00:06:57,470 --> 00:07:02,660
And although there are a lot of zero was at the end, the numbers still remains the same.
106
00:07:02,870 --> 00:07:09,500
And then C land, which you can limit the total amount of zeros after the floating point just by specifying
107
00:07:09,500 --> 00:07:14,780
how many digits you would like to see on the screen after this floating point itself.
108
00:07:14,930 --> 00:07:18,920
So, for example, you specified point one before the F.
109
00:07:19,160 --> 00:07:24,170
And this means that there will be only one digit after the floating point.
110
00:07:24,560 --> 00:07:25,220
Exactly.
111
00:07:25,220 --> 00:07:28,370
As you can see here, there are no zeros after seven.
112
00:07:28,580 --> 00:07:29,540
All right.
113
00:07:29,590 --> 00:07:30,480
Good, good.
114
00:07:30,740 --> 00:07:33,200
Now, you know what is a format specification?
115
00:07:33,230 --> 00:07:41,450
These percentage D percent, a Jaff and many others are simply used as a place holders with some format
116
00:07:41,450 --> 00:07:46,000
specification like we've just discussed, for example, percentage.
117
00:07:46,010 --> 00:07:53,510
They will be used for integers while percentage F for floating point numbers and percentage C for characters.
118
00:07:53,840 --> 00:07:59,390
And many other options are basically available in C programming language.
119
00:07:59,630 --> 00:08:06,290
So I hope that makes sense for you because we are going to use that a lot in many Constellation's during
120
00:08:06,290 --> 00:08:07,190
our course.
121
00:08:07,400 --> 00:08:09,620
So last thing for this video.
122
00:08:09,740 --> 00:08:14,390
We've previously said that practice is one of the main things in these course.
123
00:08:14,750 --> 00:08:18,230
It's necessary for you to write and execute some code on your own.
124
00:08:18,500 --> 00:08:24,740
So I'm going to give you 10 exercises of format specification in the next PDA file.
125
00:08:25,100 --> 00:08:30,830
And I want you to try to take a look at them to think what will be printed to the screen.
126
00:08:31,250 --> 00:08:35,060
And also open up code blocks and try to run it on your own.
127
00:08:35,300 --> 00:08:40,900
Make sure that what you thought that will be printed on the screen is actually printed out.
128
00:08:41,360 --> 00:08:45,380
And if it's the case, so you're a good you know that you cover it up.
129
00:08:45,410 --> 00:08:47,790
This topic in a good way.
130
00:08:47,960 --> 00:08:50,430
But if you see that something went wrong.
131
00:08:50,480 --> 00:08:56,330
Try to figure out why it happened, why the results you see on the screen are not expect.
132
00:08:56,450 --> 00:08:59,360
Not not the same as you would have expected.
133
00:08:59,570 --> 00:09:05,450
And that's one of the most important things in learning C programming guys to practice, practice,
134
00:09:05,480 --> 00:09:07,880
practice, practice, a lot of guys.
135
00:09:08,210 --> 00:09:09,560
So thank you for watching.
136
00:09:09,650 --> 00:09:12,110
And download the PDA file.
13427
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.