0:00
in this session we will see slightly
0:04
more sophisticated programs they call
0:07
that in our discussion numbered
0:08
flowcharts we talked about variables
0:11
which were conceptually seen as little
0:14
boxes in which you can hold values so
0:18
let us see how to write simple C
0:21
programs in which we make yourself
0:23
variables so we will illustrate with the
0:27
help of a sample program so we have this
0:31
following program which is very simple
0:33
32 Celsius temperature and convert it
0:35
into the equivalent fan 1950 this is
0:38
something that all of you must know so
0:41
the flowchart is very simple you have an
0:44
input C which is the current services
0:47
that you want to convert then you apply
0:50
the formula if which is 9c upon 5 plus
0:56
32 in this session we will see how to
0:59
write simple C programs which make use
1:01
of variables recall that in our
1:04
discussion about flowcharts we talked
1:06
about variables which were conceptually
1:08
seen as a little boxes in which you can
1:12
ok so let us illustrate a simple C
1:15
program making use of variables with the
1:18
help of a program so we have a small
1:22
problem which is convert celsius
1:24
temperature into the equal and family
1:26
temperature this is a formula that all
1:29
so let's try to see program for it so we
1:33
will draw the simple flow chart for
1:35
doing the program you input the
1:38
temperature in see in searches converted
1:42
into fat night according to the formula
1:44
9 x 5 x Celsius plus 32 once you're done
1:50
that available if holds the fanlight
1:54
value so you output the if so here is
1:58
the simple future that we want to
2:00
implement now how do we write the
2:03
equivalent to see called
2:07
so we're in the future we have seen that
2:10
we have two variables see and if ok
2:14
these are the variables that we would
2:16
want to implement in the c code so let's
2:20
see how to do it so we write the
2:23
following sea program in which now we
2:26
have two more components one is the
2:31
variable declaration float integrates
2:33
and then the second assist another
2:37
variable float family
2:39
ok so send the grades correspond to see
2:41
in the future and fam might correspond
2:47
ok so I i write the following code which
2:50
is supposed to implement the flowchart
2:52
on the left so let's say that the input
2:56
this 50 degrees celsius and then the fam
3:00
the formula is exactly the same as what
3:02
we have in the future we have nine times
3:05
integrate / fine plus 32 ok notice here
3:10
that these are headed medic operators so
3:13
the started Atlantic operator is stands
3:15
for multiplication /sand for division
3:20
and plus stands for edition
3:23
ok so this is exactly as in the
3:26
flowchart except that here month in the
3:28
flowchart multiplication symbol has been
3:32
but then see you have to specify it
3:34
using the star operator
3:36
ok so that night equal to nine times a
3:41
negative divided by 5 plus 32 is exactly
3:43
that similar to the analog asseline in
3:47
the future and finally for outputting we
3:50
will use the printf statement
3:52
ok so here is something new in the print
3:55
of statement we use what are known as
3:57
format specifiers these percentages f
4:00
simple certain you and will describe
4:04
ok so let us look at the program in a
4:11
so we have two statements which are of
4:15
interest in the beginning of the code
4:18
which are different but known as the
4:23
definition of the two variables recall
4:26
from our discussion on futures that
4:28
available said boxes and each box has a
4:33
name associated with it
4:34
ok so you have two concepts associated
4:37
with a variable as far as flow charts
4:39
were concerned one was the box and the
4:42
second was the name of the box now when
4:45
we come to see we will associate a third
4:47
concept with a variable which is the
4:50
ok so if you look at the first statement
4:53
it says floats integrates same equipment
4:55
now this defines availables integrates
4:58
it creates a box capable of storing a
5:02
real number and names the box and
5:05
centigrade so the box is of type float
5:08
that type float means that
5:12
yeah that box can hold a limit similarly
5:16
fascinate is also a box which can hold a
5:19
real number so you declared that the
5:21
type of that variable is flow
5:23
ok so these are supposed to be the first
5:26
two lines of the code now centigrade is
5:33
equal to 50 that is the line which
5:36
assigns the value 52 the variables
5:41
integrates so once you execute that code
5:44
the box can associated with the names
5:47
integrates will hold the value 50
5:49
followed by the line which computes the
5:55
value of the firmament
5:56
ok so fat not equal to nine times
6:00
integrates / phi plus 32
6:03
it's an it associates and arithmetic
6:06
expression so it evaluates an arithmetic
6:09
expression takes its value and still sit
6:13
associated with family ok and as we just
6:17
saw before start is the multiplication
6:22
forward slashes the division operator
6:26
and plus the addition operator now the
6:31
brackets in an arithmetic expression not
6:35
just like brackets in mathematics so the
6:37
group together particular things now
6:41
let's just praise to the program let's
6:45
step-by-step when we run the program ok
6:48
let's say that we save the file in
6:50
sample 2 dot C and then run it as dot
6:56
ok so first we will have two boxes
7:01
created one force integrate and 14
7:05
these can store float numbers and water
7:08
flowed numbers basically there real
7:11
numbers which is saved by the
7:12
microprocessor now the microprocessor
7:16
has been stored available real numbers
7:19
only using finite precision so this is
7:21
different from the actual real numbers
7:22
that we encounter in mathematics so we
7:25
have only a limited number of digits
7:27
after the decimal point but other than
7:30
that you can think of them as real
7:32
numbers will see floating point numbers
7:34
later in the course in greater detail
7:36
ok thank for now think of them is the
7:39
Machine representation of a real number
7:41
so once you finish the Declaration
7:44
statements what you have are two boxes
7:47
14 centigrade 14 family and because you
7:51
declared their types to be float it is
7:54
understood that those boxes will hold
7:56
three levels so let's execute the first
8:00
executable assignment here centigrade is
8:02
equal to 50 and the you will see that
8:06
the box contains 50.000 something okay
8:10
even though we specified it as an
8:12
integer it will convert it into a real
8:15
number of floating-point number and
8:16
stored then this is followed by the
8:19
calculation of the fanlight value and
8:23
let's say that you compute 9 x 50 / 50
8:27
plus 32 it comes out as 122
8:30
after that line is executed the box
8:33
associated with that night will contain
8:35
hundred and twenty-two ok then the next
8:40
line says print print of the temperature
8:43
okay there is no new line so the next
8:46
printf we'll start from there this land
8:50
ok and you hear you see something new
8:54
which is the person date f symbol
8:57
ok so these and water known as format
9:02
specifiers so the percentage f symbol
9:06
says that take the corresponding
9:08
variable which is given centigrade here
9:11
now and put a printed doesn't float
9:14
printed a serial number
9:16
ok so notice the difference between the
9:20
first printed in the second point of the
9:22
first printed just had a string between
9:25
double quotes the second point of has
9:27
two arguments one is a string between
9:29
double codes and the strings passenger
9:32
and then the second argument
9:34
disintegrates okay so it says take the
9:37
value of syndicate's and print it as a
9:41
floating point number so it does that
9:45
and you see 50.000 in the output there
9:53
is no new line so the next friend of
9:54
starts from the previous line where the
9:57
previous printed left of 50 Celsius
10:01
equals focus with Prince that and now
10:04
you have another format specified it's a
10:07
sprint of percentage of fat night
10:10
tonight is 122 and it will print it as a
10:13
floating point number or the serial
10:14
number so it will print it as one 22.00
10:17
so printer family so the final message
10:23
that will be printed will be the
10:25
temperature 50 centigrade of 50 Celsius
10:29
equals 122 family so the new thing that
10:34
we have seen in this program include
10:36
variable definitions how they are how
10:40
they have an associated type
10:43
and similarly how do we print these
10:46
variables so we don't want to put in the
10:48
names of the variables we want to print
10:50
the content of the variables we want to
10:52
print or distorting the box for that we
10:54
use this format specifiers like this
10:57
person native so let us briefly
11:00
introduce water types and see so
11:04
variables have the names of the boxes in
11:08
which to store the values but these
11:11
boxes especially certain boxes can hold
11:14
only certain kinds of values so all
11:17
boxes and noticing there are different
11:19
kinds of boxes now types are basically
11:23
saying that a particular box can hold a
11:27
particular kind of data so variables are
11:31
defined with the associated type and we
11:34
will use some basic types during the
11:36
course of this programming language
11:39
tutorial one of the two common types of
11:44
the common types that you see in this
11:46
program are int which stands for an
11:51
integer and float which stands for a
11:53
floating point number
11:55
ok so it stands for every real number
11:57
notice that machines can hold only a
12:00
fixed number of bits so into does not
12:03
mean that the integers can go from minus
12:06
infinity to infinity it goes from a
12:09
certain real lot of very small negative
12:13
number to a very large positive number
12:17
similarly floating-point also is limited
12:20
by a particular range this is because
12:23
machines cannot represent arbitrary
12:25
values the type of integer can store all
12:29
numbers from minus M plus 1 through 0 to
12:35
plus them so there will be some large m4
12:38
which which defines the upper limits and
12:42
the lower limits of the particular
12:43
mission now that limit made the
12:45
depending on which particular machine
12:49
on it on a 32-bit machine it will be 2
12:51
to the power of 32 m will be able to the
12:57
ok a few final words about variables
13:01
just like in a cooking recipe you will
13:06
never mention a step which involves an
13:10
ingredient without mentioning that that
13:14
ingredient is needed in the first place
13:16
ok so you will never say that use salt
13:20
and if you look at the list of
13:22
ingredients you were you will see that
13:25
there is no salt in the distributions
13:27
such recipes are considered bad so when
13:29
you try to typical recipe list out all
13:31
the ingredients first and then apply the
13:33
steps for the cooking
13:35
similarly the program you define
13:37
whatever variables that you need before
13:41
those variables are used by any
13:43
statement in the program always define
13:45
available before use now a word about
13:48
names of variables in the C programming
13:52
the names are consisting of numbers
13:55
letters and an underline simple and
13:58
underscore symbol ok and there is a
14:02
particular convention that you available
14:05
cannot start with a number
14:06
ok so the initial letter has to be a
14:11
letter or an underscore it cannot be a
14:13
number but for for the letters can be
14:16
either capital letters monitor or
14:20
numbers or an under school
14:23
ok so there that there are valid names
14:26
like cns court SQ c SQ 1c and this code
14:30
SQ underscored one so all these
14:32
one thing to notice that the names are
14:37
sensitive to upper and lowercase so for
14:40
example capital C integrate is different
14:44
from is integrated with starts with a
14:46
small singing so these are two distinct
14:48
variables good that's a common source of
14:51
errors when you start programming and
14:54
the thing to note it to watch out for is
14:56
that the assignment operation which is
14:58
equal to is not the same as the
15:01
so when mathematically you say equal to
15:04
B it means that a and B at the same
15:07
quantity so equal to B is the same as
15:09
saying the equal 3 this is not true and
15:12
see for example let's say that you have
15:16
the statements be equal to 3 semicolon
15:19
and then later you have equal to 2
15:21
semicolon and further you have the
15:24
statement equal to B semicolon so the
15:27
statement equal to B will set a two
15:30
beasts menu so this value is 3 and that
15:34
value will be copied to in so it will
15:36
set 823 and b will be unchanged
15:41
ok so watch out for this if if you were
15:46
expecting the mathematical operator
15:48
after the operation equal to B a and B
15:50
will have the same value but that is not
15:53
the case the the meaning of the same
15:56
symbol equal to is that take the value
15:59
on the right-hand side of the expression
16:01
and copy that into the box specified by
16:05
ok so copy the value in the right-hand
16:08
side to the variable on the left
No comments:
Post a Comment