Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:01,810
What is going on, guys?
2
00:00:01,860 --> 00:00:07,650
And in this video, we're going to talk about how to use variables in C programming language.
3
00:00:07,980 --> 00:00:13,770
We are going to talk about variables, declaration, variables, assignment, and also how to read an
4
00:00:13,770 --> 00:00:15,120
input from the user.
5
00:00:15,510 --> 00:00:20,190
So in the previous media, we've talked about how variables are similar to boxes.
6
00:00:20,610 --> 00:00:23,970
Now let's understand how we can create variables.
7
00:00:24,210 --> 00:00:26,880
So let's introduce the variables declaration.
8
00:00:27,100 --> 00:00:30,810
This will usually be the first step for working with variables.
9
00:00:30,810 --> 00:00:35,430
And that makes sense because variable declaration is the same as box creation.
10
00:00:35,730 --> 00:00:42,180
So variable declaration is equivalent to variable creation and it simply states that you just create
11
00:00:42,180 --> 00:00:45,200
some variable in the memory of your computer.
12
00:00:45,510 --> 00:00:48,210
And how do you create a variable in C language?
13
00:00:48,630 --> 00:00:50,640
The structure is very straightforward.
14
00:00:51,000 --> 00:00:55,620
First of all, you specify the type of the variable and then you specify its name.
15
00:00:55,950 --> 00:00:58,260
And of course, at the end you add a semicolon.
16
00:00:58,470 --> 00:01:05,400
For example, if we wanted to create a variable of type integer that will store our age, we would first
17
00:01:05,400 --> 00:01:11,220
of all specify its type, which is Ent., and then specify its name, for example, age.
18
00:01:11,430 --> 00:01:16,410
So we have a variable named age and it is of type end.
19
00:01:16,650 --> 00:01:24,900
So once again, this way you created a variable called age and it's of type int to hald integer values.
20
00:01:25,110 --> 00:01:30,150
And let's say you wanted to create a variable of type double, which is used to hold floating point
21
00:01:30,150 --> 00:01:30,840
values.
22
00:01:31,230 --> 00:01:36,990
So in this case, the type of the variable and the declaration phase should be specified as double.
23
00:01:37,300 --> 00:01:43,140
Then in the same way we will give this variable and name, for example, temp, which will be used to
24
00:01:43,140 --> 00:01:46,800
hold the temperature in Celsius or foreigner degrees.
25
00:01:47,340 --> 00:01:49,020
So just use double temp.
26
00:01:49,070 --> 00:01:50,250
Double is the type.
27
00:01:50,340 --> 00:01:57,150
Temp is the name of the variable and the semicolon at the end to specify that we've done with our variable
28
00:01:57,150 --> 00:01:57,770
declarations.
29
00:01:58,050 --> 00:02:03,600
So this structure for creating variables in ceiling, which will be pretty much always the same.
30
00:02:03,930 --> 00:02:07,410
You first of all, specify the type of the variable to be created.
31
00:02:07,770 --> 00:02:10,020
Then you specify the variables name.
32
00:02:10,350 --> 00:02:13,140
And at the end, of course, you add a semicolon.
33
00:02:13,350 --> 00:02:19,770
So to summarize this thing a little bit, whenever you want to work with a variable, you need, first
34
00:02:19,770 --> 00:02:20,880
of all, to create it.
35
00:02:21,180 --> 00:02:26,100
And how can you created by using variable declaration, specify its type.
36
00:02:26,310 --> 00:02:29,100
Then give its name at a semicolon at the end.
37
00:02:29,460 --> 00:02:31,380
And your variable is ready to go.
38
00:02:32,220 --> 00:02:35,040
So now let's talk about an assignment operation.
39
00:02:35,460 --> 00:02:39,870
So once you have your variable, you may want to put something inside of it, right?
40
00:02:40,050 --> 00:02:42,480
Simply to let it hold some value.
41
00:02:42,570 --> 00:02:44,760
That's what variables are all about.
42
00:02:44,930 --> 00:02:51,270
And this operation, when you want to put some value in a variable, is called an assignment to assign
43
00:02:51,270 --> 00:02:53,550
some value to a variable.
44
00:02:53,790 --> 00:02:57,240
So how should it be done in our C programming language?
45
00:02:57,690 --> 00:03:04,260
The variable itself has a type either or that's an integer, a floating point, a character or what
46
00:03:04,260 --> 00:03:04,620
not.
47
00:03:04,860 --> 00:03:11,100
And in most cases, we will put inside of that variable some value that matches the variables type.
48
00:03:11,550 --> 00:03:13,770
So how will it look like in C?
49
00:03:14,010 --> 00:03:17,220
Previously would declare were created two variables.
50
00:03:17,250 --> 00:03:18,720
The first one was age.
51
00:03:18,840 --> 00:03:21,010
And the second one was temperature.
52
00:03:21,180 --> 00:03:24,600
And we can see that the variable ages of type end.
53
00:03:24,710 --> 00:03:24,970
Right?
54
00:03:25,020 --> 00:03:27,840
We can see it in the declaration face and age.
55
00:03:28,080 --> 00:03:32,020
That means we expect to assign it a value of an integer type.
56
00:03:32,040 --> 00:03:33,450
For example, 30.
57
00:03:33,750 --> 00:03:36,950
So that's why we use age equals to 30.
58
00:03:37,140 --> 00:03:41,160
And we can see the assignment simply taking the value of its right.
59
00:03:41,310 --> 00:03:45,750
Which is 30 and placing it into the variable called age on its left.
60
00:03:46,030 --> 00:03:47,760
A variable which we declared.
61
00:03:48,030 --> 00:03:49,140
Just one line above.
62
00:03:49,320 --> 00:03:50,520
So as simple as that.
63
00:03:50,640 --> 00:03:53,520
Just the sign of value to a given variable.
64
00:03:53,790 --> 00:03:57,550
And you can do pretty much the same for also the variable term.
65
00:03:57,600 --> 00:04:00,240
Which we declared using double temp.
66
00:04:00,450 --> 00:04:06,840
You can assign it a value of twenty six point five four temperature in Celsius or seventy six point
67
00:04:06,900 --> 00:04:12,220
point seven if you're using Fahrenheit, which is the same temperature, adjusting different units.
68
00:04:12,450 --> 00:04:15,870
And you can see that the values are floating point numbers.
69
00:04:15,900 --> 00:04:20,790
We're using these Daudt thing, these twenty six point five.
70
00:04:20,820 --> 00:04:21,600
DOT five.
71
00:04:22,050 --> 00:04:25,060
This indicates that this number is a floating point.
72
00:04:25,560 --> 00:04:32,130
And that's OK because we declared the temperature variable as type double to hold floating point numbers.
73
00:04:32,460 --> 00:04:33,360
All right.
74
00:04:33,720 --> 00:04:39,330
And by the way, in your next milestone, you're going to develop a program that will be capable of
75
00:04:39,330 --> 00:04:45,610
making these conversion between temperature given in Celsius to foreign aid degrees and vice versa.
76
00:04:46,110 --> 00:04:52,620
You're going basically to develop a weather station program, but we'll get to that later on in this
77
00:04:52,620 --> 00:04:53,220
chapter.
78
00:04:53,670 --> 00:04:59,670
So to summarize, for now, the first step will usually be to create word, declare a variable.
79
00:04:59,720 --> 00:05:06,390
Well, and the second step will be to put something in that variable or simply sink to assign some value
80
00:05:06,390 --> 00:05:07,920
to a given variable.
81
00:05:08,230 --> 00:05:11,730
And now we're going to talk about a very important part in this section.
82
00:05:12,000 --> 00:05:16,780
And it's called reading input from user or variable is input.
83
00:05:17,070 --> 00:05:18,750
And that's a really important part.
84
00:05:18,780 --> 00:05:25,410
Guys, it simply explains how your program can be reading data or information from the user really much
85
00:05:25,410 --> 00:05:28,230
as you use forums that you feel on a Web site.
86
00:05:28,530 --> 00:05:34,770
The program that runs in the back end reads, Every field of this form and store is 18 variables.
87
00:05:35,070 --> 00:05:39,680
And up to this point, we've learned how to create variables in how to use assignments to.
88
00:05:39,690 --> 00:05:41,550
They are most part of our program.
89
00:05:41,850 --> 00:05:47,910
And now what we would like to do is to get the value from the user instead of using the assignment operator.
90
00:05:48,240 --> 00:05:54,930
The assignment was done as part of our program, which means we simply used some hard coded assignment
91
00:05:55,290 --> 00:05:59,730
and we set a value to a variable without the ability to give the user.
92
00:06:00,080 --> 00:06:01,610
We are the developers, right.
93
00:06:01,650 --> 00:06:08,940
And we also have the users without providing the user with the ability to enter to insert its value
94
00:06:08,940 --> 00:06:10,140
to that variable.
95
00:06:10,320 --> 00:06:14,630
We simply declared it and then assigned it a given value.
96
00:06:14,640 --> 00:06:17,130
For example, a grade is equal to 30.
97
00:06:17,370 --> 00:06:19,050
And what is the problem with that?
98
00:06:19,090 --> 00:06:22,110
Basically, there is no problem with using any of them.
99
00:06:22,410 --> 00:06:26,550
There is only a difference between when one should be used over the other.
100
00:06:26,850 --> 00:06:32,340
So once again, there may be cases where we would like to use assignment operator and there may be other
101
00:06:32,340 --> 00:06:38,430
cases when you will need to use the input thing to get some information directly from the user.
102
00:06:38,670 --> 00:06:41,010
As we are going to see in the following examples.
103
00:06:41,250 --> 00:06:46,830
So suppose you have to write down a program that should calculate the average of two grades.
104
00:06:47,190 --> 00:06:52,860
One way to do so would be to refine these two variables by using the assignment operation to set their
105
00:06:52,860 --> 00:06:59,330
values to, let's say, grade one equals to 80 and grade two equals to a hundred.
106
00:06:59,610 --> 00:07:03,300
First of all, we know that an average of two grades can be calculated.
107
00:07:03,330 --> 00:07:06,990
But summing both of them and dividing the sum by two.
108
00:07:07,290 --> 00:07:14,720
So the average would be simply 80 plus seeing a hundred divided by two, which gives us a total of 90.
109
00:07:14,970 --> 00:07:18,830
So we can simply use a print command to print the screen.
110
00:07:18,880 --> 00:07:26,220
That average equals two percentage D, and instead of these presenters will just replace it with a total
111
00:07:26,220 --> 00:07:27,120
of 90.
112
00:07:27,120 --> 00:07:27,450
Right.
113
00:07:27,860 --> 00:07:31,040
They are the expression that on the right of the comma.
114
00:07:31,380 --> 00:07:32,190
That's great.
115
00:07:32,220 --> 00:07:33,030
It would work.
116
00:07:33,070 --> 00:07:36,300
But in this example, we define the two rates.
117
00:07:36,600 --> 00:07:38,100
We as the developers.
118
00:07:38,430 --> 00:07:44,190
We've simply created a program that will always print the average for these specific grades.
119
00:07:44,200 --> 00:07:48,210
One read one equals two 80 and grade two equals to a hundred.
120
00:07:48,390 --> 00:07:51,850
And I guess you noticed that this program is not generic.
121
00:07:52,170 --> 00:07:56,100
It will always print the average 90, always print the same.
122
00:07:56,370 --> 00:07:58,680
Unless we manually change the grades.
123
00:07:58,960 --> 00:08:05,070
And in such a case, we simply cannot sail or ship this program to customers because the program does
124
00:08:05,070 --> 00:08:07,620
not know anything except these two rates.
125
00:08:07,750 --> 00:08:09,030
Eighty and hundred.
126
00:08:09,480 --> 00:08:16,260
We want to give the user the ability to insert his own grades and then our program will calculate the
127
00:08:16,260 --> 00:08:19,140
average grade and give the result to the user.
128
00:08:19,440 --> 00:08:22,620
And that's exactly why we learn to work with the inputs.
129
00:08:22,950 --> 00:08:28,380
Now let's take a look at the syntax of how that should be done in our C programming language.
130
00:08:28,950 --> 00:08:34,290
So we we want to create a program that calculates the average grade of two given grades.
131
00:08:34,640 --> 00:08:37,560
We, first of all, need to declare these two variables.
132
00:08:37,800 --> 00:08:39,420
So we created a grade one.
133
00:08:39,540 --> 00:08:41,610
And also we create grade two.
134
00:08:41,880 --> 00:08:44,970
And at this point, these two variables are kind of empty.
135
00:08:45,030 --> 00:08:48,660
We say that they contain garbage value that can not be used.
136
00:08:48,930 --> 00:08:53,070
And we could assign some values to them like we've seen previously.
137
00:08:53,070 --> 00:08:56,850
Something like Ridwan equals two, 80 and grade two equals to 100.
138
00:08:57,180 --> 00:08:59,790
But that's not exactly what we want to do.
139
00:08:59,910 --> 00:09:05,310
What we want to do is to read the grades from the council, to read the grades from the user.
140
00:09:05,670 --> 00:09:08,640
We want to let the user to provide his own grades.
141
00:09:08,670 --> 00:09:13,780
So then any user will be able to use our program with his own grades.
142
00:09:14,040 --> 00:09:18,180
And to do so, we'll use one little function that will help us with that.
143
00:09:18,450 --> 00:09:19,710
And this function is called.
144
00:09:20,640 --> 00:09:27,990
So that syntax in C language for the input of the first grade looks like this scan F percentage, the
145
00:09:28,310 --> 00:09:30,000
on percent grade one.
146
00:09:31,200 --> 00:09:32,610
Don't panic just yet.
147
00:09:33,060 --> 00:09:36,360
Let's try to understand the structure of this comment together.
148
00:09:36,690 --> 00:09:40,740
These functions simply reads the value we inserting the council.
149
00:09:41,210 --> 00:09:43,980
And we first of this value as an integer.
150
00:09:44,010 --> 00:09:46,370
That's why we have this percentage D.
151
00:09:46,500 --> 00:09:46,850
Right.
152
00:09:47,130 --> 00:09:51,780
Just like we used percentage the in the print F comment to print something to the council.
153
00:09:52,170 --> 00:09:54,250
Now we are reading from the console.
154
00:09:54,300 --> 00:09:58,050
The input that the user will provide us with.
155
00:09:58,170 --> 00:09:59,610
And we treated.
156
00:09:59,750 --> 00:10:03,980
As an integer, thanks to these percentage deals, pretty simple, right?
157
00:10:04,190 --> 00:10:10,010
And the value itself will be stored inside of grade one variable, which we've just created.
158
00:10:10,250 --> 00:10:16,340
And these sampas and sign, all it does is to specify that the value read from the console using these
159
00:10:16,340 --> 00:10:21,830
percentages, they should go and be placed at the address where grade one resides.
160
00:10:22,340 --> 00:10:29,120
So once again, read an integer from the console and put it in the address where grade one resides,
161
00:10:29,390 --> 00:10:33,410
or simply put, this value in grade one variable.
162
00:10:33,560 --> 00:10:37,370
So pretty much the same way we'll do this for a grade two.
163
00:10:37,640 --> 00:10:42,320
So now at this point, we have two variables grade one and grade two.
164
00:10:42,590 --> 00:10:46,050
And they hold the grades that the user just provided.
165
00:10:46,070 --> 00:10:47,280
Using these console.
166
00:10:47,540 --> 00:10:50,630
And now let's print the average of them on the screen.
167
00:10:51,020 --> 00:10:54,500
There are actually a couple of ways that we can do it.
168
00:10:54,530 --> 00:10:59,270
One of them is just to write the mathematical operation in one print off-line.
169
00:10:59,600 --> 00:11:03,840
Something like that print F average equals two percent percentage day.
170
00:11:04,160 --> 00:11:07,800
And then to write the mathematical operation as we've seen previously.
171
00:11:08,030 --> 00:11:08,870
And there you go.
172
00:11:08,870 --> 00:11:11,750
Your average grade should be printed on the screen.
173
00:11:11,780 --> 00:11:13,690
Average equals to 19.
174
00:11:13,790 --> 00:11:14,660
In our case.
175
00:11:14,960 --> 00:11:16,090
Pretty cool, right?
176
00:11:16,240 --> 00:11:18,140
Threats around this code on your own.
177
00:11:18,170 --> 00:11:20,900
Open code blocks or visual studio or wherever.
178
00:11:21,110 --> 00:11:25,910
I'd use and try to run this code on your own and see how it works for you.
179
00:11:26,180 --> 00:11:31,760
One thing to mention here is that we didn't speak about anything related to casting, and that's actually
180
00:11:31,760 --> 00:11:35,320
the result of dividing the sum of your grades by two.
181
00:11:35,540 --> 00:11:38,210
This result may be actually a floating point.
182
00:11:38,210 --> 00:11:42,740
Number is something like ninety seven point and Dinara case.
183
00:11:42,740 --> 00:11:44,600
It won't be printed correctly.
184
00:11:44,960 --> 00:11:49,960
The only thing that will be printed on the screen will probably be just 97.
185
00:11:50,240 --> 00:11:51,830
It will be just rounded.
186
00:11:52,040 --> 00:11:55,630
So one of the ways to handle that is to use casting.
187
00:11:56,060 --> 00:11:59,300
But for now and for this video, I think that's enough.
188
00:11:59,690 --> 00:12:06,830
I hope you got the idea of how the functions SCAF works and that you are ready to move on for your next
189
00:12:06,830 --> 00:12:07,420
challenges.
190
00:12:07,790 --> 00:12:11,080
Where we will practice that even more.
191
00:12:11,300 --> 00:12:12,890
So I wish you good luck, guys.
192
00:12:12,950 --> 00:12:15,050
And I'll see you in the next video.
18517
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.