declare

Constants in C (Part 1)



Sharing buttons:

now we are going to have a discussion on

constants in C what is a constant as the

name suggests some think that never

change of course once defined cannot be

modified later in the code yes

now how do we define constants we can

define constants in our code using hash

define or using Const keyword let's see

how to use hash define syntax for has

defined would be hash define then name

and then value sometimes name is also

called as macro okay job of preprocessor

not compiler is to replace name with

value now this is the job of

preprocessor like preprocessor does the

job of replacing the header files with

their actual content you know this thing

that hash include stdio.h is going to

get replaced by preprocessor this name

over here is also going to get replaced

by this value available over here and

this is the job of preprocessor let's

see one example here suppose I define a

pie with 3.14159 now this is the name

and this is the value hash define name

name is pie and value is 3.14159 right I

am simply going to print this pie

instead of using this 3.14159 constant

over here I'm simply using pi and print

it up to five decimal points let's see

what is going to be the output here you

can see this is going to be our output

3.14159 right we would be able to print

this pi using just this P I name over

here right with the help of this name we

would be able to print this value

3.14159 and this is the advantage of

using hash define suppose in our code we

have to write 3.14159 many number of

times then writing this constant is

quite cumbersome

therefore instead of this constant we

can write the spy which is quite

readable as well as easy to write

therefore we are using hash define now

here are some takeaways to take along

with you please don't add semicolon at

the end if you notice it carefully that

we are not adding any semicolon at the

end so we should not add semicolon at

the end here you can see in this example

I'm adding a semicolon at the end now

this is one thing that you need to avoid

at any cost this will produce an error

so please avoid adding semicolon at the

end now this is the common practice and

this is the common mistake we do almost

all the time because we usually end our

statements with semicolon like this

printf function we are ending with

semicolon after this return 0 we are

ending with semicolon but here we are

not going to add semicolon at the end we

need to avoid this choosing capital

letters for name is a good practice for

example here you can see I am NOT using

capital letters for me now what is the

disadvantage you may get it is possible

that I declare a variable with the same

name right and I assigned it a value 74

and I'm just simply trying to print this

value now we got an error out of this

because our macro is also with same name

right now preprocessor does its job and

replace this value with 89 wherever it

finds it out here also it replace this

with 89 and replace this also with 89

now this is the disadvantage because as

you can see this is equivalent to this

statement right when the preprocessor

replaced this value with 89 and then

this is what we got 89 is equals to 74

now this is something we cannot do right

we cannot assign a constant to a

constant and we got an error now it is

better to write capital letters for the

hash define macros this will avoid

confusions later on suppose you have a

big file with lots of lines of codes and

some where you declare a variable with

the same name as macro then in that case

you got an error and finding out the

error is very difficult so we need to

avoid such practice

better is to write capital letters for

the names of the macros you're defining

over here now whatever inside double

quotes won't get replaced if you think

in this example this value is also going

to get replaced with 89 then you are

wrong because this is inside double

quotes and it is treated as string

preprocessor won't replace this

particular value with this 89 it is only

going to replace this value with 89 now

the output for this is value is 89 not

89 is 89 whatever is there in double

quotes is not going to replaced by the

preprocessor remember this thing we can

use macros like functions for example

here you see I use this add X comma Y

just like a function you know and after

that I perform some operation which is X

plus y what I want to do is I want to

perform the addition of two numbers for

entry I simply call this macro and this

is going to get replaced with this X

plus y which is nothing but equals to 4

plus 3 this gives us the output as 7

dashing of two numbers is 7 we can write

multiple lines also using slash now here

we can see that I am writing multiple

lines over here using slash here what I

want to do is I want to know this thing

that whether X is greater than Y or Y is

greater than X if X is greater than Y

then I'm going to print X is greater

than Y if it is not then X is lesser

than Y in this case I am providing 5 and

6 as the values of x and y this is the

if/else construct we are going to talk

about if-else in later sections here you

can see inside if if the condition is

evaluated to true then this statement is

evaluated else

this statement is evaluated here you can

see I provided 5 and 6 as the values 2 x

and y 5 is not greater than 6 therefore

this condition is false

this means else condition is evaluated

which is 5 is lesser than 6 and this is

going to be our output here 5 is

than six right first expansion then

evaluation let's see what does it mean

here you can see I have written a result

of expression a star B plus C is person

times D this person that D is going to

get replaced with the second argument of

the printf we know this thing already

here I'm performing the operation five

multiplied with this macro over here

here you can see this macro is going to

get replaced with four plus three which

is equals to seven and we simply

multiplied with five which results in 35

right wrong this is not done five is

multiplied with macro first the

replacement is going to get done first

expansion then evaluation here the

expansion simply means that this macro

is expanded to X plus y and it is

getting replaced over here which is

nothing but 5 into 4 plus 3 and then the

whole expression is evaluated which is 5

into 4 plus 3 result of 5 into 4 plus 3

according to the board mass rule we know

this thing that 5 into 4 is evaluated

first which gives us 20 and then we add

3 to it which gives us 23 now the answer

is not 35 answer is 23

some predefined macros like date/time

can print current date and time if we

use this macros they are going to print

the current date and current time please

note down this thing that these are

double underscores not single same for

the time as well now these are the

predefined macros or standard macros

which prints the current date and

current time okay friends this is it for

now see you in the next lecture bye bye

[Applause]

[Music]