C_07 Constants in C | Types of Constants | Programming in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c we have already discussed some basic about c introduction to c language structure of a c program and execution process of a c program the steps the translation linking and loading model in the previous videos right and before writing a program you should aware about some more concepts and these concepts we will use while you write a program like constants variables will use functions data types without these you cannot write program so first of all we will discuss these concepts then we will come to programs writing programs right so now in this video today i am going to talk about constants in c language all the type of constants will discuss with some examples and some rules to declare declare constants like um this value is a constant and this value is not a constant means valid and invalid constants you can say right and this video is brought to you by an academy because an academy is going to start a test series for all the gate 2021 aspirants named gate challenge and the series is having four levels first three levels would be consisting 30 questions and 90 minutes it's three levels are just it's like to warm you up for for the final round in the final level you are having 65 questions and you need to solve those questions in 180 minutes it's like the final grand finale test it's like you are you are sitting in your proper gate exam final exam right and these tests has been curated by the best faculties at uh an academy and these are going to cover the complete syllabus of csitga 2021 and the series is going to start from 19th of december as you can see here and you can also take the these tests live if you take the test live all india ranking would be provided to you and you will get to know your position means where you are standing in the competition how well you have prepared how well your preparation is for your gate exam and one more thing if you are not able to take the test lie it's okay you can also write the test after the live test is over right so if you are interested you can go for it all you need to do is you just have to download an academy app and you have to enroll for this test series so if you are interested you can go for it all the details and the involvement link as well as the invitation code i'll put in the description box of this video you can go and check out so now let us discuss constants in c constants are having fixed values the value of constants are not going to be changed throughout the programmer you can say during the execution of a program the values are to be fixed the programmer while writing a program will specify these values means compiler know the value of constant during compile time you will not give the value at runtime right one thing is what we use symbolic constants so that we use to define some fixed values like suppose um value of pi so you will write hash define pi is 3.14 this is what that definition section we have discussed when we were discussing structure of a c program so the value of pi is fixed 3.14 it is symbolic constant if you write hash define suppose max value is 50. so the value of that max is what is to be 50 throughout the program it is not going to be change you cannot change that value these are symbolic constants right so we are having basically two types of constants in c you can say numeric constants and character constants or somewhere it is also written four types of constant integer constant floating point constant and character constant and string constant let us discuss so these are four types of constant or you can say two types numeric and character constant is numeric is further divided into two types integer constant and real constant of floating constant here single character constant string constant so basically four types so let us discuss first one is integer constants these are having decimal values like if you write this uh 10 it is it is a constant five integer constant one integer constant integer constant right these are valid but if you write 0 5 this is not you can say decimal constant see in integer also we have 3 types decimal constant octal constant and hexadecimal constraint right decimal means we can use uh the the numbers from 0 to 9 base is 10 octal constant means from 0 to 7 base is 8 and hexadecimal means we can use from 0 to 15 and bases base of these constants are 16 so integer also having three categories so if you write 0 5 that comes under ah octal constant because the rule is what octal constants starts with the value 0 always right but decimal constant means yeah you can use 0 but if there are two digit in that constant so 0 can be at this side right side but 0 cannot be at this side left side if you write like this this comes under category octal constant right and constants are also called literals right so here also you can we can express an integer in three forms decimal octal and hexadecimal and by default if we say integer constant generally by default we represent that in decimal constant not octal not hexadecimal right difference i have told you what is octal now how you write hexadecimal constant c in hexadecimal we have sequence of digits preceded by either 0 x or you can say 0 small x let us suppose i write ah 0 x this is what hexadecimal constant right and see in hexadecimal we use what from 0 to 9 numbers and a to f characters i have told i have written here 0 to 15 but here we use from 0 to 9 numbers 10 numbers like right and after that 5 is what alphabets a to f right so here you can say a means 10 0 to 9 we use numbers 10 numbers then b like this we are having from 0 to 15 0 to 9 numbers and then 10 to 15 we represent like abcdef so if i write 0x that is also exactly decimal constant if i write like 0 x and 7 f this is also hexadecimal constant but if you write 0 x and suppose i am i am writing 7 g this is not hexadecimal constant because we cannot use g we can use only a to f right so you can say these integer constants are sequence of digits suppose i am writing here one two three four this is also what integer constant but rule or what you cannot write like this 56 comma 100 you cannot write any comma or special character between these constants so this is not valid this is invalid constant integer constant right or if and by default the sign is what plus you can also use sine plus or by default x plus if i write minus 1 to 3 this is also valid so you can write a sign either minus 1 plus if you write like this dollar one two three this is incorrect you cannot use any special character you can you you cannot use any comma you cannot use any space like fifty seven space hundred this is also invalid you cannot use space uh between these sequence of digits this there should not be any space any commas any special character this is valid one two three four or like 56 or five six seven eight this is also valid right so now i hope you got integer constant let us discuss real constants so real constants are also known as floating point constants these are having fractional part like if you uh write 12.56 this is decimal part this is you can say you can say this is what the integer part this is decimal point and this is fractional part so having decimal point these are what real constants so if i write plus this that is also fine if i write minus 50 6.02 that is also fine sine can be plus or minus by default sign is plus right but if you write this is invalid there cannot be two decimal points right and you can also represent these uh decimal points or these floating point constant in uh exponential form having mantissa then e then exponent mantissa can be anything like having that decimal point and exponent is always constant right so these are some examples see which one is valid constant or numeric constant or not this one is valid 0x this is also valid this this is not valid this one this is not valid because here we have space 0 1 2 this is valid 1 2 3 yes this is valid right and if suppose i write 0x af this is also valid and suppose if i write hash one two three this is invalid you cannot use any special character or known digit character right so these are numeric constant let us discuss now character constraints these are also two types single character and string string constant what is single character constants if i write like suppose single character is a so you will write like this in single quotation so this is what character constant so you can say character constant is what it is having a single character enclosed within single quote marks this is character constant this is also character constant and suppose i write this backslash this one zero this is also character constant these are backs backslash character constraints these are having different meanings there is list of backslash character constants right those are having different meanings fine and you can say like if you write this one comma this is also character constant right and if you write this at the rate this is also character constant so any single character which is enclosed within these single quotation mark within this you can also write down some characters also like a b c d capital a b c d or some numbers and some special characters also those are character constraints and these character constraints are within computer it these are being stored in the form of sky codes right like uh here this a means 97 capital a means what 65 like this small b means 98 and like this capital b means 66 and like this so on right and these are also having uh their different this this value this is also character constant if you write 1 and see 5 is not equal to this 5 this is character constant it is having some different value this is numeric constant this you need to take care right if you are writing anything between these single quotes the number then that is character constant that is not numeric constant this is not integer constant fine so sky value i am writing here you can see so these are sky values capital a to z 65 to 90 small a to z 97 to 122 zero to nine numbers are having a sky value 48 to 57 and some special character like this um coma or at the rate or some special characters are having values from 0 to 47 then 58 to 64 and 91 to 96 right you can also check out like which special character is having between these lie between these values and between these and between these you can google it out right but you have to take care of this thing and you can also perform some arithmetic operations on these character constants right suppose i am printing uh this see if you are writing like this percentage printf percentage d this is for integer format specifier and a it is what single quote mark so this is what character constant so it will print what integer value of this a because i am writing here format specifier percentage d this is for integer this is to print integer value right here if you compiler will find it is percentage d then it will print what whatever is given here after this coma the first one the first one for that it will print the decimal value means that that integer value the integer value of a is 97 so it will print 97 and if you write percentage c it is what character constant it is used to print characters and if you write here 97 then it will print the equivalent value character value of this 97 that is smally so you can check out these thing maybe you it may the output may be wrong so you can tell me in the comment box if it is room or right fine and see every character is having integer value that is why i am saying that you can also perform arithmetic operations on character constants right next comes to string constant so string constants are you can say sequence of characters enclosed within double could take code mark like i am writing here this is what sequence of characters and enclosed within double quote mark so this is string constant if you write abc this is what string constant and these characters may be letters numbers some special characters space everything if i write like uh here a b dollar this is also string constant but here c one thing if i write a in double quote this is not it is single character but enclosed in double quotation mark so it is not character single character constant it is a string constant and if i write a this is what single character constant but these are not equal this is different thing this is different thing this you need to take care this is string constant this is what character single character constant fine and if i write like 1 2 3 4 5 in double quote this is also string constant right but here this like 1 is not equal to this one because this is single character and it is equivalent to its sky value sky value of 1 is 49 but string constant this is not equal to its sky value this is string constant right so you need to take care of this thing also and whenever this string constant is there in a program and when compiler read this string constant then what compiler will do compiler will store the address of the first character reads the address of the first character and it will append a null character constant it will append a null character constant at last why so just to mark this is end of the screw end of this string right so length of the string is what one two three four five six here if you find out the length of this string then it is not five it should be six because a compiler has added null character just to mark that this is end of this string fine so now let us discuss how to declare constants in your program we use what keyword const keyword to declare constant like this right one method is this one so if you write in a program suppose i am writing a function word main i am not writing a complete program you can include those header files and i am writing here const int a value is 10 in this program right means this value of a is constant throughout the program you cannot change this value if suppose you will print this value a then always the output would be 10 and suppose after that i am writing a is equal to 50. you are trying to change the value of a then it this line will give error that this value is you can say its read only value you cannot change this value because you have written conest keyword before this declaration before this initialization of this variable this is variable name that also will discuss what are variables in c this is data type but if you write like this into a is equal to 10 fine when you will print value of a 10 would be printed but after that anywhere in a program you can change this value if you write a is equal to 50 and then if you print value of a then the updated value would be printed 50. now you can change this value this will not give error so you have to declare constant using const keyword right another thing is that i have discussed in starting of this video you can use hash define macro definition symbolic constant so there you can write this would be above this main although you can use anywhere the hash define statement but generally we use where above this main function right so we write like what hash define suppose i am writing a 10 this is also fine right but generally here we write what capital uh letters only like if you're writing pi then pi also if you write max then max also like this right and why i am using these capital letters so that you can distinguish between the normal variables within the programs and the symbolic constraints that is why there are some rules when you use this hash define statement then there are some rules to write this the statements and what are those rules there should not be any space between hash and define there should also be a space between hash define this and then name of that constant and then value of that constant another thing you cannot use here equal to right this would be wrong fine you cannot use semicolon after the statement this would be wrong so these rules you have to take care so this is you can say types of constant and how you declare constant in your c program so you can also write down this thing on your ide or you can run this program and you can check this is giving an error or not and when you remove this you can also check the value is going to be updated or not you and you can tell me in the comment box right so i think that's it about constant in next video i'm going to discuss that variables in c so now that's it for this video and i'll see in the next video till then bye take care you
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 71,704
Rating: 4.9288259 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, programming in C, constants in C, introduction to programming in C, C tutorials, jennys C lectures, ugc net computer science preparation strategy, gate cs lectures
Id: 6gVT64lk764
Channel Id: undefined
Length: 20min 10sec (1210 seconds)
Published: Sat Dec 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.