0:00 welcome to this complete C language course with handwritten notes C is known as the mother of all programming languages in fact a lot of compilers and jvms are written in C whenever it comes to programming C language is considered as the best to learn for beginners my name is Harry and I will be your instructor throughout this course we'll start from the basics of C in fact from the basics of programming and then we'll raise the bar like anything we learn about pointers we learn about functions we learn about Dynamics memory allocation which is one of the most confused topics when it comes to C the only prerequisite of this course is your time I just want you to come here devote some of your time to this course and definitely you will learn and master see like anything if you want to support this initiative make sure you hit the like button and also give me a comment saying this C language course is helpful that would mean a lot to me I have divided this course into different chapters each chapter has its own practice set which makes makes it easy 1:00 for you to practice about the topic discussed in that chapter to make learning fun and simple I have also included some projects for you and one of the projects we are going to do in this course is a very interesting game and you'll definitely enjoy it when I started learning C I was really very confused with the syntax in fact the concept of pointer was something I was not able to wrap around my head but there were some of the tips and tricks that I used and some of the very amazing analogies which helped me learn C in a very very beautiful way this chapter consists of handwritten notes which are made by me I have used different colors in the notes I have written each and every chapter in The Practice set of the note to make things easier for you not only that I have also included a cheat sheet with this course so that after you are done with the course you have the boilerplate syntax and all the syntax that you want to use in your C programs is single click away I'll give you a link to the online and the PDF cheat sheet along with the cheat sheet and notes I have also timestamped this video 2:01 so that you can easily navigate to the different chapters and you can easily learn C programming language the projects that we are going to do in this course can be added to your GitHub profile to enhance your portfolio do not forget to add them to your GitHub profile so enough of talks now let's get started so I am inside my computer screen let me be very honest with you guys this course is very very beautifully structured I have created notes for all the chapters and we have around 11 chapters in two projects and this is going to be fun so let's open chapter number zero where I'm going to give you introduction on what exactly C programming is and what exactly programming in general is because if this is your first language and you have not seen programming or maybe you haven't written any program in the past this course is going to be the starting point so what is programming computer programming is a medium for us to communicate with computers so just like me and you are communicating we are communicating with the help of a language which is English I'm using 3:02 English language to express my feelings to you I'm using some English words which you can understand and take actions on for example if I say please come here you can come here if I say please go to the market you will go to the market and all those things so we have a computer and computer can be a desktop computer a laptop computer and I even call this phone as a computer this is also a mini computer so we can give instructions to the computer it can be of any form it can be a chip of this size or a phone of this size or a laptop of this size IR respective of what your computer is you need a medium to talk to the hardware and that Medium is programming so programming can be seen as a medium for us to communicate with computers just like we use Hindi or English to communicate with each other programming is a way for us to deliver instructions to the computer you might use Spanish you might use Dutch you might use some other language irrespective of what language you are using you are simply using it as a 4:03 medium to communicate with the other person just like that we use C programming language to communicate with our computers so what is c c is a programming language it is one of the oldest programming languages it is called mother of all programming languages for a reason a lot of jvms and compilers are written in C operating systems are written in C A lot of lowlevel stuff is written in C so C is one of the most important programming languages and this is said to be a programming language which all the computer Enthusiast and computer software Engineers must learn so C is one of the oldest and finest programming languages he was developed by Dennis Richie at AT&T's Bell Laboratories in 1972 you don't have to remember this I remember this just because I've seen or read this statement a lot of times but you don't have to remember this but remember the name of Dennis Richie he indeed brought a revolution to the world of programming now let us talk about about the uses of C what exactly are the uses of C where would you want to use C 5:03 programming language so C language is used to program a wide variety of systems some of the use cases are major operating systems Linux and there are a lot of other Linux distributions which are written in C so we use C to write an operating system which controls all the hardware of our machine so C is a very important language now let's talk about driver program C is also used to write driver program s let us say I have a device it's a camera it can be a printer it can be uh it can be a chip I can write device driver software using C programming language now C programming language is also used to program embedded systems where programs need to run faster and limited memory see there are a lot of programming languages in the market we have python we have Java we have JavaScript all these programming languages are good for one task or the other C is good and C is is famous to run faster in limited memory and wherever we have limited memory wherever 6:04 we have embedded systems a microwave a very small chip something that needs to run in milliseconds is written in C now C is something that is very very close to the hardware and it is one of the most loved Language by those people who want to have a lot of controls in hand now C is also used to develop games an area where latency is very very important if you have played pubg or GTA y City GTA San Andreas GTA 5 cyber Punk we all play games right we all play one or the other game in our phones sometimes the latency is really very important especially in the case when we have limited Hardware in memory when we have limited Hardware limited amount of memory latency is really very important can you imagine you are pushing a button and that button is kicking somebody in your game and that kicks actually takes place seconds after you press the button you'll not play that game you'll not enjoy that game so latency is really very important in such games and C language is used to develop such games 7:05 it really reduces the latency and C is an amazing language so we will start writing our first program in the next chapter we'll see the building blocks of a c program we'll see how C program is structured and how it looks and why we need to follow some of the best practices we'll see all those things in the next chapter let's move on to chapter one now let's start start with the first chapter and let's open this PDF I have written this PDF with a lot of hard work so please listen to whatever I'm telling you in this video very very carefully and definitely you will become a champion of C within no time uh let's now install a software called vs code you must be wondering what vs code is if you are a beginner but just follow along and see how I'm downloading and installing Visual Studio code you just have to download it I'm downloading it for Windows because I'm using Windows if you're using Mac Linux or some other operating system it should be available for your operating system it's very famous and it is really very good and fast so I'll simply install 8:06 this vs code by clicking on this it is going to take 10 more seconds in my machine Visual Studio code has been installed so I'll run it I'll simply click next but I'll not be able to click next until I read this license agreement I've read the license agreement I'll click on accept the license agreement I'll go to next and make sure that these check boxes are checked this is very important and I'll tell you why later but make sure that all all these checkboxes are checked you might want to create a desktop icon if you want to create you can check it otherwise not but these other checkboxes must be checked I'll tell you why after some time let's click on next let's click on install and this is going to install Visual Studio code on my machine so I'll wait for it to install and in the meantime what I'll do is I'll say download MW now what is this MW it's a compiler that we are going to use and this is something that will help us Runner C programs I'll tell you how and why later but let's download it and let's set up MW in our machine so all 9:06 you have to do is simply click on this download button and your download will start shortly I'm waiting for it to download this is simply gnu for Windows it's GCC which is used to compile C programs you can see your Chrome downloads by clicking here and clicking on downloads and after you do that you will see that MW get setup.exe this has been downloaded so I'll run it I'll simp simply click on install uh this is the directory where I want my MW to get installed if you don't want to get it installed here you can install it in program files or something but I'll not touch it I'll simply go to continue and this is downloading the installation manager this takes some time let me be very honest with you I'm using quite fast internet connection still it takes some time so give it its time you can go grab a cup of coffee or maybe you can go for lunch or dinner and come back and this will be downloaded but yeah it is downloading quite fast f it depends upon the source for servers to be very honest sometimes it's fast sometimes it's slow but again irrespective of whatever the 10:07 case is you can grab a cup of coffee so I'll give it its time and in the meantime I'll look into Visual Studio code but I'll not launch Visual Studio code from here and I'll tell you why I'll simply uncheck this check box and I'll click on finish and what I'll do is I'll come back to this folder where I've created all the notes for you I'll come back to this chapter one folder and what I'll do is I'll right click and and I'll see this option open with code in a very very rare case if you do not see this options after following the steps that I mentioned you can restart your computer but in most of the cases you should and you will definitely see this option open with code simply click on open with code and once you do that this folder will be opened in Visual Studio code now what does this mean when I say that this folder will be opened in Visual Studio code this simply means that I have this Visual Studio code source code editor and all the files that I'll create here will reflect here and vice versa so if I create a new folder here I'll see it here and if I create a new folder by 11:07 clicking here let me call it new folder 2 I'll see it here as well so I can look into all the files delete read write all the files from here itself so this is one of the biggest advantages of Visual Studio code I simply selected it and pressed delete on my keyboard and this actually deleted all those folders that I didn't need it now what I do is I'll simply click on this plus icon and I'll say 01 uncore basic. C and this will actually create a file now what is this do c and what does this program name mean let me tell you so when I say 01 basic. C it actually creates a C file a c source code file just like we save our PDFs with an extension of PDF just like we save our text files with an extension of txt just like we save our movie files with an extension of MP4 we save our C source code files using C now what I'll do is I'll come back to visual studio 12:08 code and this will look very very haunting to you if you are opening it for the first time you might get confused what these buttons are about you might get confused what the setting is about and let me tell you how this Visual Studio code has been customized for my case now you will see some other color in your Visual Studio code and that might bother some of the students so let me tell you that you can actually install a jellyfish theme extension simply type jellyfish themes after clicking on this extensions button so I'll say jellyfish theme or other themes if I'm not wrong uh I think it's theme jellyfish theme and I already have jellyfish theme installed um maybe I'm not spelling it correctly let me find it out jellyfish jellyfish theme yeah so it's jellyfish theme it's said so simply search for this jellyfish theme install and you should also see this color theme another thing that I'm using is cc++ intellisense so you can click on install you will see this install button 13:10 for my case it has already been installed so I'm not going to reinstall it you can click on this install button and this will be installed so uh for the time being we'll use these two extensions I have some other extensions too which I have uh enabled for other projects but I'll disable them immediately so let me disable all these uh Community material theme so I usually work with a lot of extensions with a lot of other languages so what I'll do is I'll simply uh disable all these extensions so I have disabled GitHub markdown so let me disable all these things which we do not need here and which might confuse you you don't have to install and disable all these extensions let me tell you I'm disabling it because I don't want them to disturb me while I'm coding in C with you so let me reload my vs code I'll simply click on reload U and that should do now what I'll do is I'll simply create a boiler plate C code and for that what I'll do is I'll open uh my C cheat sheet and 14:11 I'll give you the link of my C cheat sheet from the description uh so let me open the C language cheat sheet I'll copy this code I'll paste this code I'll save and what I'll do is I'll simply say print F and I'll say hello word now you must be thinking what have I written in here I haven't explained anything here and I have written all these things without even explaining this is unfair but again you just have to type this without understanding yes without understanding what it means because we are going to look and desect into all these things in the rest of this video so do not worry and type this as is you can copy the boiler plate from here I'll give you the link of the street sheet as well and also what I'll do is I'll see if this has been completed so I'll click on on continue and all you need to do is you need to click on this minw 32 GCC g++ and I'll mark this for installation just click and click on Mark for installation and this is a C++ compiler and C++ compiler 15:14 as well as a c compiler because we have GCC g++ so just install it I'll go for installation and I'll say apply changes and I'll say apply and this will actually download this and install it it sometimes gets very very annoying when the server is slow and it takes a lot of time so let me warn you beforehand do not cancel it it sometimes takes a very long amount of time and yes it is annoying but again it's worth it just go for a cup of coffee or go for lunch or dinner and you'll definitely love it when you come back and see everything you know getting installed and uh set up for you so I'll also do the same let me go and grab a glass of water and I'll come back after I have all this installed and set up on my machine so finally this has been completed and let me tell you guys this took a lot of time lot of time and my computer is fast and internet is fast so let me close this dialogue box and we have installed MW GCC g++ now see how we can use it so what I'll do is I'll go to my local DC 16:16 and I'll go to the folder where this m GW has been installed I'll go to bin and I'll simply copy this path now what I'll do is I'll type en EnV and I'll be able to edit my system environment variables and and I'll click on environment variables then I'll click on path then I'll edit the path I'll add A New Path I'll paste this path and I'll click okay and then I'll simply click okay once again and once again and that will actually add GCC uh gc. exe this thing to path so basically what I want to do is I want to use this GCC exe to compile my C programs and because of that what I've done is I've added this folder this particular folder to path what happens when you add add a location to path basically you will be able to use all the txe executables without having to navigate to this path so let me show you I'll close this and what I'll do is I'll open my windows poell and I'll simply say GCC now the moment I enter you can 17:16 see that no input files message has been provided to me it is basically telling me that you have not given me any source file to compile so let's come to visual studio code and see how we can compile our source files so so I'll close this terminal and you can open the terminal by clicking here terminal new terminal this actually opens a new terminal and you can say GCC 01 basic. C so once you type this you are basically saying compile this program which is written inside this source file 01 basic. C so you want to compile this file to a file which can be executed by this machine this machine is a Windows machine so it will compile it to aexe file let's see the moment I press enter you'll see an a.exe file created here now I can run this a.exe file I'll simply say a and press tab this actually autocompletes the moment you say a so anything that starts with a 18:18 will get autocom completed so I'll simply say a and then I'll press tab or I can even type this particular thing dot back/ a.exe the moment I press enter I'm seeing this hello word so this program basically prints hello word I can even say hello Harry let me show you and back sln is nothing but a new line character so it actually prints a new line character because of which we are seeing this PSD this thing here instead of here so if I if I skip this back sln what what will happen is let me compile it once again and then once again I'll say/ a.exe the moment I say hello Harry this will actually not print a new line so let me show you a practical example of what this back slash n is doing so the moment I run this program once again you'll see that hello Harry a new line and then hello Harry but if I skip this back sln and once again I I'll compile this and I'll run this you'll see that there is no new line after Harry so what 19:19 I'm doing is I'm using my arrow keys to compile my program once again and execute my program once again so you'll have to compile every time you make a change to your program this is something that you need to remember I hope that I'm clear I hope that all these things are clear in fact this is not clear so far but I'll make it clear to you very very very soon so let's come back to our PDF cheat sheet uh or rather PDF notes not cheat sheet let me close or I'll simply keep this cheat sheet open as well I'll close other tabs and let's talk about variables constants and keywords so for now just forget about how all this works and let's focus on the concept of variables constants and keywords what are these three things and why do we need these three things so let's talk about a variable it's a container which stores a value so if you have a kitchen and you cook some food in your kitchen there is a high chance that you'll have some containers which will contain some rice maybe some Dal maybe 20:21 some um what do you call it um some tea leaves maybe you'll have some vinegar in some container you'll have some water in some container you'll definitely have some soya sauce or tomato sauce all those things in a container in a very similar way just like a container is used to store a particular thing we use small container for Dal and for rice and we use a big container to store say floor or we use bottles to store vinegar and also we use uh one or two liter bottles to store water okay so different containers for different things in a very similar way C programming language has a concept of container and container is nothing but a variable it is called variable in C programming language in fact in any programming language it is called a variable and it simply stores a value so in kitchen we have container storing rice T sugar Etc similar to that variables and C stores value of a constant for example a is equal to 3 now 21:23 a contains a constant which is three B is equal to 4.7 B is aign 4.7 7 C is equal to a c is assigned a character which is a we'll talk about what characters are what floating Point numbers are and what integers are but for now we need to get a hang of how these things are working so let's talk about few rules for naming variables in C can I name my variable as 001 harry2 no I cannot do that and we'll see why we cannot do that so first character must be an alphabet or an underscore so you can use an alphabet or an underscore so let's try so I'll say intore a and semicolon and I'll say seven now just forget about what this int is what this equal to is how these things are working just focus on the concept of variable see I'm not getting any error I'm not seeing any error from my vs code when I type underscore a but the moment I say zore a and I save it you'll see that a 22:24 red line is actually beneath this zero which which is basically telling me that hey you cannot name this variable starting with zero so I'm actually violating this particular rule because of which I'm seeing that error now commas and blanks are not allowed in variable names so even if I have it as underscore a and let me save it so even if I have an underscore a and I give a space and say Harry no this is not allowed the moment I save it this will give me an error but if I join it with an underscore and save it this is actually allowed because spaces are not allowed commas are also not allowed so if I put a comma and save it this is actually not allowed as well and this will also create problems a variable with this name will not be created okay so this is something that you need to know and let's move on to the next rule for naming variables in C no special symbol other than underscore is allowed so let me show this to you let me put a 23:26 dollar here and save it no this is is not allowed the moment you try to compile this file you'll not get the desired output now no special symbol other than an underscore is allowed also variable names are case sensitive which means that if I have a variable called Harry and I have another variable which is capital H Harry these both are different variables and will uh store all these numbers differently in different separate containers these are not exactly the same thing so this is allowed so you can say in small Harry and in capital L and this will not create any problems in your program now we must create meaningful variable names in our programs this enhances readability of our programs and actually makes it easy for other programmers to read our code so this is something that you need to follow always use meaningful variable names if you want to create a variable for storing say value of a URL simply call it URL or value of URL do not call it ABCD or something else or your neighbors or your crush's name not 24:28 do that just type the thing that it is for example if I want to create a variable for storing the amount of coffee that I'm taking per day I should actually name it amount of coffee cups of coffee or something like that I should not call it Puja Reena Tina or Monica for that matter so let's talk about constants for now an entity whose value doesn't change is called as a constant what is a constant it doesn't change three it doesn't change four is four it will never change four will never ever become five or 10 for that matter so four is a constant three is a constant something which is not going to change is a constant but if I say a is equal to 3 I can always say a is equal to 4 and I can actually put four inside that container which was previously holding three so I can change this container to hold four now so this is a variable we can change its value its value is three at this point but later it can become four also it can become 14 25:31 some time in the future so you never know but a constant is not going to change four is not going to change four is going to be four in this lifetime and even when we have kids of our kids they will also call it four and it's going to carry the same value which is four so four fingers are going to be this now 40 years ago and even 2,000 years later it is going to be four so it's not going to change change ever so this entity is called constant variable is an entity whose value can be changed now doesn't mean that it will always change but it can be changed so a equal to 3 can be changed B is equal to 4.7 can always hold 5.8 in the future so it can change this is something that we need to hold in our mind before we start with this chapter before we start writing programs and see I want you to ensure that you have this code Runner extension if you don't have this extension simply type code Runner and simply install it you'll see an install button here just like I'm 26:32 seeing for Chinese and for editor config you simply have to click on that install button and your code Runner will be installed this will give you this run button here so whenever you are in a c program you'll see this run button so make sure that you get this run Button as soon as possible it will help you execute and run your C programs comfortably now we'll move ahead and talk about the types of constant we have various types of constants primarily there are three types of constants we have an integer constant we have a real constant also we have a character constant so we are going to see how we can write these three types of constants in C also we are going to see few examples of us assigning these constants to a variable so what I'll do is I'll open vs code you can see that I already have this program written I'll do one simple thing I'll say 02 underscore I'll simply say constants do c and the moment I say constant. C and um let me also copy the boilerplate code I'll Simply 27:34 Save this uh what I can do is I can say int Harry is equal to 7 also I can say car Harry 2 is equal to C notice that I am using single codes for a character you have to make sure that whenever you are assigning some variable to a character you need to use single codes now this might sound very very confusing to some beginners but let me tell you something as we move ahead in this course you are going to find all these things very simple and easy so please bear with me for sometimes and definitely you are going to grasp all these uh Concepts very very easily similarly we use a float keyword now these keywords might sound very very strange to you if you are a programming beginner or C beginner for that matter but let me simply show you how we can assign some uh values different types of constants to a variable so I wrote 5.23 I could have written 5.23 I just removed 28:36 minus sign uh and similarly I can use minus 7 when I'm assigning an integer to to this Harry variable now let's come back to the notes and let's build some understanding around this and then what we'll do is we'll come back to vs code and write some sensible programs so primarily there are three types of constants we already saw that integer constant real constant and character constant so this is very very important these three types of constants are something that you really need to remember and let's now talk about keywords What are keywords so whenever we are writing a program there is some part of the program that we have written it's coming from our mind it's coming from our head and there is some part of the program which is not coming from our head it is something that has already been reserved by the C programming language Community somebody who built this programming language reserved these keywords or rather declared these keywords as reserved so these are 29:38 reserved words whose meaning is already known to the compiler so meaning of all these keywords is already known to the compiler now there are 32 keywords available in C uh let's see what those 32 keywords are but before we see these 32 keywords let me tell you you don't have to remember these keywords a lot of people try to remember these keywords and and that doesn't sound good you don't have to do this please do not try to remember these keywords this is something that we should avoid you will eventually start remembering all these keywords when once you uh start writing some meaningful programs and once you start building few projects in C language these keywords or other some of these keywords will definitely sit in your head now Auto double instruct break long else I have joted all these uh keywords down just for your understanding because I wanted you guys to have a comprehensive list of all these keywords at your disposal so let's write a first C program uh we have already written a first C program but I 30:39 want you to write this program as your first C program type each and everything do not use any shortcuts or copy paste simply type this out Simply type this hash include stdio.h now uh simply say in Main and simply type it out do not think about what hash include is what stdi .h is do not think about what int main is what uh print f is what return zero is just type it out and execute in in your um vs code or if you're using some other IDE or source code editor uh which I do not recommend if you are a beginner please follow along if you are a beginner but if you are an advanced user just trying to hone your C skills you can simply come to this tutorial and use the IDE or source code editor of your choice so so once you start typing this program out uh let me write the first program as well so I'll say 03 uncore first. C uh so I've typed this program out and also 31:40 what I'll do is I'll simply copy this uh I I told you that you have to type but I'm copying I'm copying because I don't want to waste your time to be honest but I want you guys to type it out because when you type it out when you type something out you actually remember things uh but if you copy paste and you know start running or simply read from the nodes you might not remember it that well so let me see if it is the exact same program which I've written uh hello I'm learning C with Harry let me write it down hello I am learning see with Harry and um back sln is something that you might want to write but uh let's write return zero and let's run this program also I'll save this program so what I'll do is simply run this program and once I run this program you can see Hello I learning see with Harry has been printed on the screen now let's talk about what this structure means what is this basic structure of a c 32:42 program how all this works together this is really very important so let's talk about this let's talk about the basic structure of a c program so all C programs have to follow a basic structure a c program starts with a main function you can see that we we have a main function and execute instructions present inside it so main is a function now you might think what what is this terminology about what is this function I've never heard of a function but bear with me function is something uh you can you can look at function as an entity enclosed in this curly braces name of this function is main int is something that it returns but we'll come back to functions in the later chapter let's focus on uh this particular function and let's let's think of function as uh let us say a box containing instructions let let's C function in a very very U layman terms of uh box containing structure instructions so printf hello I am learning C with Harry 33:42 is an instruction return zero is an instruction but for now what we will do is we'll ignore this return zero instruction definitely it's doing something but let's ignore this part of the program in mean and return zero let's look at this piece of the program as something that we will have to write to execute the program and believe me we'll come back to this and we are going to understand all these things in a real great detail also this is a header file and we are going to talk about it as well so let's ignore line number three and four and line number six and 7even and whatever is between this and this which is a single line will be executed by the program let's look at this and let's also remember that we cannot change the name of this function it has to be main because main is the function which gets executed whenever your program runs okay so I hope I explain this program to you pretty well let's move ahead and let's see each instruction is terminated with the 34:42 semicolon there are some rules which are applicable to all C programs so we going to see all these rules now so the rule number one is every program's execution starts from the main function so whenever we are executing a program the execution starts from the main function all these statements are terminated with the semicolon so this is instruction it is terminated with the semicolon this is an instruction it is terminated with the semicolon this is actually part of this function so this is not terminated with a semicolon also this is a part of the function this is function definition so this is not terminated by a semicolon we are going to come back to functions and function definitions uh so please bear with me until that time Now instructions are case sensitive if you type something in capital P if I say Capital BR this is is going to show me some issues because capital printf is not a function to print it's small printf so it is case sensitive do not change the case of U the function names or the keywords or um 35:43 the function names for that matter uh I hope you are enjoying and understanding what I'm trying to tell you instructions are executed in the same order in which they are written so if I have written hello I'm learning C with Harry first and let us say I write another hello after that this is going to print first and this is going to follow the first print statement let me show you so what I'll do is I'll save this program and run this you can see hello I'm running C with Harry and hello has been printed but if I shift this line above this line then hello will be printed first and then hello I am learning C with Harry will be printed let me show you so I accidentally selected it and run it you don't have to select it and run it so I'll click somewhere and once again I'll try to run it and you can see hello hello I'm learning C with Harry has been printed on the screen so never select a part of program and run it if you don't know what you are doing because whenever you select a part of program and try to execute it what happens is it is copied to a file called temp Cod Runner file. C 36:44 and you are going to be in trouble okay so always make sure that whenever you are running your programs you're not selecting a part of it okay so coming back to our notes uh we have understood what the basics structure of C program looks like you might also have some ambiguity in your mind you must be thinking what is this mean what is this end what is this curly bracket but I want you to please sit back relax and just listen to me for some time and as we move ahead in this course you are going to clear a lot of your Concepts I promise you so let's Now understand the concept of comments what exactly is comment so comments are something that is used to clarify something about the program in plain and simple language let us say me and you are working on a c program what if I want to tell you that please do not uh touch this function I'm going to write it if I write something like this if if I say that um I want to write code to read the user uh 37:47 mic if I if I write it in plain text what is going to happen if I run this program I'm going to see some errors you can see that c language compiler is telling me that this is is not a valid C code please do not write it there another option uh for me can be creating another text file with the same name as that of program and putting all these comments with line numbers but that will be really very tedious so C provides the mechanism of comments you can add comments to your C program all you have to do is simply use this double forward slashes this is a single line comment in C and what it tells your C compiler is that do not not execute this line as an instruction completely ignore this line This is written for me so I can also add some other details to my program using comments there is another format of comment which is multi-line comment whatever you write between these two is a multi-line comment so if I say author Harry Place India I can I can write all 38:50 these U comments inside my um this forward slashstar and star forward slash and this is going to really make my life a lot lot easy because I don't have to create a separate text file also it'll be very easy and straightforward for me to U Mark few places in the program with some text so this is all what comments do coming back to the notes comments are used to clarify something about the program in plain language it can also be a specific place in the program a specific line number so it is a way for us to add notes to a program um there are two types of comments in see uh single line comment is one multi-line comment is another one single line comment is written like this this is the comment forward SL forward SL this is a comment so this is a comment for slash star this is a multi-line comment star forward slash this is a multi-line comment so you can actually press enter and uh write as many new lines as you want but here you cannot add new lines 39:50 because the moment you add new line this particular line is treated as a separate instruction and this might uh cause some errors in the program we don't want that so I'll press contrl Z I'll Simply Save and we can see that a program looks clean if I run this program you can see hello hello I'm learning C with Harry has been printed coming back to the notes comments in C program are not executed and are ignored so comments are ignored by the compiler compiler will not execute your comments as instruction even if it is some code for example if I comment this particular line out I can use a keyboard shortcut which is control for slash to write single line comments so the moment I comment this particular line out this print of hello will not be executed so the moment I run this you can see hello I am learning C with Harry has been printed so this is how this particular program works and hello I am 40:51 learning C with Harry has been printed uh also what I want to do is if if I comment this line as well will something happen no nothing happens a program has no instructions so it simply executes and exits because there are no instructions to execute inside the main function but ideally we'll not have this kind of scenario we'll have some instructions that we want to execute and you can see that the moment I uncommanded using the same shortcut of control forward slash you can see that I am now able to see Hello and hello I'm learning see with Harry printed on the console so I hope you understood all these things I hope this was easy for you now we are going to learn about I would say the most important Concept in C programming language which is a concept of compilation and execution and do not get haunted with this concept because I'm going to explain you in a 41:52 very simple way and you don't have to understand how compiler works if you are a programming beginner yes if you are in the field of compilers if you are a compiler expert maybe you you might want to look into those topics but for the sake of this tutorial for the sake of learning C programming language I do not recommend looking into ins and outs of working of a compiler you should know how to use a compiler just like you know how to use your uh cell phone you know how to use your laptop but there's a high chance that you do not know what is happening inside the chips of your laptop in a very similar way we are going to use the compiler and maybe in the future if you are you know choosing that field as your major you might want to look into what a compiler is but for the sake of this tutorial let's understand how compiler works so first. C in vs code is a file that I created it is no different than some file that we create using notepad or using Microsoft Word or some other text file for that matter uh Microsoft Word is not a PL and simple text file it uh might be a binary 42:54 because it is uh created using word so what basically happens is that we have this source code file we have some characters written inside this file just like we have characters in the notepad so if I show you this file it looks something like this now this file will be taken as input by the C compiler and what C compiler which is GCC what it will do is it will convert it to a code which is understood by my operating system which is understood by the machine I am on in this case Windows machine so for the windows operating system this particular source code will be compiled and for Windows it is compiled to a first. exe file so dotexe is the file extension to which it is compiled in Windows so let me show you this thing uh I'll come back to my vs code I'll open the terminal I'll say GCC I'll say 03 obviously after a space I'll say 03 uncore first. C and the moment I press 43:55 enter I'll get a a .exe so a.exe is the file which is generated but in case I want to get a file with a desired output name I'll say harry. exe so a harry. exe will be created now if I type harry. exe uh oops I need to type something like this it's telling me that you need to type this instead of harry. DXE so I'll obey the instructions and the moment I do this and press enter you can see that this program is being executed so how is it working let me explain it to you once again so first. TC is a universal code file which can be circulated around to any operating system but once you are into an operating system what is an operating system Linux is an operating system Mac is an example of operating system Windows is an example of operating system and there can be a number of operating systems but at this point what we are worried about is we need a compiler for our operating system I have installed a c compiler for Windows and 44:57 and the moment I use that compiler using GCC 03 first. C minus o harry. EXC what I'm telling this compiler is I'm saying hey compiler hey C compiler I have this first. C file please take this file as an input and spit out a file which is compatible with my operating system so first. texe is generated here in this case since I've written harry. DXE harry. DXE is generated but let me do one thing let me make it first. exe so what happens is first. exe this file will be generated after compilation of this source code file now I cannot distribute or rather take this file to some other operating system this is a binary for window this is an exe file for window this will only work in Windows I cannot take it to Linux and I cannot take it to Mac and run directly directly it's not possible to run maybe you can use some virtual 45:58 machine there are a number of ways to run it in Mac but again we are talking about straightforward direct running from the terminal you cannot do that you cannot take an exe file and run it in Mac or in Linux directly you will have to use this source code file and once again compile the source code file for your operating system and then only you'll be able to use that binary I hope I'm clear I hope I made these things easy for you so coming back to the notes a compiler is a computer program what is a compiler now compiler is also a computer program uh and it might sound strange to you if compiler is a computer program who compiled the compiler please do not think all these things just focus on how this compiler is working this is a computer program which is GCC exe I've shown you we have seen this GCC exe file what it is doing is it is taking first. c as the input and it is fitting out an exe file for my operating system this exe file is support by my operating system it converts a c program into 47:00 machine language so that it can be easily understood by the computer and that machine language is not uniform across operating systems it is specific for a Windows operating system for a Mac machine or for a Linux machine for that matter but you will have to compile it for your machine and the source code file can be distributed across operating systems you go to some other operating system take the source code file compile it for your operating system and you are good to go a c program is written in plain text the plain text is combination of instructions in a particular sequence just like we saw here this is an instruction this is an instruction and this program is a set of instructions we have already seen that now the compiler performs some basic checks and finally converts the program into an executable so this is a program this program will be converted to an executable which is an exe file by my C compiler so this C compiler will take the task of converting this particular uh piece of text I would say 48:02 to a piece of binary which is executable or understood by the machine I hope that is clear now let's talk about some Library functions now what are Library functions in C remember we are using this print F but we never defined it where is this printf coming from how are we able to use this print F and how does this work so let's talk about this printf is nothing but a library function so C Lang has a lot of valuable Library functions which is used to carry out certain tasks for instance printf function is used to print values on the screen you can print values using print F just like we are doing this and percentage D for integers percentage F is For Real values and percentage SE for characters we'll look into these once again but if we want to print a number I'll say this is an integer and I'll simply say percentage f uh sorry percentage D percentage D is for integers and I I'll simply say a I can 49:02 say in is equal to three it's an integer int is equal to 3 and this particular thing will print nothing but this is an integer three so you can see this is an integer three has been printed so this is how this um this is an integer three is printed on the screen I hope you got the point I hope you understood how all these things are working and and I hope you also understood how I'm using this particular thing to print this particular integer but what if you want to print a double can I say int B is equal to 3 point uh sorry float I want to print a float so I'll say float B is equal to 3.1 can I print B let's see what happens when I try to print B this garbage value is being printed why is it so this is happening because if I want to print a floating Point number which is a number with a decimal I need to use percentage F so now if I run this program you can see 3.1 is being printed and this will work 50:05 for character as well so I'll have to say car c is equal to and whenever we talk about characters we need a single code so single code and inside single codes will be a character which will be printed on the screen so what what will I do I'll say this is a character I'll say C and this is a floating point Point number and um I'll have to use percentage C for character if I run this you'll see this is a character C this is a floating Point number 3.1 and let me write this for a as well this is an integer and I'll save this is a floating Point number and let me also add a percentage n sorry not percentage n back sln I'm sorry about that back sln is is the new line character it simply adds a new line at the end of your print statement and I'll have to do the same thing here as well so after every line I'm writing a new 51:06 line character just to ensure that whatever I'm writing contains a new line at the end of it so if I try to run this you'll see hello hello I'm learning C with Harry this is an integer why is it showing me zero okay I need to use percentage D for integers uh let me run it once again yeah this is an integer three this is a floating Point number 3.1 this is a character C and I hope you got the point I hope you understood everything that I taught you so far okay let's now talk about the types of variables how many types of variables do we have we have integer variables just like we saw recently I created in equal to 3 and this was an integer okay so we can store numbers from minus infinity to Infinity uh inside this uh variable a and it will not contain a decimal like this if we want a decimal number we use floats so we say float B is equal to 3.1 for floating Point numbers now what I'll 52:06 do is I'll talk about real variables and we have two types of real variables we cannot do in equal to 7.7 this is wrong 7.7 is not an integer it's a real number it contains a decimal and a number after it so we'll have to use something called float so we say float a is equal to 7.7 and this will store this number accurately and correct correctly so character variables similarly we have character variables which are nothing but uh character containers they simply store characters and you can store A Single Character inside these character variables they take one bite in memory and uh let's not talk about receiving input from the user so what I'll do is I'll create a file l04 input. C and what this input do c will do is it will prompt a user to take an input so let let me show this to you let me delete all these U terminals I've opened a lot of terminals I don't want that I'll remove these variables as well now what I'll do is I'll say print if 53:09 enter your number okay and I'll simply say int a to declare an integer so basically what I'm doing is just like if you want to have an empty container ready to store something in your house you go to the market and you come back from the market and keep that container safely so that whenever you have something to store you can simply put it inside that container in a very similar way what I'm doing is I'm declaring an integer so in case I have an integer to store in my program I can use this container or variable and I can say a is equal to 8 and it will be stored inside a so I have simply declared a variable now what I'll do is I'll use a function called scanf and I'll simply say percentage D because I want to take a number as an input and I'll simply say am person a and let me explain this thing to you because this might sound very scary and also let me show you something before we discuss about this I'll say print F your number is 54:12 percentage D and I'll simply say a and definitely I'll put a semicolon let me run this program and see what happens enter your number six your number is six let's try this program once again and see if it works for variable inputs yes it does your number is three how is this thing working so scanf is basically a function it's a library function and you can use scanf to take input from the user so what you are doing is you are saying to the compiler that I am going to enter an integer from the keyboard kindly store that integer at this location simply look at the syntax and you are good to go so if you look at the syntax scanf percentage D and uh the memory location where you want to put that number which you are going to enter this is all you need to know about scanf at this point in the course I'm going to explain you more about scanf and functions in the future but let's look 55:13 at this am percent operator this is very important I've even written this uh red color in the notes this am percent is important am person is the address of operator and it means that the supplied value value should be copied to the address which is indicated by variable I so when I say % I I'm saying the memory location of I in the memory the location of I in the memory is ENT I and what is I I is the value at that memory location I don't want to know which location I just want to know the value let us say I'm talking about you I'm talking about a guy named sham if I say sham is a good boy I'm commenting on sham if I say m person sham is a good place I'm simply saying the place where sham recites is very good I'm not commenting on sham sham can be a good boy or sham can be a bad guy as well I'm not commenting on me I'm simply commenting on the place where sham is living I hope this address of 56:15 operator is clear to you so that was about chapter number one I hope you like chapter number one and you enjoy the concepts of variable constants and keywords but this is not it we are going to practice is chapter number one and the concepts in chapter number one using a practice set we are going to move to chapter one practice set where we are going to solve few questions so let's move on to chapter one practice set and solve some problems now that we have learned so much about C we are going to look into this practice set so that we can practice the concepts that we have learned so far so the question number one says write a c program to calculate area of a rectangle and it is going to be very simple because we are going to use hardcoded inputs also we'll use scanner function to supply input from the user end let's start coding this problem so what I'm going to do now is I'm simply going to create a new file which is program number five and I'll simply say pr01 Doc which basically means that I am going to um I'm going to 57:17 write the first problem solution of my practice it so what I'll show you is the moment I just see boiler plate code so I simply said Boi and press enter you'll see that something happened how did I do this I want to show you so if you click on your settings in Visual Studio code and you go to Snippets user Snippets and you go to c. Json you can add something like this and this will basically be the shortcut to type a very long boilerplate code repeatedly let me explain so what I'm doing is I'm adding to this Json a key value pair which is an object so if you don't know what Json is simply copy this you can look into this stuff and directly make your c. Json look something like this so we have a prefix we have a body we have a description prefix is something you will use whenever you want this body to be added to your program make sure that you use back sln for new lines and also back SLT 58:19 for tabs uh if you look into my code you can see that I have a tab here a tab here just because I had back /t here so make sure that you type this and then you can add description of your choice and we can have as many Snippets as we want in this particular c. Json if you're not able to add snippet for any reason maybe you are in some different source code editor or something do not bother about that because you know what you'll simply have to type these many lines that's about it you don't have to worry and if you have the cheat sheet that I've supplied to you so let me show you the cheat sheet I'll simply go to my website and the blog and we'll open the C language cheat sheet you simply copy this and you can paste it in and that's about it you have this boilerplate code okay so you can add your code here and you know what you can simply either use the boiler plate or use the cheat sheet it doesn't matter you should know how to use both of these 59:20 things let's come back to the problem I'll simply say in a is equal to 4 and say in B is equal to 7 what is A and B A and B is nothing but the height and width of U of my rectangle so I have to Simply calculate area of the rectangle so using hard coded input so what is the area of rectangle I'll simply use print F I'll say the area of rectangle is um I'll simply multiply a by B I'll say percentage D and I'll perform a calculation I'll say a star B and that should do that should do the trick so percentage D is for integer my a is an integer B is an integer a is the width B is the height and a multip by B will give me the area of the rectangle let me run this program for you and show you that the area of the rectangle is 28 because 7 * 4 is 28 and this program works what if I want to use a user 1:00:22 supplied input in that case I'll simply declare a and B and then I'll simply say scanf percentage D and I'll want my user to enter a and I'll do the same for B but before I make my user enter something I'll simply put a message for him I'll say enter the value of length back sln and then I'll say enter the the value of breadth dth and if I enter the value of the breadth it will be stored in B because I've WR be here let's run this program and the value of length eight and the value of breadth let me say three and you can see 24 what if I go for five and two it should be 10 and yes it is so the area of the rectangle has been calculated using the hardcoded inputs and using the inputs supplied by 1:01:25 the user now what we are going to do is we are going to calculate the area of a circle and modify the same program to calculate the volume of a cylinder given its radius and height if you don't know man duration or you are not into a lot of mathematics bear with me I'm going to explain everything to you so this question says calculate the area of a circle which is p pi r squ so let's prepare for calculating the area of a circle in a program called 06 p02 doc let me add the boiler plate code and I'll say int R is equal to 4 and now what I'll do is I'll say print F the area of circle is percentage D and what I'll do is I'll put down the value of pi and then I'll simply say R and then once again I'll say R so Pi * R * R is the area of the circle P pi r squ let's 1:02:25 run this program and see the area of the circle is this thing uh which is a bit problematic because I have used percentage D I should have used percentage F because this is going to evaluate to a floating Point number so this is something that you need to remember now if I run you'll see 50.2 4 4 * 4 is 16 * 3.14 will be approximately around 50.2 4 which makes sense okay I hope hope you understood this now I have to modify the same program to calculate the volume of a cylinder which is p piun rยฒ * H where H is the height of the cylinder let me show you these formulas area of circle and also I want to show you volume of a cylinder so area of the circle is P piun R square I'll enter the value of R and it will show me U the area so when the area 1:03:25 is four uh sorry the radius is four the area is around 50.26548 1:03:57 and I'll say multiply by h four and five four and five let's see what we get we got 21.2 and yes we got something very close once again I'm using an approximate value of pi 2 point Sorry 3.14 is an approximate value of pi so make sure you understand that the value of pi you are using might be a bit different from what is being used in the online formulas because of which our results are going to be a a bit different so they got 25133 and I got 21.2 which is pretty close but again not exact just because of the value of pi but congratulations we have solved this problem moving on to the next question right a program to convert Celsius degrees temperature to Fahrenheit so what I'll do is I'll say celius to forhe and here is the formula I'm going to copy this formula as is and before that what I'm going to do is I'm going 1:04:59 to write a new program name 07 pr03 and this is a c program so I'll say boiler plate code I'll simply cut this and uh I'll simply write a comment here I I'll simply refer to this formula I'm not going to use it exactly I'll say n c is equal to 5 and I'll use printf and I'll say [Music] the value of temperature in fenit is percentage F and I'm going to use this formula but it'll be easy for me if I use something like this let me paste this formula let me fix uh this formula and replace x with star because in C star is the multiplic ation operator not not X okay so I'll simply say C multiply by 9 by 5 let me 1:06:02 close it in the brackets and yes this works and I'm going to Simply say F let me write semicolon at the end of print F and also at the end of this expression and this should do the trick so if I say C is equal to 4 I should get 36 let me punch in four I got 39 looks like there is some problem with my formula so let me see what the problem with my formula is I think I've replace the value of C wrongly so this should be C * by 9 by five and uh yeah I got the I got the problem so the problem is that the value of C is an integer 9 is an integer five is an integer simply say 9.0 / 5.0 and I'm going to explain to you why this is the case why you need to use 0 Z so let me run this and the and we get 1:07:02 the correct answer but allow me to quickly answer you this question that why did 9x5 not work and why is this 9.0 by 5.0 is working so whenever there is an operation between integer and integer in C the resultant is also an integer so 9x 5 will be rounded off to the nearest integer and this will be the value and then it is multiplied by C and then 32 is added to it which creates a lot of error when we say 9.0 by 5.0 we get the exact value in decimal because this is evaluating to a float Float by float is a float int by int is an INT so this value will be a float multiply by an integer as a float and then we add an integer as a float so this is why we are getting uh the correct value using this formula I hope you understood it I hope you got the point now coming back to the next question which is write a program to calculate simple interest for a set of values representing principle number 1:08:04 of years and rate of interest and this is very straightforward I'll show you something uh simple interest formula and simple interest formula is really very simple it's PRT ided 100 so if you want to calculate the interest this is uh PRT by 100 but a is a amount so you need to use PRT divided by 100 so why divided by 100 because we have the rate of interest in percentage if the rate of interest is not in percent then you can simply use the decimal rate and then you don't need divided by 100 but you if you have the rate of interest in percentage then you need to do divided by 100 so if you have 4% rate of interest 9% rate of interest then you definitely will have to do divided by 100 so simply apply this calculation use PRT divided by 100 and that's about it you'll get the answer I want you guys to comment below if you have solved this problem I want each and 1:09:06 everyone of you to comment and say that we have solved question number four I have solved question number four take accountability of yourself and tell me what is the answer for the simple interest if the value of principle is 100 number of years are four and rate of interest is six let me repeat it 100 4 and six rate of interest is 6% for the four years principal amount is 100 how much will be the final amount how much will be the simple interest you need to calculate the simple interest so tell me the simple interest tell me what will be the simple interest for these values I am waiting for your comments I hope everybody posted the comment and also don't mention to put the time stamp when you commenting so that I'll know where or which part of the video you are at when you are commenting I I hope you enjoyed practicing all these questions from chapter 1 practice set now we'll move on to chapter 2 and we'll Explore More concepts of C let's move on to chapter 2 now so what I'll do now is 1:10:07 I'll open this chapter 2 PDF so let me open it in my Chrome browser and in my opinion Chrome browser is the best PDF viewer in my opinion I mean there are a lot of other PDF viewers and it doesn't even matter if you have a very powerful computer coming back to the C topic chapter 2 instructions and Opera what are instructions instruction is something you tell computer to do just like you give instruction to your younger brother to bring something or your dad to bring something from the market while he's in the market or your mom might give you instruction to get some groceries uh in a very similar way we use C program to give instructions to the computer so a c program is a set of instructions it is collection of instructions indeed so if I show you my my vs code you can see that this particular C program is set of this instruction this instruction this instruction and this instruction so coming back to the notes instructions 1:11:08 are nothing but instructions are nothing but commands for computer to do something just like uh uh if you want to prepare a particular dish you will have certain set of instructions add salt add pepper maybe you'll have some instructions of marinating something for some time these are all instructions for preparing a particular dish you might want to um you know cook some vegetable you might want to cook some oat meal and there is a way to get there basically using some set of instructions in a very similar way we write programs and their collection of instructions now let's talk about types of instructions what are the types of instructions we can give um to a computer using C programming so if I show you my VSS code you can see this particular type of instruction and this particular type of instruction are a bit different they are not exactly the same they're not exactly telling the computer to store something in the 1:12:09 memory this particular instruction is saying that store four inside this variable C at some place in computer's memory and this particular instruction is saying print this particular thing in the console fetch the value of F and for format it this way print it in a way that it looks beautiful just like this so this is an instruction and this is instruction but these two are different types of instructions so coming back to the notes there are three types of instructions primarily first one is type declaration instruction second one is arithmetic instruction third one is control instruction so let's talk about type declaration instruction remember we talked about int a float b car c we can have even more data types and we can use those data types to declare a variable so when we say int a we are basically saying Reserve four bytes in memory which is taken by int so an integer in C 1:13:11 language takes four bytes what I'm telling my computer is hey computer please reserve four bytes in memory I want four bytes to be reserved in the memory so int a means Reserve four bytes in the memory float b means Reserve 4 bytes in the memory for a floating Point number then there are some other variations of this type declaration instruction int I is equal 10 J is equal to I int a is equal to 2 so basically here I'm saying give I the value of 10 so 10 is a constant and it will be stored inside I when I say J is equal to I what I'm telling my computer is I'm saying fetch the value of I and put it inside J then when I say in T is equal to 2 I'm assigning a with two and in a very similar manner we can have such kinds of variations of type declaration function but if I do something like this float B is equal to a + 3 float a is equal to 1.1 this is going to throw an error as 1:14:12 we are trying to use a before defining it so you need to define a thing first remember in C language instructions are executed from top to bottom so they are executed one after the other so this will be executed first this instruction will be executed after that this will be executed after that and this after that and so on so here if I say in C is equal to 4 I can actually use C in this particular instruction but if I try to use C in some instruction and then I Define c that is going to get me into big trouble okay so I hope that you understood this I hope this concept is clear to you now int ABCD is a shortcut to tell please Reserve four bytes for a b c and d so this is a shortcut so instead of writing in a in b and c and d in four different lines we have a shortcut we are writing all these things at once in ABCD and if we say a equal to b equal to Cal D is 1:15:13 equal to 30 value of a b c and d will be 30 each I hope this thing is clear to you now let's talk about arithmetic instructions in I is equal to 3 * 2 + 1 when we do something like this what we are telling our computer is perform these calculations we are telling our computer perform three mtip by two and then add one to it so basic mathematics but let's see some terminology when I say inti is equal to 3 * 2 + 1 uh here star and plus are operators and these numbers are operant so basically we have these operants which are operated upon by The Operators so op operator can actually operate on these operant and then produce a result and that result can be stored inside I so when I say in I is equal to three star 2 + 1 basically 6 will be the result of three star two and when I say three star 2 + 1 then it 1:16:13 will be 6 + 1 which will be 7even oper operant can be in float Etc it can be anything and plus minus times divide our arithmetic operators okay there are some other arithmetic operators as well but these are the most basic ones then if is a b is equal 2 comma c isal 3 it means Please assign B with 2 and C with 3 and when I say int Z is equal to B * by C or rather if I say in Z and then after semicolon I say Z is equal to B * by C this is legal but if I say in Z and I say B multip by C is equal to Z this is illegal this is not allowed because when I say Z is equal to B * by C the right right hand side will be evaluated first in this case when I say B multiply by C is equal to Z the right hand side doesn't have any value this is illegal you cannot do that so whenever you want to assign something to a variable this needs to be clear answer this should 1:17:15 have a clear value B multip by C here has a clear value of 2 and three because V is clear it's the value two c is value three and B multi by c will be value six so we are all clear on what B is what C is and hence we are clear on what B multip by C is but had we not had this b isal 2 c isal 3 instead of this if there was just in B comma C this would have created an error so I hope you understood this and we'll practice and we'll see how these things are working so you don't have to worry on anything because I'm going to hold your hands and walk you through all these Concepts both in the notes and in code as well so practical is very important this is something that I totally understand and we are going to do the Practical as well now percent is modular division operator it Returns the remainder and it cannot be applied on floating Point number percent sign is same as numerator so basically when you do something like this if you say 5% 2 what is the 1:18:17 remainder when 5 is divided by two it's one so it returns one but if I do minus 5% do I'll get minus one so sign is same as that of numerator we we rarely use minus 5% to you know modu with negative numbers is something that you will very rarely see but again if you were to see this is the rule if there is minus sign before this five it'll be transferred here so minus 5% 2 is equal to minus1 now you need to note some things no operator is assumed to be present you cannot say it I is equal to AB B just like you do in basic mathematics and ab means a multiply by B no it doesn't hold in C language there is no presumption on any operator being present if you say in I is equal to a star B yes star is present between A and B so we can multiply a and b and the value of I will be a multip by B but if such kind of operator uh is not present if you say I is equal to a this is invalid this is 1:19:17 invalid and this is going to throw an error there is no operator to perform exponentiation in C however we can use p x X comma y from math.h more later so there's no direct operator in C to perform exponentiation if you want to create um Square you can do a star a if you want to find out cube you can do something like a star a star a but if you want to do a to the^ 39 there's no other way directly than to write a39 * manually but there is a function called pow where you can do X comma Y and it will evaluate to X to the^ Y and it is from the library matt. we are going to look into the libraries very soon later in this course but again this is something that you need to keep in your mind that yes we have a function we have a handy function it's not like you cannot do it in Z you can definitely perform exponentiation very very easily in see so we'll have to use something called power and we are going to learn matt. later so let's skip it and move ahead 1:20:19 type conversion what is this type conversion so there is a rule that an arith Matic operation between int and int will result to an INT an arithmatic operation between int and a float will result in a float an arithmetic operation between float and Float will result in a float so at a very high level roughly I can say that int and int or rather any data type and other data type the result will be promoted to the higher data type so int and int will return an INT int and Float will return a float float and Float will return a float so 5x2 is two not 2.5 in C and A lot of people they face a lot of trouble because of this so what you have to do is 5.0 by 2.0 if you want to get the accurate answer but if you do 5.0 by two then also you can get the accurate answer if you do 2x 5 it will be zero but if you do 2.2 by 5 it be 04 reason being it will be a calculation between a float and in and it will be a float but here it will be a calculation between int and int and it is going to be an INT 1:21:21 which is zero and hence it will be wrong wrong mathematically now let's talk about some noes uh so when you say int a is equal to 3.5 in this case 3.5 which is a float will be demoted to three because when you say in a is equal to 3.5 a cannot store 3.5 because it's an integer you cannot store a floating Point number inside an integer variable so what will happen is it will be demoted to three because a is not able to store floats this is something that you need to keep in your mind then if you say float a is equal to 8 a will store 8.0 so 8 will be converted to 8.0 and it will be a promotion to float So when you say float a is equal to 8 a will not directly store eight because a is a floating Point number and since a is a floating Point number it will definitely have decimal so float a is equal to 8.0 and Float a is equal to 8 will be the same when you say a is equal to 8 in floats it will be 8.0 by default now here's a quick quiz for you what will 1:22:22 happen if I do int K is equal 3 3.0 / 9 what will be the value of K and Y so if I do something like 3.0 divided by 9 this particular thing will be evaluated to 3x 9 some 0.33 something uh and since this is an integer K will be zero because K cannot hold floating Point numbers now if I do something like 3.0 divided by 9 it will be 0.333 but since K is an it it cannot store floats and value this is demoted to zero and yes we can see see this we can definitely see this in action let me open this in vs code and we are going to create a first program uh let me call it demoting do C or rather let me call it calculation. C I'll put the boiler plate code I'll say int a is equal to 3.0 / by 9 if I'm not wrong yes and now what I'll do is I will simply say print F and yes I want to do print F the value of is let 1:23:26 me say percentage D and if I try to print the value of a it is going to be zero so hence it is demoted to zero it was 3 point Sorry 0.33 but it will be demoted to zero because a cannot store floating Point numbers what will happen let me close this one what will happen if I make it float and if I make this percentage f will it be 0.33 yes it will be 0.3333 so this is something that you need to keep in mind I'll make it int because you might refer to the notes and you will uh see that program for sure now let's talk about operator presence so before I talk about operative presidence and associativity let me also tell you that this is one of the most confused topics when it comes to C language and I'm going to demystify all these things for you because in my opinion this is really very simple and these are unnecessarily Complicated by people so 1:24:27 let's talk about operator precedence what if I do 3 star x - 8 star y it is 3x - 8 y or 3x - 8 Y what will this be evaluated to if I do something like this and see there should be a start here I want to put a star here 8 star y so is it this expression or is it this expression because these two expressions are different they are not the same this is something that you need to keep in your mind in C language simple mathematical rules like like Bard Mass no longer applies you cannot directly apply board Mass from mathematics to C you you cannot do that the answer to the above question is provided by operator precedence and associativity and operator precedence and associativity are one of the most important concepts of see let's see what these concepts are let's talk about operator precedence the following table lists the operator priority in C so the first priority is given to Star slash and person these these three have the same priority then second most priority is given to plus 1:25:29 and minus and third most priority is given to equals to now you can Google the priority chart and you will definitely find priority of other operators but these are the most used operators and these are the operators whose parity you would want to learn yes remember all these this is not difficult to remember let me tell you the first row contains star/ and percent second one contains plus and minus and third one contains equals to so this is something that you need to remember operators of higher priority are evaluated first in absence of parenthesis so yes parenthesis will definitely evaluate first because the meaning of parenthesis is that you want something to be evaluated first but in the absence of parenthesis what happens is that priority of The Operators is checked and then these operators are applied to the operants so let's let's see this in action I want to show you this in action so I'll simply create a program called priority. C boiler plate code int a isal to 3 * 5 minus U 5 and 1:26:33 let me also do 9 + 3 * 5 - 5 and I'll simply do print F percentage T I'll say value of a is percentage T and what I'll do is I'll write a here let me run this program the value of a is 19 let's see if we are able to use this priority table and find the value of a so 5 * 3 this will be evaluated first which is 15 then 15 + 9 is nothing but 24 minus 5 is nothing but 19 and hence the result 19 I hope you got this point now do not apply board Mass here some people might argue that board mass is directly being applied here no do not apply board Mass you have have to go with operator presidence and associativity because sometimes in a very rare case maybe B Mass might not work because these expressions are not 1:27:33 evaluated using Bard mass or some other mathematical rule for that matter these are evaluated using operator precedence and associativity so you need to keep this in your mind now coming to operators of higher PRI valuated first now let's talk about associativity so we talked about Pres what is associativity when operators of equal parity are present in an expression the tie is taken care of by associativity so what if you have a star and divide what will be performed first so let me show you an example of this what if instead of this I had this 9 ided 3 * by say 27 - 5 now if you perform this thing first then yes uh answer will be different if if you perform this thing first answer is going to be different what is the rule that c follows so firstly what we do is we know that either slash which is divide or 1:28:34 star is going to be executed first but which one out of these and let's see what the associativity rule says so when The Operators of equal priority are present in an expression the ti is taken care of by associativity so XR y by Z is evaluated to xar y ided z x by y star Z is evaluated to X x y y star Z and Stars slash follows left to right associativity so what is left to right associativity left to right associativity simply means that you need to evaluate the left expression first then the right so 9 by 3 will be 3 let me write a comment for you so it will be 3 * 27 minus 5 which will be 27 * 3 - 5 which is 49 okay so let's see if we get 49 so this is nothing but 3 * by 3 * 9 which is 9 * 9 which is 81 - 5 which is nothing but 76 okay so let's see if we get 76 and if I run this program the 1:29:36 value of a is 76 yes this works so star and slash follows left to right associativity and we need to evaluate them from left to right always remember okay when it comes to remembering the priority and associativity let me tell you something this is all you need to remember number and you'll be able to solve most of the questions 99% of the questions but in some of the rare cases you might want to Google the priority of some of the miscellaneous operators so the rule will be applied just like this first find the priority once you have the priority you need to evaluate the operators which have the higher priority which one will be evaluated first you need to find the associativity if it is left to right then yes you will um you'll use um the left to right associativity to evaluate those operators go move from left towards the right and keep on solving The Operators I hope you got the point I hope this is clear let's talk about control instructions remember we were talking about instructions uh we had type declaration instruction arithmetic 1:30:37 instruction then the third one was control instruction the type declaration function look like this sorry instruction look like this then arithmetic instruction looked like this now let's talk about control instructions now there are four types of control instructions in C it determines the flow of control in a program what is a control instruction it determines the flow of control in a program where to go when what will be executed first so there is a sequence control instruction there's a decision control instruction there's a loop control instruction and there is a case control instruction sequence control instruction is nothing but what we already saw then there is something called decision control instruction we'll look into if else in the later chapters and you will come to know what decision control instruction is then we have Loop control instruction which is nothing but some of the loops y Loops for Loops do wi Loops we'll look into Loops do not worry about loops but this is one of the control instructions is something that you need to remember 1:31:39 you might want to come back to this page from the notes when we look into uh look into Loops andl statements and switch case statements so case control instruction is something that uh we'll look into later as well so these are the pH instructions we are going to look into the later chapters and let's now do something which can strengthen our concepts of instructions and operators let's go to chapter 2 practice set and solve for some of the questions on instructions and operators let's move to chapter two practice set now now before we look into the C chapter 2 practice set I want to discuss few things and these things are going to be really important so listen to me very very carefully we'll look into the data types and see so we'll learn about the four major data types in C which are cars so let me Mark it as question number one so program number three question number one from the practice set and do c is the extension let me write down the boilerplate code and let me write down 1:32:40 the first character so let me type it down uh there are four major data types in C what is the first one in C we'll talk about the first one and the first one is uh let me also put something like this it's int then we have then we have float for floating Point numbers then we have car and car is for characters and then we have double and double is for it's for uh decimal numbers but with double Precision than the floating Point numbers okay so this is single Precision floating Point number and let me also close this and what I'll do is I'll now write a double Precision floating Point number so let me type double here and then character is for storing characters enclosed in single code so this is 1:33:41 something that is important you need to enclose a character in single codes you cannot do something like this to store a character you'll have to do something like this so let me type it down this is valid okay so this is important and then this is for storing integers okay so this is something that you need to keep in your mind now let's solve the question number one so coming to the question number one which of the following is invalidy first one is valid second one maybe let's check it it's invalid by the way but in case you want to check whether it's valid or not you can simply use this so if you you have done some sort of mathematics and uh you want to use this character to raise the power of 3 to three it is going to be invalid but if you want to see whether this actually runs or not this is going to run because this is a bitwise character in C so we are not going to look into bitwise characters in this video but this is 1:34:43 actually valid this is valid in C this will this is not valid to calculate the power of 3 to the^ three so this is valid so you need to make sure that this is a valid character in C even if you don't know about a character that might be a valid character in C so this this this is the takeaway of this question basically and what about the third one is definitely invalid because you need to store a single character in a car variable you cannot store a date you can't do this so this is something which is invalid so you need to make sure that this this is valid what data type will 3.2 / 8 -2 return and this is clearly very simple this this particular thing will return a float and the entire thing will return a float right or wrong wrong let me show you why so um I know I know you are you might be thinking that uh why is it wrong what what what's wrong about this so let me tell you so this is problem number 2.c let me put the boiler 1:35:46 plate code and let me show you so what I'll do is I'll simply say uh uh 3.0 / 8 - 2 so if I do something like this uh I'll say 3. 6666 what is this is it a double is it a float it's by default a double so whenever you write a decimal number in C by default it is double but sometimes it might depend upon the compiler but in most of the cases it is double by default so since this is double so the entire thing is going to be double so let me save this program for you and let me also show you why this entire thing will return double double divided by integer will be a double double minus integer will be a double so this will return a double data type I hope that this is clear and you are able to grasp the concept I want to teach you write a program to check whether the number is divisible by 97 or not and this is going to be simple let me change this to pr03 1:36:49 pr02 was very simple so I just want to check whether a number is divisible by 97 or not so I'll say int a is equal to let us say I have a number which is 194 and I want to check whether this is divisible by 97 or not so what I'll do is I'll do print F and I'll do percentage D and let me see if I get a remainder so I'll say a% 97 uh and let's see whether this is divisible by 97 or not so run this program I get zero so the remainder is zero which means it is divisible what about 195 uh it's one remainder is one so this is not divisible by 195 97 doesn't divide 195 so 195 is not divisible by 97 okay I hope you got this point and this is a program to check whether a number is divisible by 97 or not now I want you to refactor this program and convert it to a program which takes an input from 1:37:51 the user I want you to write a program which takes an input from the user Yes you heard that right now uh this is it for this question I hope you will accept the challenge and show me the program wherein you are accepting input from the user I want all of you to comment that program um in the comment section make sure to write the time stamp so that I can see which question you have solved okay coming back to the practice set question number four says explain step bystep evaluation of this thing where x equal 2 Y is equal 3 and Z is equal to 3 and K is equal one and this is important this is very important so it's 3x / y - Z + K let me write it down let me create a new file and then write it down okay so let me put the boiler plate code I'll say in result is equal to and let me write it down 3x by y - Z 3x by y - Z + K but we we still have to write down all 1:38:54 these um all these variables X is 2 Y is 3 Z is 3 and K is 1 okay so X is 2 Y is 3 Z is 3 and K is 1 okay so let's try to find out what this particular thing will return and yes you can always say int result is equal to expression this will put the value of the expression into result variable also what I'll do is I'll use my printf I'll say the value of result is and I'll do percentage D okay and I'll say result but we need to explain the step byep evaluation so the first thing we are going to see is the priority or rather precedence what is the Precedence which which one is the operator with highest precedence star and divide are the operators with highest precedence what is the associativity because we have two such operators one is star and one is divide it is left to right so 3 * X will be 1:39:57 evaluated first which is going to be 6 so let me write it down 6 ided and the rest of the numbers will be uh written as is 6 / 3 - 3 + 1 okay so this is going to be the resulting expression now what I'll do is I'll simply evaluate 6x3 to 2 why because this is going to be the operator with highest precedence star and divide are the operators with highest precedence let me show this to you if you have not seen it previously um I've written it down in the notes for you so if you look at the priority of these operators you will see the first priority is given to these operators second is to these operators now we since we have operator with the topmost priority we will have to evaluate it to the integer now 2 - 3 + 1 is going to be zero so the answer answer should be zero and let's see if this is the case and yes this is the 1:40:57 case answer is zero so this is how we can evaluate this particular thing step by step now this is a this is an important question what is this is it a 3.0 is 3.0 + 1 an integer character floating Point number it is going to be a floating Point number for sure but not float it is going to be double and it might depend on the compiler as well your compiler might store the default floating Point number as floats you never know you can always design a compiler which stores a floating Point number as a float but in my case the compiler I'm using it is a double but it's always a floating Point number so B is the correct answer I hope you were able to understand all the concepts pretty well and the moment we you know start exploring more and more Concepts in this particular course the basic things are going to be clear for example uh we can we can do something like this is something which you know now we can do something like this is something you know now you always knew that we can do intx samigon so all these things are 1:41:57 going to make your life easy all these programs that I'm going to make for you are going to make your life easy when it comes to the revision so make sure you download the notes you you don't know how much hard work I have put into you know bring these notes for free for all of you I would appreciate if you download it and start learning C and become a champion of c and if it helps you that is a huge win for me let's move on to chapter 3 now where we are going to understand the concept of conditionals so now that we have some good idea of the C programming language let's move towards chapter number three I'm going to open this PDF that I've created for you and let's see what this PDF has to say so I have written some of the lines which will sound a bit fun and it'll make things easier to understand for you for example conditional instruction is something that might sound boring but I have tried to make things interesting so this chapter is about conditional instructions what exactly are conditional instructions most of the computer programs use conditional expressions and what does 1:42:58 this mean they simply mean that whenever you are writing a program you might want to do something and you would want to execute an instruction only when a certain condition is true so I've written this sometimes you want to watch comedy videos on YouTube if the day is Sunday so if the day is Sunday you might want to watch a comedy video on YouTube otherwise you might want to watch some study videos on YouTube sometimes we order junk food if it is a friend's birthday in the hostel so you might be dieting uh year round but when it's your friend's birthday or your birthday or some other friends birthday you might you know Ease on your dating and you might go out and eat some junk food or maybe order it from outside so in all these scenarios what happens is based of a certain condition you are doing something based of some condition you are doing something you're watching comedy video when the day is Sunday you are eating junk food when it's your friends bir in the hostel you might want to buy an umbrella if it's raining and you have the money so there are two conditions here if it's raining and if 1:43:59 you have the money if you have the money then only you'll go and buy the umbrella otherwise you would not want to buy the umbrella you order the meal if Dal or your favorite Bindy is listed on the menu so if your favorite food item is listed on the menu then only you will go and order the meal okay so these are some of the conditions based on which you are performing certain actions all these are decisions which depend on a certain condition being met in C language 2 we must be able to execute instructions on a condition or more than one condition being met so this is decision making instruction in C so we have two types of decision- making instructions in C we can use an F ladder we can use just an if and also we can use switch statement so we'll look at F statements and switch statements one by one so let's get started by opening this folder in vs code so let me open this folder in vs code so simply right click and click on open vs code open in code and let me write the very first program for you so I'll simply say if else doc 1:45:01 I'll put the Boiler code uh boiler plate code down here and I'll show you something let us say I have a variable and I'm assigning three inside a variable which is an integer and the name of the variable is a now what I want to do is I want to check if a is greater than three or rather four it can be either three or four I want to print a is greater than 4 okay otherwise what I'll do I'll do nothing I'll not print anything or if I want to print something when a is not greater than four I'll simply say print F print F and I'll say a is greater than is not greater than four okay so uh I'll simply do this and I'll press a semicolon and now what I'll do is run this program and you'll see that this program is executed and a is not greater than four because a is three now what if I make a 32 and if I run it you'll see a is greater than four so this is an example of if else 1:46:04 conditional statement coming back to the notes we have another type of conditional or rather decision making instruction in C which is switch case statement so we just saw the syntax ofl statement we simply write condition to be checked inside if and if that condition turns out to be true then what we do is we simply write some statements or um we write some instructions which we want to get executed and if that condition is not true then else is executed so it's kind of simple plain and simple English when we write this if else statement if this happens do this else do this so this is just to make things easier for the programmer uh at a very low level these instructions are compiled into some other instructions which your CPU can understand but we are not going to go into those details what we want to do here is we simply want to be able to write IFL statements we want to write some conditions inside C inside if and uh whenever those conditions are true we want some statements to be 1:47:04 executed and if those conditions are not true then we want something else to be executed so let's see a code example uh just like I show you this example here we have another example where we have an integer variable a the value inside this integer variable is 23 if a is greater than 18 we are simply saying yes you can drive otherwise you cannot drive uh I don't have any else here and that's totally fine you can simply have an F without an else and if that condition is true then only these statements will be executed otherwise these statements will not be executed so when I say int a is equal 23 I can simply say print F you can drive if a is greater than 18 now note that else block is not necessary but optional else block is optional so you can either have else or you can simply have an if and that's totally fine even from our program here if I comment out my L statement let me show you this program works perfectly so let me show you the terminal so a is greater than four and if I make it two if I save 1:48:07 the program and run it once again nothing happens because I don't have an L statement here but the moment I comment uh uncomment it and run the program a is not greater than four is printed which means that now a is not greater than four so I'm actually able to check whether the value of a is greater or lesser than a constant which is four so now in C language I'm able to write some logical things I'm able to perform some logical operations I can check whether certain condition is true or not I can compare a variable with a constant I can compare two values which is pretty cool and we are going to look more into this so note that else block is not necessary but it's optional and we just saw that now let's talk about about relational operators in C what are relational operators in C so don't get scared by the names of these relational operators logical operators these are really very simple let me tell you just understand the concept behind it you don't even have to remember down the 1:49:08 line which one is relational which one is logical because you are going to automatically remember all these things in programming whenever a beginner comes to learn all these things what they think is that they will not be able to learn so many terminologies in programming but believe you're going to remember everything by yourself and at this point do not focus on remembering what relational operator is how many types of relational operators are there but rather practice and write more and more programs that will get you the programming success that you want so let's talk about relational operator and C relational operators are used to evaluate conditions true or false inside the if statements some examples of relational operators are double equals to note that this is double equals it's not single equals single equal to in C is assignment operator and you don't have to confuse equals to with dou equals to dou equals to is a comparison operator which will tell whether two values are equal or not now this greater than equal to will tell you whether a 1:50:10 value is greater than or equal to some other value or not similarly we have just greater than and less than then less than equal to not equal to is exclamation mark and then equal to so this is not equal to and U this is also so uh something that we are going to use a lot in this course so you need to remember a bit all of these operators initially and um you don't have to mify all these things what I want you to do is just reference these notes and you know look look up all these things and once you write few programs everything is going to sound very very simple to you so I have an important note here equals to is used for assignment whereas double equal to is used for equality check I just told you this that that beginners will confuse this double equals to with single equal to and you don't have to do that equal to is assignment operator and we have already used equal to here we in fact we just used it in this program in a is equal to 2 here we are not comparing a with two but rather what we are doing is we are assigning the value to inside of this 1:51:11 variable a so you need to understand this coming back to the notes double equal to is comparison operator which will actually tell whether two values are equal to each other or not now the condition can can be any valid expression and see a nonzero value is considered to be true this is super important and let me shout here so that you know I can emphasize that this is super duper important and I'm shouting like this so that I can emphasize enough on this any nonzero value is considered to be true in C and you must be thinking why I'm so excited all of a sudden I want to emphasize on this thing any nonzero value is considered to be true so even if I put if1 this is true even if I put if 10 if 10 is considered as true whatever is there inside the if block will get executed you don't have to have a greater than four or you know a less than 5 or a dou equal to 10 no you don't have have to do that even if you have a nonzero value if it is a 1:52:12 non-zero value your if block is going to execute for sure this is something that you need to remember here now what I'll do is I'll simply come back to the notes and uh the condition can be any valid expression and see nonzero values consider to be let me emphasize a bit more and I know that you you'll think that why am I emphasizing so much but this is important and I can't emphasize more on this let me write a program for you nonzero Doc and what I want to tell in this program is that if you have a nonzero value and if you write something like this print F by the way uh I write programs in python as well which is why you know whenever I type I just write print but that's fine even if you're coding in multiple programming language uh evaluated to True uh or rather let me tell any nonzero value is evaluated true semicolon save and if I run this any 1:53:15 non-zero Valu is evalu to true this is something that you need to know and a lot of people will tell me why are you emphasizing so much you have written it in the notes you have told it several times but this concept is very important which is why I have to emphasize it this way let's talk about logical operators now we just talked about relational operators and um I hope that you will understand uh what relational operators are just try out these operators greater than equal to less than less than equal to I think this is pretty straightforward and you don't have to you know uh rather I don't have to show you each and every operator inside of if statement I I hope that you'll practice practice all these operators yourself now let's write another program and we are going to talk about logical operators in this program so let me write a boiler plate code and uh let's talk about logical operators double and or and exclamation mark this is actually a pipe sign double pipe this is double end this is double pipe in case you are 1:54:17 wondering where is pipe in your keyboard let me show it to you can you see this key this key this one this is the pipe okay I know that it sounds really crazy me holding the keyboard and you know holding it in front of the webcam but I hope that you understood let me Google keyboard and I think this is a better way of showing the pipe key so if I open this image this is the pipe key this one oops let me scroll this one so you'll have to press shift and then you'll have to press this key and this will give you a typ this is something that you might not have used a lot while you are typing or maybe surfing the web or doing anything in the computer but when you're programming you'll use this a lot so let's talk about these logical operators at a very high level and then we'll come to the notes and see what notes have to offer so I'll simply say int a is equal to uh 34 and let's um say int B is equal to 5 what if I say if a is greater than 1:55:18 7 and B is less than four uh let me show you how this works I'll simply say yes okay so if this happens I'll simply say yes otherwise um I don't want to say no I'll simply say yes so if a is greater than 7 is a is greater than 7 no a is not greater than 7 is B less than four no B is not less than four it's actually greater than four which is five so both these conditions are false this is false this is false false and false is false okay now let's um make things a bit interesting here let me make this two so one of these conditions is true a greater than 7 is false B less than 4 is true false and true will evaluate to false okay so a is greater than 7 is true actually uh my bad a is greater than 7 is true and B is less than 4 is also true now so B is also less than 4 2 1:56:20 is less than four and 34 is greater than 7even so both of these conditions are true now so true and true will evaluate to true but if I make one of these conditions as false it will not return Yes but if I make it two it will return Yes and uh I know that this is a bit confusing at this point so let me explain this to you in Greater detail and that's why I've written these notes for you okay so we have three operators and or and not these are the three most used logical operators and or and not not now what we'll do is um we'll simply use it in our programs these operators are read as and or and not they are used to provide logic to a c programs okay so that's why they are called logical operators and I have put them down in the notes really very well and uh let me tell you what I mean when I say that so this is the end operator we have used this end operators on two conditionals okay so these are two values which will evaluate to either true or false now now 1:57:22 what we are doing is we are operating on 1 and zero so uh let's talk about end operator it is true when both the conditions are true so if you say true and true it will be true otherwise it is going to be false if you have true and false it will be false if you have false and false definitely it would be false if you have false and true it will be false so 1 and zero is evaluated to false by the way one is true in C any non-zero value is true in C and zero is false in c z and Z is evaluated to false one and one is evaluated to true and even if you think about it from the language the English language perspective just think about it when you say I want this and this it means that you want both the things doesn't mean that you want one of these things so whenever you have one and one then only that condition will be valuated to true if I say that you are a good boy and you live in New York so you'll have to be a good boy and you will have to live in New York for this entire statement to be be true so both of this both of the um 1:58:25 operant should be true when I operate these two operant with end both of these operant should be true both of these conditions should be true you should be a good boy you should live in New York then only this entire statement is going to be true if you are a good boy and you don't live in New York no this entire statement is false uh if you if you don't live in New York if you live in chamoli and let us say then I say that you are a good boy and you live in New York York no you don't live in New York you live in chamoli so the thing is that this entire statement is going to be false so one and Z is evaluated to false 0 and 0 is EV Val to Falls one and one is EV Valu to Falls now that we have spent so much time on and operator or operator is going to be super clear to you or means Choice what would you like to have you have two options either you can have chapati or you can have uh rice you can have these two things what do you want to have so you can have either chapati or rice you can't have both of these so if going to a mess and they they tell you that uh you can either have chapati or you can have rice what 1:59:26 do you want so you cannot say that I want both of these things no they are not going to give that either they'll give you chapati or they're going to offer you rice based on your choice in a very similar way if you say true or false one of these conditions is true so it will valuate to true false or true one of these conditions is true so entire statement will be evaluated to true but if you sayal false and false that will be false okay so our operator will evaluate to True when it is operated on 1 or zero one or 1 one 0 or 1 but if we have 0 or 0 it will be valuated to false rest of the things will be valuated to true so zero or zero is false rest is true okay I hope that uh I'm able to explain this pretty well now uh let's talk about not operator and not operator is important because it will simply invert true to false and false to true so this is also called an 2:00:28 inverter this not operator is called an inverter and no I'm not talking about this inverter no this I'm not talking about this inverter no don't don't confuse it with this inverter I am talking about inverter inverter means something which can invert which can make true as false and false is true okay so this is going to return true if uh it is supplied false and if you supply true to it it will return false as simple as that I hope that uh this makes sense now if I say not off three dou equal to 3 so you'll have to evaluate this first 3al to 3 is actually true because 3 is equal to 3 and when I'm saying double equal to I mean is 3 equal to 3 or not yes it is so this is true but not of true is going to false uh is going to be false because not operator is an inverter so this entire thing will evaluated to false now if I say 3 greater than 30 and I put a not outside of it 3 is not greater than 30 so this thing is false but since 2:01:29 we have a not operator outside of it this is going to valuate to true I hope that this makes sense to all of you as the number of conditions increases the level of indentation increases this reduces readability logical operators come to rescue in such cases and let me tell you what I mean by this statement so let us say you have an you have an if statement now you have another if statement inside that if statement and then again you have another if statement inside that if statement and you keep on going so what happens is your program will start from here and you'll find your program going here because you will have if if if if if if if if and you will you know keep on indenting keep on ending until your program looks ugly so what you will do is you will want to write something like this and that will actually give a better logic to your program and we are going to use these logical operators in the practice set so in case you are not able to understand this you'll have to uh just wait for the practice set and listen to these 2:02:30 Concepts download these notes read about all these operators I know if you are a beginner it might be little bit difficult to wrap all these Concepts around your head but again I can assure you that by the end of this course you are going to have each and everything including pointers dynamic memory location crystal clear in your mind this is the guarantee so let's move ahead with this guarantee and um as the number of condition increases the level of we just talked about this let's talk about else if Clause something that we have not yet talked about else if what is this else if we talked about if we talked about else we didn't talk about elsf okay so let me show you a scenario let me create a program for you I'll call it lf. C I'll put the boiler plate code what if I say if a is greater than five um Let me let me let me say int age is equal to 45 and let me say int uh Place 2:03:30 code is equal to 12 let us say we have 12 places and a person can have an age and he can have a place code where he has got his passport okay now what I want is I want to say if a is greater than five if a is um or rather if a is greater than 18 if a is greater than 18 then what you do is you simply print sorry I should write age if age is greater than 18 you simply print uh you are eligible to drive okay you are eligible to drive else if uh AG is not greater than 18 so I'll say else if so I'll have something inside there uh and let me explain explain you how this is different from a normal if else okay so let me write it down first and then I'll explain it to you so if age is greater than 18 then you do this else if 2:04:32 AG is if age is not greater than 18 then definitely age is less than 18 so we'll check if age is greater than 15 we'll simply say you are eligible you are not eligible to drive you are not eligible to drive but we will check for a toy vehicle for you okay we can do something like this so let us say we have uh 12 places and uh inside all these 12 places we have some uh toy vehicles on rent which people with age greater than 15 can drive okay now what I can do is I can simply say else sorry you are a kid and cannot drive okay so now if I run this program so the age is 45 so what will happen is this thing will get executed first and this 2:05:33 condition will be checked for that so is age greater than 18 or not the answer is true since the answer is true this will get executed and since this is executed this will not get executed this will not get executed so what I mean to say by this is that this entire thing is one single entity this is one single entity and what does it mean it simply means that either this or this or this will be executed but if we have only if statements all those conditions will be checked so let me compare this so let me create two scenarios for you so this is scenario one and let me also create another scenario for you and uh we'll compare these two scenarios so what I'll do is I'll simply comment out scenario number one okay and let me create a scenario number two for you and inside the scenario number two what I'll do is I'll simply say if age is less than 15 2:06:35 okay less than equal to 15 maybe so what I'm doing is in scenario 2 I'm checking three different conditions so how will C compiler decode this scenario to what it will do is it it will see the value of age is 45 so the value of age is 45 now what it'll do is since the value of age is 45 what it will do is it will check whether this 45 is greater than 18 or not is it greater than 18 yes it is greater than 18 it will print you are eligible to drive but instead of stopping and skipping the other conditions what it will do is it will once again check for this condition 45 greater than 15 45 is greater than 15 yes what it will do is it will print this once again and again it will check whether 45 is less than or equal to 15 is 45 less than or equal to 15 yes it is uh no it is not I was just kidding I was 2:07:35 just checking whether you are attentive or not 45 is not less than equal to 15 okay so this is not going to get printed now you are eligible to drive will be printed you are not eligible to drive will be printed so this will this will get a bit confusing so let me show you so this this is this is printed let me add a back sln after all these statements so that you know we have some pretty output so I'll simply say back sln so I'll say back sln and um yeah I'll run this program uh so you are eligible to drive you are not eligible to drive but we'll check for a toy vehicle for you so both of these conditions are executed let me commment this add you out do not try to you know map the logic of whether you can drive or not I'm just showing you some if statements you don't have to think about why are these two things are printed this is because we use these if statement now let me show you how C uh C 2:08:37 compiler is going to run through these statements what C compiler will do is it will go and check the value of H which is 45 what it will do is it will see whether age is greater than 18 or not is it greater than 18 yes it is now that we we have else if it will ignore it else will be ignored because we have already mashed one condition so what it does is that it will check for if if if this condition is not true it will go ahead and start checking for lse if is is it met is this condition true if not it will go out if there is another LF let us say we have another LF here we can have more than one lsf inside our if else ladder this is if else if else ladder this this entire thing is called One ladder and this is one single entity and only one out of these if else if else if and else will be executed only one out of these will be executed and the moment it finds one which will get executed which actually uh meets the condition in this case age greater than 18 will meet this condition this 2:09:39 condition is true so this is executed and then this C compiler will directly jump out of this letter because we have already matched one condition one condition is true it will directly come out of this and it will start executing the statements written here so this is how plain if statements and if El letter is different I hope you got the point let me run this program and show you only you are eligible to drive is printed what if this condition was false what if I say if age is greater than 48 it will check whether age is greater than 15 or not and yes it will be greater than 15 so this will be printed and then it will stop so I'll show you uh oops I selected and ran you don't have to commit this mistake so you are not eligible to drive but we will check for a toy vehicle for use printed because this condition is matched and once the condition is matched it will forget about the entire letter and it will go out and again it will start executing the statements which are written outside this letter okay coming back to notes and this will really clear 2:10:41 all the confusion uh because I've written it and put it in the notes in a very beautiful way instead of using multiple if statements we can also use else if along with if thus forming an if else if else letter so this is a letter and only one out of these statements will be executed using IF else if else reduces indents the last else is optional also there can be any number of else if so this is something that I've made clear in the notes you can have any number of else if you can have uh only one else and you can also omit that else if you want okay so I hope that you understood all these things now I've written something very important here that last else is executed only if all the conditions fail so if all of these conditions will fail then only this else will be executed so this is something that you need to keep in your mind once again so now we'll talk about operator precedence once again uh in the previous chapter I talked about operator precedence and I thought that you know I 2:11:43 need to jot down some more operators for their priority because you might want to use this priority table and you should have some good amount of operators for your reference which is why I created this list for you and um i' I've added this here because I haven't added it in the previous chapter so I simply added it here okay so remember that we have a priority table of some more operators in chapter number three okay so uh now we'll talk about uh further Concepts which is a conditional operator so we have a short hand if else which um which is also called Turner operator okay it is written using a conditional let me show you how we use it so I'll simply say 05 Turner do c I will add a boilerplate code and I'll show you so what I'll do is I'll say int a equals to and what I can do is I'll say three if five or rather U say C equals do 2:12:45 equals to B else it is going to be seven okay now if I say print um the value of a is percentage t a and yes let me finish this program because I have not defined C I'll say C and then I'll say in B = to 8 okay so C is 6 B is 8 and we have this thing which is called tary operator so how this works is if you say in AAL to 3 question marks a condition and then colon 7 so if this condition is true then three will be assigned inside a otherwise 7 will be assigned so let me run this for you and you can see the value of a is zero so it says value of a z okay so I made a mistake here this condition will come here and this value will come here so I have written this very clearly in the notes that condition then Turner operators expression if true expression is false so let me run this 2:13:46 once again and you will see the value of a is seven why is it seven because this condition or let me you know add parenthesis so that you can easily understand that this is the condition if this condition is true then the value of a will be three otherwise it will be seven okay so this is how it works if I make it six then c equal to B will be true because c will be equal to B and if C is equal to B what is the value of a 3 let me show you the value of A3 so this is how Turner operators work this is also called shorthand IFL statement it can be written using the conditional or tary operators like this so you have a condition here question mark value if True Value if false as simple as that so I hope that you understood all these things now we are going to move towards switch case control statements what are these types of statements and why do we need these switch case statements so switch case is used when we have to make a choice between the alternatives for a 2:14:48 given variable so let us say we have few Alternatives and and uh we want to make a choice for example let us say we have uh we are saying that press one for this two for this three for this four for this five for this and we have a lot of options now you can either press one you can either press two three or four so what I would want is I would want the user to press a key and based on what keys pressed on the keyboard I would want to do something so I hope you understood the essence of switch case statements now what I'll do is I'll simply create a program and I'll name it 06 um switch. C and uh this is going to be very very easy for you because you have already looked into APL statements so what I'll do is I'll say int age is equal to say 34 now what I'll do is I'll simply write switch uh s SW T CH and the moment I do that there is a code snippet for switch and these code Snippets are provided by the cc+ uh Plus+ extension if you don't have these Snippets do not 2:15:49 worry you can type all these things on your own you don't have to use Snippets but again I'm using Snippets just to save time if I say if age switch age okay so I want to check whether age is a particular number or not if it is 24 I want to say I'll U remove this code I'll say printf U r24 okay now after this what I'll do is I'll put a semicolon for sure so let me put a semicolon here I'll write another case let me let me say 25 I'll say you are 25 I'll do the same for 26 I'll do the same for 27 and also I'll do the same for 28 so let me say 27 here let me say 26 here 25 is already there so we have case 28 and let me save this so case 24 you 24 you 25 26 7 8 and just ignore this break statement at this point because I'm 2:16:50 going to talk about this break statement in a very great detail for now just forget about this break statement okay let's run this program and see what happens so the moment I run this program nothing happens because uh because I don't have 34 as the case so let me punch in 24 here and let me run this so you'll see that U24 is being printed on the screen why is this U24 being printed on the screen let me explain you how this thing works what happens is the moment you start a switch statement it starts checking for the cases so it will take the age so it will take the value of age it's 24 it will start matching is it 24 yes it is 24 if it is 24 execute this execute this execute this execute this and execute this okay uh let me give you another example what if age is 26 if age is 26 what happens is since we are doing switch on age it will take the age variable it will one by one start comparing with this is this 26 no it's not it's 24 is this 26 no it 2:17:54 is not is this 26 yes it is 26 and since it is 26 what it will do is it will start executing all the cases from here onwards but wait what happens is the moment it goes into the switch case of 26 value so the moment we have case 26 what happens is it will print Ur 26 and it will encounter a break statement now in order to explain what this break statement will do let me comment all these break statements off so I'll comment them and um you'll see what they were doing so the moment I run this program now you'll see you are 26 you are 27 28 everything is being printed so all the cases from here onwards are being printed so this code is executed it will not even check the condition because we have already matched the condition so all the cases starting from case 26 will get executed so you are seeing case 27 you are seeing case 28 everything is being executed but I want to stop on case 26 I want to execute 2:18:55 just this code the code which is inside case 26 how do I achieve that and how do I achieve that all I need to do to achieve this is I simply write a break statement so I'll uncommon these break statements so these break statements will ensure that the moment I encounter any case further cases will not be executed only the case which is matched will be executed in this case only case 26 will be executed because what this break statement will do is it will throw the control out of the switch case statement so break simply control throws the control out of this particular statement which is uh switch case statement so we'll talk about break and another keyword which is continue in later chapter and there you will understand even more on break and continue but for now let's keep it this way okay so coming back to the notes s case is used when we have to make a choice between number of alternatives for a given variable we simply say 2:19:56 switch and we have an integer expression and we can start you know our cases and the moment a case is matched all the code below the cases executed and I have written all these things in the notes as well the value of integer expression is matched against these values if it matches any of these cases that case along with all the subsequent case and default statements are executed even the default statement is executed okay this is something that you need to keep in your mind so what happens is let us say the age is 36 okay um let me add a print F statement after default and show you I'll say no case mashed okay and the moment I run this program now because age is 36 and it is not going to match any of the cases what will happen is it will simply run the default uh statement so so none of the cases matched so it has no other option than to run the default statement so the default statement simply gets executed 2:20:58 when none of the cases match but if any one of the cases are matching what happens is all the subsequent cases will run but since we since here we are using a break statement break statement will uh throw the control out of the switch case statement okay I hope this is clear for you uh the value of integer expression is matched against this and you know we already talked about this that all the subse in case and default statements are executed a quick quiz for you write a program to find grade of a student given his marks based on below criteria so we have this criteria if uh if the marks lies between 90 to 100 you need to tell that the grade is a if if the lie between 80 to 90 you need to say the grade is B otherwise C otherwise D so make sure that you solve this problem and you can use f statements to solve this problem okay so this is going to be very simple problem for you if you have have listened to me very carefully on F statements now some important notes on F statements and switch case statements we 2:21:58 can use switch case statements even by writing cases in any order of a choice so it is not necessary that you have to uh write your cases in increasing order 24 will come before 25 no you can write cases in any order and whenever you write cases they will simply get matched with the number you are running switch on in this case that particular variable was age so whatever the value of AG is will be matched with all these cases okay so I hope that you understood this next important note is that character values are allowed as they can be easily evaluated to an integer so character values can be easily evaluated to an integer character values are nothing but integer Expressions because every character has a value associated with it and do not get confused with this statement but character values are allowed which means you can always do something like this if you want to match characters you can do something like this and that's absolutely fine um we'll see some questions later on how we can use characters inside switch case now a 2:23:00 switch can occur within another but in practice this is really done so you can have a switch inside of switch whenever you have a switch case and you have some code inside a case that is simply a c code you can either have if else statements inside that case so let me give you an example let us say we have case 27 now that you are executing this particular line can you write another switch here yes you can you can start another switch statement here but in practice this is really done you can even have AFL statements inside a case but again that is also rarely done so I hope you understood all these things and uh you were able to find out how you can actually uh use switch case statements and conditional expressions in general so we now move on to chapter number three practice where we are going to look into some questions on switch case statements and and uh then we'll start moving towards some other lectures on C let's move on to chapter 3 practice set now so here is the chapter number three practice set which I've created for all of you and let's now start coding some 2:24:02 of the problems so that we have some solid understanding of the conditional Expressions how do we uh start solving these problems I'll show you all the techniques let's start with the problem number one I'll simply name it pr01 doc I'll include the boiler plate code I'll uh you know close this terminal just to get some space and let's read the question it says what will be the output of this program so we have a is equal to 10 the value of a is 10 then we have a is equal to 11 if a is equal to 11 print F I am 11 else I am not 11 if you do not look at this question carefully what you'll end up doing is you'll say a is equal to 10 it's not equal to 11 so this is false if a is equal to 11 I am 11 but a is 10 so I am not 11 will be printed on the the screen but you will be wrong and I just want to type this program real quick and I'll show you why and I'll explain you why but let me type this program real quick and then from 2:25:03 there we'll see why the answer is wrong uh print F uh a is 11 and and once again I'll say a 10 here uh else a 10 so let me replicate it and and I'll uh change it to else and once I do else I'll say a 10 but if I run this program I'll show you what guess what it prints a 11 but a 10 why is it printing a 11 and I told you initially that the answer is wrong why why is it wrong so I'll wait for some of you to comment down below why this answer is and I want your comments guys just put the time stamp and drop a comment and I want you to tell me why this answer or rather why this program is behaving in this manner all right I hope few people commented and if you told me that uh AAL to 11 in 2:26:05 AAL to 11 equals to is an assignment operator and this entire thing will return a non-zero value you were right this entire thing is going to return a non-zero value when you assign something to something then this is not a Boolean expression this this is a non-zero value and since this is a non-zero value this is true in the eyes of C programming or rather in the eyes of C compiler so this is the reason why this is printed so I hope you understood this problem number one let's now move towards problem number two write a program to find out whether a student is pass or fail if it requires total 40% and at least 30% in each subject to pass so at least 33% is required and a total of 40% is required in each subject to pass assume three subjects and take marks as an input from the user so you have to take marks as an input from the user okay so let's write this program and what we'll do is simply 2:27:07 write a file we'll say 08 which is the eth program of this chapter underscore PR 02 Doc and why am I wasting writing these programs in such an organized way just so that you can come back to these programs and start revising while you are watching the video I can always name these programs in some gibberish manner uh you know but I don't want to do that because when you are watching this course you might want to come back to the programs that I'm writing and this pattern or rather this structure of naming the program is going to be very useful for you okay coming back to the question I have to take marks as an input from the user so what I'll do is I'll say int marks and I'll say Marx equals to scanf what is it it's it's an integer so percentage D and M percent marks okay and I don't have to do something like this just plain and simple scanf 2:28:08 function will take care of everything so we have this scanf which will take marks as an input from the user and after the marks are taken as an input from the user marks will be stored inside this integer variable because I have mentioned percentage D here okay I hope it's clear so far so we have a Marx variable which is an integer now I have to check whether a student is pass of or not so it requires total 40% and at least 33% in each subject so we have to assume three subjects so what I'll do is I'll simply say Marks One marks 2 and marks 3 so these are three subjects you can even say physics chemistry mathematics or or uh you can say Commerce economics accounts just assume your subjects guys um and do the same thing so just to facilitate the input taking from the user what I'll do is I'll say print F enter Marks One okay 2:29:09 enter your marks in the first subject and I'm going to use back sln just to you know uh just to make sure that uh these things are very nicely organized and I'll repeat it for Marx 2 I'll use alt and click to use multiple cursors of vs code uh I'll simply use alt and click here again so when I hold alt and start clicking I keep on getting the cursors and this is how you use multic cursor functionality of vs code so this is a great feature in vs code and I love this feature to be honest now if I run this program and uh I have a habit of checking my program uh add different Milestones just to be sure that I'm writing correct code so 34 34 54 the program works nice so far let's move ahead and start writing the logic so what I'll do is I'll simply say in total is equal to so the total marks will be Marx 1 plus Marx 2 plus Marx 3 2:30:11 just see how I'm writing M3 and I'm getting marks three so it's picking M from here and three from here even if I say mkss it will pick up all the variables which contain this MK and S and if I if I type three then this is the only variable which has MKS s and three so you can use this type of technique to you know write your code faster so we have this total so assume three subjects and take marks as an input from the user but here's a catch we need at least 33% in each subject and total of 40% so let's check the total and since this is percentage I'm going to use float why am I using float because I'm going to divide this by three I'm I'm going to take average of these three marks and if I use integer I might lose Precision I don't want that okay uh oops I need to write three because these are three values and also what I'll do is I'll check this I'll sa if total is greater than or equal to 40 you are pass okay but if it's not greater than 2:31:13 equal to 40 what I'll do is I'll say print F sorry you are are not passed because your marks are less than 40% okay less than 40% okay so if your total is greater than or equal to 40% I need to check your individual marks so what I'll do is if Max 1 is less than 33 or or so I'll use this pipe Max 2 is less than 33 or I'll once again use the pipe so what I'll do is is I'll say Marx 3 is less than 33 if this happens what I'll do is I'll simply say print F you are not pass you are not pass because uh your marks in one of these subjects uh is less than 33 Okay so this is something that you can write and yes 2:32:14 that's fine okay now what I'll do is if this is not the case I'll simply say X else if this is not the case simply say congratulations you are you have passed the exam okay so once I do this I can run this program once again and let me you know award myself 23 marks in one of the subjects and 55 in the other 88 in the other and I have not passed because my marks in one of the subjects is less than 33 okay uh so this is how this program works I once again use arrow keys and press enter and uh in fact I use the up Arrow key and you can do that you know click on the terminal use up Arrow key press enter and your program will fire up once again okay so max one is 67 Marx 2 45 and let me say 35 so yes I have passed the exam because my marks in all three 2:33:15 subjects are greater than 33 and the total aggregate is also greater than 40 okay now let's run it once again so what I'll do is I'll simply award myself 34 marks 34 34 and 34 so what this will do is this will uh end up awarding me 34 marks in total which is 34% in total and since this is less than 40 I'm going to fail okay so you are not passed because your marks are less than 40% okay did you notice one thing that we typed this percent and we were not able to see this percent inside the uh console why was that uh so what I'll do is I'll type double percentage and when you say double percentage it is actually printed as a single percentage let me show you I'll save this program this program and I'll uh simply run this program once again now what I'll do is I'll simply award myself 35 marks in all these three subjects and you can see that this percentage has been printed so when you say percentage it will assume that you are 2:34:17 going to type something like percentage D percentage f F or percentage U LD or something but but if you type percentage percentage printf will get the signal that you are going to type or you want percent literally to be displayed here like this so if you want to have percentage displayed literally on the screen simply type percentage percentage and um that will solve the problem okay that will do the trick for you so we solved question number two we solved question number one let's move on to question number three now and it says calculate income tax paid by an employee to the government as for the slps mentioned below so income slab rates are these and tax is this note that there is no tax below 2.5 lakhs take income amount as an input from the user simply take the income amount from the uh user and you can write some logic to calculate the text so let's let's solve this problem this is an interesting 2:35:17 problem so what we will do is simply create a new file 09 uncore pr03 Doc and we'll simply add some boilerplate code we'll simply say and you know what I'm going to do I'm going to copy the print and scanf from here and I'm going to create a variable tax and income okay so let's assume that the income is um an integer and uh we'll see if you want to make tax as float so let's make tax as float and let's keep on adding to text if the income is um you know greater than 2.5 so if the income is please enter um enter your income okay so so the user will enter the income if income 2:36:18 is less than 2.5 lakhs or rather 25 this is 2.5 lakhs if this is the case your income is less than or equal to or let's keep it less than uh 2.5 lakhs so no TX for you okay so uh since the tax from 2.5 lakhs to 5 lakhs is 5% what we'll do is uh we'll simply add zero to the tax if this is the case now what we'll do is if the income lies between 2.5 and five what we'll do is We'll add that particular amount of tax to the user's income okay so what I'll do is I'll now type LF and inside the if what I'll write is I'll say if income is less than 7.5 lakh 2:37:20 and it's greater than income is greater than 2.5 lakhs now what will happen if I do this so I hope I have put down the zeros correctly yes I have 4 + 1 5 4 + 1 5 yes just make sure that you are typing the number of zeros correctly okay so what I'll do now is I will simply say your income is your income is between 2.5 lakhs and 7.5 lakhs so that's it we are not going to say no text for you now what we'll do is we'll see uh or rather it was five laks my bad so I'll have to say five here five LHS and also five ha here okay so if this is the case we'll do 2:38:21 something okay now if this is the case where the user income is uh greater than five and it is less than uh say 10 lakhs okay so if it's less than 10 lakhs uh 3 + 1 4 + 1 5 3 + 1 4 + 1 5 so if it lies between 5 lakhs and 10 lakhs what we'll do is we'll charge 20% of the Tex okay on this income otherwise what we'll do is if the income is greater than 10 lakhs what we'll do is we'll charge the base amount and then we'll add 30% of tax to the remaining of the income okay so if this sounds confusing simply take a piece of pen and paper and start writing whatever I'm seeing start calculating use a calculator and things will start making sense to you so I'll simply say your income is above an LX here now let's add to the text so when 2:39:22 the income is between 5 lakhs plus 2.5 lakhs what we'll do is we'll simply add something to the tax so we'll say tax equals to Tax Plus We'll add a value to the Tex and this is how you add value to a variable so let us say the text was zero and I add five to it so I'll simply say text is equal to Tex + 5 and text will now become five because I have added five to the text but how much will I add to the text I'll simply add um 20% or rather 5% of the Tex so since the income is between 2.5 and 5 I'll simply add 5% of the tax so what I'll do is I'll simply say 0.05 multiply by uh what is what is the income income between 5 lakhs and 2.5 lakhs I'll simply say income minus 2.5 lakhs why did I do that because I want to charge tax only on the income which is above 2.5 lakhs so if you are earning 3 lakhs I'll charge income tax on 50,000 because till 2.5 2:40:23 lakhs there is no tax and after 2.5 lakhs we have 5% of tax okay so this is how we calculate tax now if the income is between 5 lakhs and 10 lakhs what will happen is I'll simply use um how much 10% um yes no 20% so I'll simply say 0.02 this time so I'll say 0 uh 0.2 which is 20% and I'll multiply this by income minus 5 lakhs so whatever you are earning above 5 lakhs I'm going to charge your income tax on that okay but I also need to charge you income tax on um you know your income between 2.5 and 5 lakhs so I'll add another taex for you uh let me let me do it this way so I'll I'll charge this much as well so on your income between 5 and 2.5 I'm going to 2:41:24 charge you 0.05 okay so between 2.5 and 5 lakhs whatever you are earning I'm going to charge 5% and between 5 lakhs to whatever you are earning because your income is between 5 lakh to 10 lakh I'm going to charge 20% on that and when when you are earning above 10 lakhs what I'll do is I'll simply I'll simply say 0 um 05 on this 0.2 on this and 0.3 on the remaining of the income between 10 lakhs and above uh and what I'll need to do is between your income between 10 lakhs uh sorry between 5 lakhs and 10 lakhs I need to charge 20% so this is going to be the total TX let's see what is the total TX so I'll simply say total tax to be paid is percentage 2:42:25 F and I'll simply say Tex and I'll press uh I'll simply press the semic column enter your income 10 lakhs rather 11 lakhs if I enter 11 lakhs you need to pay this much in Tex so three uh to five so you need to pay 1 lak 142,000 in taxes okay now if your income is 23,000 rupes you need to pay zero tax okay now if your income is 250,000 you need to pay some tax your income is above okay so I actually used an additional zero 3 + 2 5 I actually entered 25 lakhs I should have entered one zero less but again if your income is 25 lakhs this is the amount of tax you need to pay you need to pay around 6 lakhs in taxes okay so if I enter my income as 2 lakh 4,000 this is my income tax and if my income tax is 251,000 you can see that the tax 2:43:27 to be paid is 50 rupees which is 5% of 100 rupees uh sorry not 100 rupees 1,000 rupees 5% of 1,000 rupees is 50 rupees yes that's right okay so I hope you understood and I hope that this problem solved a lot of your existing doubts on variables on data types on conditional expression and if this program sounds haunting to you watch the video once again uh you know just skip to uh where we started this question and start practicing this program yourself okay and things will start making sense to you so this is how you calculate income text once again do not take this as you know the accurate information this is assumed information I'm giving you a disclaimer Google for the tax rates in your country and you know do not use this to calculate your tax I know nobody will do that but again it's my duty to give the disclaimer now write a program to find whether a year entered by the user is a leap year or not take year as an input from the user 2:44:27 so in order to find out whether a given year is Leap Year or not you simply go to Google and simply say what is a leap year okay and you'll get the conditions a year occurring once every four years which has 366 days including 29th February as an inter calerie day so if you go into detail and if you go to Wikipedia of leaper there are some of the uh you know they have in fact written the algorithm okay so I'm going I'm not going to you know look into this algorithm rather I want you to build the algorithm yourself okay and you can always look into the algorithm and if you type that you are going to write the program accurately but what I want you to do is just think about what a leapia is and then once you have an idea of what Lear is then start coding and then you'll feel very confident after you write the program having said that what I'll do is I'll simply copy this algorithm uh I just told you that you need to understand the algorithm understand the process behind the 2:45:28 algorithm and then start writing the logic but just for the sake of saving some time what I'll do is I'll simply copy this logic and I'll start coding problem number four which is uh program number 10 pr04 Doc and when you do something like this and you you know paste it you're going to get a lot of Errors we need to fix these errors it says ear is not divisible by four how do you say this in C you simply say year percent 4 is not equals to zero is not equals to Z you can always say this and let me say in year is equal to 3444 and I'll simply print F and say it is a common year and uh you know in place of then what I'll do is I'll add these parenthesis okay and I'll simply say print f and I'll add double quates I'll select this add the double quat and and I'll do the same for these as well so let me do 2:46:28 one thing let me start using multic cursor functionality of vs code I'll simply say print F and I'll simply shift this I'll add this I'll press end on the keyboard I'll use arrow keys once again I'll close this I'll add the semicolon I'll uh close it like this okay now I'll do the same thing for this I'll simply say if year per 100 is not equals to zero if year is not divisible by 100 okay here is not divisible by 100 and if here is not divisible by 400 I'll do the same thing divisible by 400 okay and once again what I'll do is else it is a leap here so I'll simply copy this thing again and it is a leap here semicolon curly braces closed and you don't have to you know do like this you can keep it this 2:47:29 way and this program will work just fine but again it's a good practice to make your program look good with the indents let me add these indents to the program print F uh Missing closing code am I making okay I'm making this mistake I'm not adding the closing code uh I think everything is fine now and let me cross check the logic the logic says that if here is not divisible by 4 not divisible by 100 not divisible by 400 then finally it will be leap here okay so if it is not divisible by 100 and it is divisible by 4 it will be a leap here so this is a logic so how this works let me explain it to you and what I'll do is I'll uh add another endend just you know so that this looks good you can always say format document it will format it in a good way for you so how is this logic working so we have divisible uh sorry not divisible not divisible not divisible and uh let me call it not 2:48:31 equals to zero okay yeah cool so it is a common year I've written the logic correctly if the year is 3444 it will check whether it is divisible by four or not if not it is a common year it has to be divisible by four to be a leap here but there are some other conditions which should be met for this year to be a leap year so what we'll do is we'll check for all those conditions we'll say is it divisible by 100 or not if it is not divisible by 100 yes it is a leap year is it divisible by 400 or not if it is not divisible by 400 then it is a common year but if none of these conditions are actually getting true then it is a leap here okay so what happens is if it it is divisible by four which means this is false this is false this is false then finally it will be a leap here okay just think about it and start relating your program with the actual logic of things so you should 2:49:31 have the logic of leapia ready and then once you have the logic of leapia ready you write the program and then it will start making sense to you so one of these if else if else if else will be executed just look at this program in this way and it will be super simple moving on towards the next problem and the next problem is really very simple and straightforward it says write a program to determin whether a character entered by the user is lowercase or not so let me tell you something whenever you write a character in C language uh let me show you something I'll simply type character s key value in C okay um what I'll do is I'll simply use this s key table and what this Sky table is let's try to understand this first and then we'll solve the question so Sky table is nothing but some values which are mapped to the characters so let me show you something okay and once you have uh you know a good understanding of that then you'll be able to solve this question so let me create 11th program 2:50:32 and it's pr0 5.c if I'm not wrong it's problem number five if I'm not wrong yes so let me show you something let me put this boiler plate code and let me show you something what if I say uh something like this I'll I'll say the value is I'll say percentage D and I'll simply say a character a which is a now listen to me very carefully if I run this program this is not an integer by the way let me tell you this is a character but if I hover over it you'll see that it is an INT with a value 97 let me run this program for you and show you that it indeed prints 97 so a character is stored as an integer in C in the form of SK Sky value and if I say a percentage C then I'll be able to see a but if I do percentage D then the S key value of this particular thing is 97 which is being printed so whenever you are writing this literal whenever you write this character 2:51:33 literally what it means is it means that it is an integer with the SK value so you can perform addition subtraction multiplication and division with all these values okay let me show you how so uh now we have a program to determine whether a character uh entered by the user is lower case or not as the question so this is the question we have to solve this question so let's look at the SK table and let's see where ABCD is you know uh being started from it's it's being started from 65 and it's going all the way till 90 or rather 90 okay not even 96 so if a character is lowercase it has to lie between 97 to 122 so that's all that's the check I'll say enter your character so let me say car c I'll say print F enter your character and and definitely back slash in okay and once I do that I'll use scanf and I'll say percentage C note that it's not percentage D you need 2:52:34 percentage C and am percent of C where C is the character let me change it to car so that you don't um character let me change it to character so that you have a good idea of which one is variable and which one is format specifier so we have this as uh a print statement and then we are taking characteriz the input from the user and the moment we do that what will happen is we are going to check whether this particular character is greater than or equal to what is greater than equal to 97 and also we need to check if this character is less than or equal to what 128 if I'm not wrong 122 yes I'm wrong 22 you don't have to remember this you might think that I don't even remember SK SK table I don't have to remember SK SK table and and you also don't even have to remember SK table Some people prefer to remember and even some schools and colleges they say that you know remember the U ask value of 65 and the 2:53:36 value 90 and you know you add 32 and you get this particular value and this particular value respectively so if you add 32 um you know to 65 you'll get this so whenever you have to convert a character from from uppercase to lower casee you simply add 32 to the value and you know you get the character which is lower uh lowercase version of the uppercase character so you can do all those things but I don't I don't think that you have to remember anything uh if you remember 65 well and good for you you can um easily come to 90 if you have 65 and also you can easily come to um this thing uh 98 and you can easily uh sorry 97 and you can easily uh come to 122 by counting from a to zed so if you remember 65 and 32 just remember these two numbers if you add 32 to 65 you'll get this and if you add uh you know if you start counting from a b c d all the way till Zed you can come here and similarly you can come uh to this value if you start counting from 97 okay but 2:54:37 coming back to the question we have to check whether a character is greater than equal to 97 and less than equal to 122 I'll say print F yes this is a lower case character and otherwise what what will I do I'll say I'll remove this I'll say no this is not a lowercase character so let me do something like this I'll say no this is not a lowercase character and let me run this for you enter your character five uh no this is not a lowercase character capital A no this is not a lowercase character small a yes this is a lowercase character small D yes it's a lowercase character capital D no this is not a lower case character V lowercase character capital V not a lowercase character and so on you can you know I can keep on going for 3 4 hours but I'll not uh coming back to the question coming back to the practice set question number six write a program to find greatest of four numbers entered by the user and I think this is the easiest 2:55:38 problem of the practice it because if you have a little bit of um you know logical mind you can definitely do this this is problem number 6 pr06 doc at the boiler plate code so let me tell you how you can solve this problem how you can find greatest of four numbers entered by the user let us say you have four numbers let us say you have four Fighters you you have four of your friends and they say that I am the strongest person in the room so what you do is you make two people fight and then you make two people fight again they both will fight and the winner of this two and the winner of this two will fight with each other and they will find out who is the strong longest of all so we'll do the similar thing here we'll take four numbers so let me say in A1 is equal to 1 and let me do the same thing for A2 A3 A4 and A4 and let me you know assign some random values to these 2:56:38 um variables now what I'll do is I'll say if A1 is greater than A2 okay and uh if A1 is greater than A2 and A1 is greater than A3 and also A1 is greater than A4 who will be the winner A1 will be the winner I'll say A1 is the greatest okay and you can do the same thing for all the values so let me show you how you can do how you can have you know if else if LF lsf okay now if A1 is greater than A2 and A1 is greater than A3 then A1 is the winner and if I do the same thing with A2 A2 A2 A2 and we need to compare A2 with A1 A3 and A4 similarly we need to compare A3 with A1 A2 and a uh A3 with 2:57:39 A2 A1 and A4 and then A4 with A4 A4 and here so I need to make it A3 A3 here I checking if A1 is the winner here I'm checking if A2 is the winner here I'm checking if A3 is the winner and here I'm checking if A4 is the winner which is why I have A4 A4 A4 but when I'm checking A4 I need to ensure that I'm making a A4 fight with A1 A3 and A2 okay let's run this program A4 is the greatest what if I make this as 4421 then A3 is the greatest A3 is the greatest what if I make it this and I make it this then A2 is the greatest yes A2 is the greatest what if I make it 3331 then A1 is the greatest yes A1 is the greatest so this is how you can actually find out which which of the numbers is greatest of the four numbers okay I hope you were able to solve the questions in this practice set 2:58:41 also I hope that you understood all the concepts which were taught in this chapter now we'll move on to the next chapter where we discuss some more stuff on C programming language let's move on to chapter 4 now let's now look into chapter number four and see what it has to offer so what I'll do is I'll simply open this Loop control instruction chapter in my PDF viewer AKA Google Chrome okay so what I'll do is I'll tell you what Loops are we'll try to understand Loops using some basic analogies and then from there we'll start writing the code so why do we need Loops what if I tell you you have to write a program where you have to print numbers from 1 to 100 uh maybe you might be able to type uh print f 1 print f 2 3 4 5 6 all the way to 100 there is a chance you can do that what if I make it 1 to th000 it is going to be a bit difficult for you but again you can do it what if I make it one to 1,000 uh or 2:59:41 rather 10,000 what if I make it 1 to 50,000 okay here's the thing what if I make it from 1 to 1 billion it will be very very dous for you to do that and you might not be able to do that so in that case what we do is when we have something uh which we want to execute repeatedly in a very fashioned way we use loops and how do we use Loops is something that we are going to learn in this chapter so sometimes we want our programs to execute few set of instructions over and over again for example printing from 1 to 100 uh first 100 even numbers Etc hence Loops make it easy for a programmer to tell computer that a given set of instructions must be executed repeatedly so let us say you want to print a number you want to print a number 10 times you can simply say print one print one print one print one print one all the way till 10 times but if I ask you to do it 100 times it will be TDS for you but using Loops you can do it in fewer lines let's see how so what I'll do is I'll simply close this particular thing from my vs code and 3:00:43 I'll open this folder in vs code let me open this folder in vs code and um the moment do that what I'll do is simply create a new file and I'll simply say 01 uncore loop. C let me put the boiler plate code and let's learn about what Loops are primarily there are three types of Loops in C language we have a while loop we have a do wi Loop and we have a for Loop let me tell you something from my experience we are going to use while loops and for Loops a lot more than we are going to use do wi Loops now this is this is just something I'm telling you from my experience but again there might be scenarios where you'll use two wi Loops more than violent for Loops but in general and um I can say this for programming in general we use a lot of for Loops then we use fewer v loops and then again we very rarely use two wi Loops again this is going to change that depends upon what you are doing but in most of the scenarios for Loop is the mostly used 3:01:44 while loop is second mostly used and do while loop is something that you you're going to use sometimes okay now let's look into these one by one let's look into while loop first what is a while loop and how synex of a v Loop looks like so what we usually do is we create a variable we simply say int I is equal to Z and we start counting from zero because of arrays in arrays the index starts from zero if you don't know what arrays are you don't have to worry because I'm going to talk about arrays in the later chapters but for now keep in mind that whenever you are doing programming start counting from zero instead of counting from one because it is going to make things easier for you so let's make n i is equal to Z and let me write V and the moment I do that you can see that I got a snippet what I'll do is I'll say if I is less than 34 I'll press Tab Key on the keyboard and I'll simply say print F the value of I is percentage D and let me also write a 3:02:44 back sln here so that I can see a new line now this is something that you can type as well I mean I I didn't do any magic you can very easily type this thing and typing is going to be a lot more easier if you are U not having Snippets or some other extension if you are coding in some uh source codor which doesn't support these kind of Snippets you can simply type this out and it is really very easy to type this thing out than typing the value of I is 0 1 2 3 4 34 times let's see what this program returns so the moment I run this program you'll see something like this so what happens is it keeps on printing keeps on Printing and I'm in trouble let me tell you why why am I in trouble so I I I had to cancel this and it went on infinitely uh so what I forgot was I forgot to increment the value of I so there are two ways you can use to increment the value of I either you can do something like I ++ or you can do something like I + = to 1 you can also say I is equal to 3:03:46 I + 1 and and this is going to have almost the same effect okay so you can use either of these basically what I'm doing is I'm simply running this while loop and inside the while loop I'm updating the variable so there will come a time when the value of I will simply be nothing but more than 34 or it will become 34 at some point and whenever that happens this condition will uh turn out to be false I also forgot to write I here so now we have a program which will definitely work and it will start printing from 0 to 33 so you can see the value of I is 0 2 1 2 3 4 all the way till 33 how does it work let's now try to understand how this syntax is actually giving the result that I am getting so what happens is where as in I is equal to 0 I is initialized to an initial value of zero now when I say while I is less than 34 I'm saying this is a block and you have to keep on executing this block until this is true 3:04:49 and inside this block something can happen which can make this condition return false so something can happen inside this block which will make this condition false eventually and if that doesn't happen this is going to be an infinite while loop we don't want that okay in ideal scenarios we don't want that now when I say while I is less than 34 this is a condition and as long as this condition is true this block will will keep on repeating it will keep on repeating so what happens is is I less than 34 yes it is because 0 is less than 34 and the moment I see that this particular expression is evaluated to true what happens is these two lines of instructions are executed so this will get printed the value of I is z so the value of i0 is printed and then the second piece of instruction says that you have to increment the value of I by 1 so if I is Z it will become one if I is 1 it will become two if I is two it 3:05:50 will become three and if I is three it is going to become four and so on okay so this is how entire thing is going to work print fs and I ++ and eventually the value of I is going to be 33 and when it is 33 this condition is going to be true again so the value of I is 33 will be printed but during this iteration something special will happen the value of I is going to be 34 and this condition no longer evaluates to true now which is why this y Loop will cease to execute that's it for this while loop that's it because the condition is now evaluating to false as long as the condition is evaluated to true the Y Loop was running but the moment we see that the condition is being evaluated to false this Loop will no longer continue to execute okay which is why this PR will end because we have a return zero here and hence the result hence we are 3:06:51 getting this the value of I is 0 1 2 3 4 5 6 eventually I becomes 34 which makes this y Loop to stop because now the value of I is 34 and the condition inside this y Loop is going to be false okay so I hope that you understood all these things and now what I'm going to do is I'm going to move ahead and show you the syntax which have written in the notes and why do we have these notes at the first place because I want you to come come back to these notes after 6 months and you're going to have everything back in your mind because you have refreshed all the concepts which you were um already aware of now you have these notes and you can simply come back to these notes and start revising whatever topics you like okay so make sure that you download these notes and make sure that you have a printed copy of this notes and no I don't have any tie apps from the zerox shop in your country okay you can go to your nearest Xerox shop and you know get these documents printed and it is going to be really very useful but if you are a 3:07:51 Kindle guy or you are a an iPad guy or you know some tab guy you might even use the PDF okay let's see what I've written in the notes I've written the basics index which is while and the condition and then you can uh you have multiple lines of codes inside the block and all these lines will keep on executing until this condition is true so the block keeps executing as long as the condition is true eventually the condition should become calls for this while loop to Halt or else you should have a continue or break statement inside your V we will see how continue and break works but again an example of while loop has been written here the value of I is I've shown you this example and this is the same example that I've shown you so you can play around with this code and you know start changing the number start changing the initial value of I start changing the condition you can make this 10 instead of 34 you have this code I'm going to zip all this and I'm going to send it to you so you don't have to worry about that I've written a note 3:08:51 here if the condition never becomes false the V Loop keeps getting executed such a loop is known as an infinite Loop so I haven't shown you the infinite Loop yet if I don't have this condition the value of I is going to be zero always and this thing is going to be printed on the console always let me show you so if I run this program the value of I is zero will keep on printing will keep on printing until I press contrl C to hold this program so I'll have to manually hold this program or hold vs code or this terminal to stop this program okay I can either press contrl C I can either click here or I can even close the vs code on my computer Al together which will um you know stop this program but again um we want our program to stop eventually because we want to accomplish something from our program in ideal scenarios there are some cases where we use intentional infinite while Loops what are those scenarios let me tell you um about few scenarios there are few scenarios where we want our program to 3:09:53 keep running and you know keep checking something in regular intervals so if you want that you can have a while one and inside that you can keep on checking something for some time you know you can wait for half an hour and check something then again wait for half an hour and check something one of the example is whether a stock price has hit a value or not let us say you are tracking the index of your stock market and you you want to see if your index is down by 10% or not so you might want to keep checking that every half an hour so you can write a v one Loop which keeps on checking for the time and the moment time becomes a multiple of rather the minutes becomes the multiple of 30 so every half an hour you are checking whether your index has hit a particular value or not but in most of the cases we want to avoid infinite loops and uh in most of the cases for beginners in finite Loops are a result of mistake in the coding logic okay we don't want that let us come to the quick quiz which I 3:10:55 written for you it says write a program to print natural numbers from 10 to 20 when initial Loop counter is initialized to zero now the initial Loop counter is initialized to zero which means I will have to keep this I is equal to zero if that was not the case I would have simply change this value as 10 and this value as 21 and that should have done the trick but no the initial value has to be zero how do I solve this problem now so let me write another piece of code for you I'll simply say quick uncore quiz do c okay so let me paste this and also let me keep this 34 I'm going to play around with this so the value of I is zero and what I'll do is I'll keep on going till 20 from 0 to 20 but I'll have if statement here and 3:11:55 inside the if statement I'll simply say if I is greater than or equal to 10 then only you are doing that then only you are printing this otherwise you are not printing this okay so if I do that for I is equal to Z this line will not be executed for I is equal to 1 this line will not be executed for 2 3 4 5 6 7 8 9 this will not be executed in the moment I have the value of i as 10 this will be executed so I'll get the value of I is 10 all the way till 20 let me show you so run this program you can see the value of I is 10 11 12 13 14 15 all the way till 20 so this is how this uh while loop works okay coming back to the notes uh we have solved the quick quiz the loop counter need not be in it can be a float as well so this is something that you need to keep in your mind Loop C cter can be a float it can be a double it can be anything okay you you don't have to keep it as an integer but in most of the cases we keep it as an 3:12:55 integer so that we can count uh conveniently okay now let's talk about increment and decrement operators what are these operators and why do we need to use these and this is something which is asked a lot in C programming interviews and even if you are uh studying C programming language at school or in your college this is going to be asked for sure okay I ++ what does it mean it simply means that increase the value of I by 1 so if I was 4 make it five if I was 9 make it 10 if I was 22 make it 23 iusus yes you guessed it right IUS minus means that decrease the value of I by 1 so if I was 54 make it 53 if it was 128 make it 127 and so on but we have another decrement operator which we use like this minus- I instead of doing i-- we do minus- I and how does it work it 3:13:56 basically decrements the value of I first and then it prints the value of I when kept inside print F so let me explain this to you how this works uh so in order to explain this let me come to my vs code I'll write a program 3 and let me call it increment do c I'm going to say boiler plate codes so that I can put the C language boiler plate code here I'll simply say print F and then I'll say i++ okay let me show you what it prints if the initial value of I is say 9 okay if the value of I is 9 and if I do I ++ what I'm going to get is I'm going to get uh oops I need to um I made a mistake I need to say percentage d i ++ instead of literally printing I ++ my bad let me run it again and you'll see that the value of I is 9 so even if I do I ++ why am I getting nine why why don't 3:14:56 I get 10 let me try this once again let me try to print the value of I after I do this I ++ the value is 910 what is going on why is it 91 0 it's in fact not 91 0 um I'm not using back sln let me use back sln then you going to understand what is happening so this line prints n this line prints 10 so how does this work how does this increment operator works if I have a value of I which is 9 and I say I ++ and I have a print F wrapped around it what will happen is the value will be printed first and then it will get incremented so nine is printed but post printing of nine what happens is that the value of I now becomes 10 because we have I ++ here so what happens is after this is executed the value of I is 10 so if I say I print f i I'm going to get 10 so I 3:15:57 ++ means first print the value of I what was the value of I 9 so first print 9 and then increment the value of I and what does Plus+ I do let me change this to Plus+ I now Plus+ I means first increment the value Val of I and after you have done incrementing the value of I you print the value of I so what will get printed 10 will get printed and what will get printed for this statement 10 will get printed because the value of I is nine we first incremented it and then we printed it so this will print 10 as the value of I because the value of I is 10 because I have incremented it from 9 to 10 so first incrementation takes place which makes the value of is 10 then printing takes place is so this is how it works so let's see if it prints 10 and 10 oh yes it does prints 10 and 10 so this prints 10 because I'm first incrementing the value of I and then I'm 3:16:57 printing the value of I which is 10 and this also prints 10 because I'm printing the value of I which is 10 I hope you understood how this works so coming back to the notes increment and decrement operators I ++ means I is increased by one i-- means I is decreased by one when I do something like print f minus- i uh and then percentage D and if I try to print minus- I what will be printed first I will be decremented then I will be printed now first uh this first decrements I and then prints it so minus- I means first decrement the value of I then print it iusus means first print the value of I and then decrement it I hope you understood this try to run as many programs as possible to understand this concept initialize I with a value try to print i++ try to print Plus+ I this will not cost you anything you have your laptop just write the program Open VSS code and start writing code now this first decrements I and then prints it 3:18:00 and this prints I and then it decrements it so I've written this very clearly triple plus operator does not exist make sure that you are not using um i++ plus Plus+ plus and you expect it to increment I four five six times no that doesn't happen we just have this Plus+ operator now there is something called plus equals to as well is it's called compound assignment Operator just like we have plus equals to we also have minus equals to times equals to divide equals to and um percent equal to okay and this is also important you need to practice these operators as well okay so these were some of the increment and decrement operators uh you need to understand uh now we'll talk about the dowi loop and we'll write some program doy Loop is very very similar to while loop so you going to understand it very very easily so let's now talk about do while Loops the syntax of do while loop looks like this so we have a do we have some code then we have while then we have some condition but how does it all work let me tell you so what I'll do is 3:19:01 I'll show you a code snippet of dowi Loop and then I'll explain you how all these things are working together so what we do is we write a do we start a parenthesis and we start writing the code and at the end we write a V and then a condition inside that so do while loop works very similar to while loop while checks the condition and then executes the code do while loop first executes the code and then checks the condition let's see how these two kinds of Loops are different from each other so what I'll do is I'll simply start uh writing a do while loop so I'll simply say do while. c and the moment I do that and I add a boiler plate code firstly what I'll do is I'll show you a v Loop and what I'll do is and then I'll convert the same V to do V and show you how these Loops are different from each other so if I have a v Loop and if I have in I is equal to 7 and if I do something like this I'll say the value of I is percentage D and I now here 3:20:06 clearly I is not less than five so this Loop is never going to execute so whatever I write here this is never going to execute because this condition is never going to be true now if I run this program and uh I think I'm making some mistake actually I selected the code and ran it you don't have to select the code and run it you simply run it and it is going to work so this Loop didn't get executed at all why was that the case that was the case because I is not less than five I is 7 and 7 is greater than five but what if we have a dowi loop let me convert this to a dowi loop syntax so what I will do is in dowi Loop we have a block first and while is at the end so I'll have to bring this while at the end and I'll have to write do here so this is the syntax of a do while loop and also we need to keep a semicolon here so this is very very important in case of dowi Loops whenever you are converting a while loop to a dowi loop make sure you add a semicolon here okay now what I'll do is save it and run it and to your surprise you'll 3:21:08 see the value of is7 being printed on the console you might think that why is this the case when the condition is not at all true so if you look at the structure of dowi loop uh you can you know kind of I'm giving you an analogy to understand how dowi Loop works the control will come here it will start executing this it will not see the condition yet it will run this Loop once for sure no matter what the condition is it might be true it might be false but the moment it checks for the condition it will rerun the loop only if the condition is true and if the condition is not true it's not going to rerun anything it's not going to do anything okay so this is how uh a do while loop works and uh let me come back to the notes and show you how this thing works so the syntax of do while Loops look like this do code code while and then condition do while loop works very similar to while loop while loop checks the condition and then executes the code do while executes the code and then checks the conditions so dowi Loop is a v Loop which is executed at least once 3:22:09 okay so this is something that you need to remember whenever you have a dowi loop it has to be executed at at least once by the compiler okay so this no matter what will be executed at least once will it be executed again depends upon the condition as long as the condition is true it will be executed so you can look at dowi Loops as a while loop which is executed at least once for sure will it be executed for the second time maybe maybe not depends upon the condition uh I'll show you something so if I run this program you'll see that the value of y 7 is printed and this Loop never repeats itself again now what if I make it one so that the condition is true and also I'll keep on incrementing I inside the loop what this will do is this will actually make the value of i as two in the second iteration three in the third iteration and so on so for the first time this F Loop executes for sure and the value of 3:23:11 I will become two so the value of I will be two and it will keep on incrementing and the D y Loop will keep on executing let me show you if I run this code the value of I is 1 the value of I is 2 3 4 and the moment the value becomes five you'll have to Halt the V Loop because the condition is no longer true so the doy Loop will stop because the condition will be false and it will jump out of this block and start executing the instructions here okay so this is how a do while loop works I hope you got the hang of how this thing is is working and here's a quick quiz for you write a program to print first and natural numbers using doy Loop so if the input is four what you have to do is you have to uh write a program such that 1 2 3 4 is printed like this can you solve this can you write a program for me I want you to write this program give me the time stamp of the video wherever you are writing the program and write a comment below that way you will practice I want 3:24:12 you to comment and I want you to accept the challenge right challenge accepted in the comments at least and just solve this problem I mean if you solve this problem then only you'll be able to understand the concepts but if you keep on watching the video I don't think that it is sufficient to understand and get hang off do while Loops just write one program at least and make sure you access these notes I'm uh saying it once I'll say it again I'll say it 10 times let's look into for Loop now here is a syntax of for Loop and whenever we talk about a loop it's nothing but repetition of instructions we have an instruction we repeat it based on a condition which means we check the condition and we repeat if the condition is true then once again we do the same thing and we keep on doing that until the condition is true the moment it becomes false that's it the loop will terminate okay so the syntax of for Loop looks like this for initialize test and increment or decrement and then there is some code 3:25:13 inside let me explain this to you this might sound very haunting to you if you are a beginner inside a for Loop we have a initialized block a semicolon then a test block a semicolon then some line of code which will increment or decrement our variable which we initialized here so let me show you how this works okay and uh once I show you the example it'll be easy for you to understand how for Loop works so I'll simply say for. c i WR a boiler plate code let me start typing for for and the moment I start typing for I should get some snippet so if I say for you can see that code snippet for for Loop once I click this I can change this to in I can press tab I'll keep it as I I'll make it seven once again I'll press the Tab Key and I can write some code here in case you are not able to get this snippet you can type this out you can type this line this line and this line out sometimes you 3:26:14 might not get the snippet sometimes the extension might not work you might be on a slower computer there are a number of things which can happen do not stop I have uh seen a lot of students stopping just because they're not getting this Sni suggestion that's that's not good okay now for in I is equal to 0 I is less than 7 I ++ let's print the value of I let's do the simplest task we can do with I and we can say the value of I is I'll simply say percentage D and what I'll do is I'll print the value of I also I want to print a back such that uh you'll be able to see uh a new line after the value of I is 0 1 2 3 4 is being printed if I run this program let me run this program and show you the output I starts from zero and stops at 6 how does this Loop work let me show you so whenever you have this for Loop this initialized part is executed just once so in I is equal to 0 will be executed once then what happens is it will check 3:27:16 for this condition if this particular thing evaluates to true then only it will move ahead if it doesn't evaluate to true it doesn't move ahead okay so is I less than seven yes it is it will move ahead okay and it will execute this so the value of I is zero will be printed as we can see here now what happens is listen to me very very carefully because this is important and once you understand how for loops work it'll be easy for you believe me so print F the value of I is percentage D and once this line is executed what happens is this particular update piece of code runs and what it does is it will simply increment the value of I by 1 so if I was Zero before it will become one now what will happen is it will check this condition again and then it will print the value of I if the condition is true if the condition is false it will jump out of the for Loop and it will stop the loop okay so I'm starting over I'm telling you once again about the fall Loop in case you didn't listen to to me carefully in I is equal to 0 will be 3:28:18 executed once so the value of I will be zero initially and then what will happen is this condition will be checked is it true yes it is true this code will be executed once this code is executed this will run I ++ the value of I will become one condition will be checked 1 is less than 7even yes this is executed yes the value of I is 1 and this keeps on happening until the value of I gets to six now once the value of I becomes six what will happen is this will run and the value of I will now become seven is 7 less than 7 no this condition is false 7 is not less than seven since this condition is false what will happen is this for Loop will be stopped we'll come out of this for Loop and the moment we come out of this for Loop the instructions after the for Loop will be executed I hope you understood how for Loop works we can come back to the notes and we can see the synex of for Loop looks like this for initialized 3:29:19 test increment or decrement and then the code runs it might be more than one line of code in this example I have just a single line of code it can be three lines it can be four lines it can be 10 lines it depends okay you can add as many lines as you want so coming back to the nodes initialize test increment I've explained a bit about what initialize is what test is and what increment is in most of the cases initialize will be setting a loop counter to an initial value test is checking for a condition increment is updating the loop counter and here is an example so I've done the same thing I I mistakenly written % I then I scribbled it off so if I is equal to 0 I is less than 3 i ++ 0 1 and two will be printed so you might want to remember this I mean when I say remember this uh I don't want you to mug up the code and the output but you should at least understand that this for Loop will print value is starting from 0 to 6 because here I is less than 7 and I ++ 3:30:21 but let me tell you one thing in some cases your examiner might change these values to something that will produce an output different from the output you might expect okay so do not mug up the output look at the follow Loop very very carefully when you solving a problem but if you are looking at this Loop which is a very simple Loop yes it will print from 0 to 6 but if I change the condition do something like this say I I I make it I minus minus then then the things are going to change then you'll have to do the dry run you'll have to see what the value of y you'll have to execute all these things one by one so you will initialize first check for the condition execute this thing if the condition is true execute this thing then check the condition then execute this thing if the condition is true then you run this then again check the condition if the condition is true you run this and you keep on going until this condition becomes false until this condition becomes false okay I hope you understood the fall Loop now I hope that I was able to explain you how fall Loop 3:31:23 works okay the output of this program is self-explanatory now we have written a code and also I made things easier for you by writing all these things in the notes I have written the meanings of initialize test and increment and I hope that uh you are going to love for Loops this is something that you're going to use a lot when it comes to programming let's solve a quick quiz now right a program to print first and natural numbers using for Loop we have already written that but again I want you to use scan a function take the value of n from the user and write the program also make sure you drop me a comment below with the time stamp and tell me that you have solved this problem were you able to solve this problem I want a yes comment from all of you that will really be very satisfying for me if I have a yes comment from all of you saying that yes we were able to solve this quick quiz I can't tell you how good I feel when you guys comment and tell me that you were able to do something just make sure you write a comment below for me and tell me that you were able to solve this quick quiz let's move ahead and let's look into the case of a decrementing loop so 3:32:25 we have a loop we have a for Loop but these things are not looking as good or rather as simple as they were looking here this for Loop looks pretty easy and we are able to make out that the value of I will be from 0 to 6 and this statement will be executed for the values of I from 0 to 6 seven times from 0 to 6 is seven times okay this was very simple for Loop let's write a program and let's try to understand how this decrementing for Loop works so decrementing do c so I've written a boiler plate code now what I'll do is I'll simply what I'll do is I'll save this program and I'll start writing this for Loop okay so I use the snippit but I'll write this so I'll say in I is equal to 5 then we have just I here is I a condition we'll talk about it just a moment IUS minus let me write this down 3:33:28 and we are simply printing I so what I'll do is I'll save this I'll copy this and that's it I'll paste it here so we are printing the value of I so I starting from five then I then I minus minus let's try to use the skills that we learned let's try to use the concept of how for Loop Works to decode this and understand what will be printed on the console okay let me close this terminal so I is equal to 5 initially okay this is a condition so what happens is I will be initialized to five this condition will be checked is five true yes five is true any non-zero value is true I've told you before zero is false but five is true so since five is true this will be executed for five so what will be printed the value of I is five will be printed and back sln will be printed now what is the value of I the value of I is four okay so let me write the value of I 3:34:29 the value of I is four this is the output and this is the value of I okay now is four true yes four is true four will be printed but the value of I will become three because this will get executed this is the updation condition in a very very similar way three will be printed two will be printed uh sorry the value of I will be two two will not be printed two will be printed after three and the value of I will be one eventually what will happen is the value of I will become zero which will make this condition as false I repeat it will make this condition as false so sometimes you'll have to run the loop one by one one iteration at a time and then only you will understand how this Loop is work working but to make things easier for you I have written how this decrementing follow Loop is working inside the nodes so this SP Loop will keep on running until I becomes zero the loop runs in the following steps I is initialized to five the condition i0 or 3:35:32 nonzero is tested the code is executed I is decremented condition I is checked and code is executed if it's not zero because any nonzero value is true in C we have seen that we have learned about that anything which is is not zero is true anything which is zero or evaluates to zero is false in C and so on until I becomes um until I becomes zero okay so as long as I is non zero you'll keep on seeing the iterations as soon as I becomes zero the loop will halt Loop will finish okay I hope I was able to explain you this write a program to print n natural numbers in reverse order and I want you to do this in fact we have already done that but I want you to write this program just take input from the user using scan a function and start writing this code I hope that you liked this concept of decrementing for Loop let's move forward and let's talk about 3:36:32 the break statement in C and also let's talk about continu statement in C both break and continuous statements are really very important and see and we are going to look into these two statements now so what is a break statement the brake statement is used to exit the loop irrespective of whether the condition is true or false what does that mean we have seen break in switch case statements by the way let me tell you but now we are going to look at break statement very very closely so break means stop what you are doing just execute the next instruction so if you are executing a switch case statement stop right here go out of switch and execute something else execute whatever is after the switch statement similarly if we are inside a v Loop and I type break or I encounter a break statement what that tells me is stop this y Loop right here and execute the next thing what does it say it says that stop 3:37:33 this Loop right here and execute the next thing it can be inside a while loop it can be inside a do while loop it can be inside a fall Loop it can be inside any kind of loop it can be inside a switch statement let me show you how this works so break statement is very important let me comment this and let me also write down dry run for the decrementing Vol Loop okay so let's now let's now start writing this um break. C I'll add the boilerplate code and what I'll do is I'll write a very simple while loop let me copy this while loop from here this is a very simple while loop and we have talked about this while loop a lot okay we are simply incrementing the value of I we are initializing the value of I to zero and I think I copied the loop correctly yes I did um I'll simply say format document it will 3:38:34 format the document for me by the way um you can do this if you if you didn't know this you can always do right click format document and that will format your C code in this beautiful fashion okay so in I is equal to zero while I is less than I'll make it 8 so this will print 0 1 2 3 4 all the way till 7 okay if I make it less than equal to it will print starting from zero all the way till eight8 okay 0 1 2 3 4 5 6 7 8 and let me execute this and you can see the value of I is 0 1 2 3 4 5 6 7 8 okay so this is how it works nothing new here nothing fancy here but let me tell you what is new now so I can write something like this I can say if the value of I is five I want to break out of this Loop and also I'll write a print F here and I'll say we are 3:39:34 outside the loop now okay so even if I even when I don't have this one and I run this code you'll see that after the last statement we are outside the loop now is printed okay now what will happen if this is present what will this do this is simply saying if the value of I is five you break out of this Loop so zero will be printed one will be printed two will be printed three will be printed four will be printed but the moment I becomes five this will be encountered this condition will be true break will be encountered and the moment break is encountered it instructs the C compiler to throw the control out of the V Loop so now what will happen is it'll come out of the V Loop okay so let me run this and 0 1 2 3 4 and the moment I becomes five what will happen is this will get executed 3:40:37 and what does this mean break means break out of the loop I don't want to execute this Loop anymore I want to stop to right here and I want to jump out of this Loop so where was this Loop starting at line number seven this is the block of the loop and it will jump out of this and will start executing statements from here okay I hope I was able to explain this I hope I was able to explain it pretty well and I hope that uh you understood it okay so let's come back to the notes and read about the break statement it is used to exit the loop irrespective of whether the condition is true or false so the while loop works works on the basis of condition okay it will check the condition again and again if the condition is true it will execute one iteration if the condition is true once again it will execute next iteration and this keeps on Happening Now what will happen is the moment the value of I becomes something which which makes this 3:41:37 condition of V Loop fals the V Loop will stop this is one way of stopping the V Loop this is how a v Loop naturally terminates but but if I want to interfere into the natural termination of Y Loop and I want to terminate it based on some other condition I can use break so I can say if the value of I is five stop this y loop I don't care about this condition if the value of five uh if the value of I is five I want this y Loop to be stopped that's it and this is what break statement is doing so brake statement is very easy um this way you can understand it whenever a break is encountered inside the loop the control is sent outside the loop let's see this with the help of an example uh how is my drawing by the way just let me know in the comments if you like this drawing okay for I is equal to 0 I is less than 1000 I ++ so here I'm saying if the value of I is 5 you break so uh what will happen is for the value of zero this will be executed this will be executed so zero is printed for the value of one this will be executed this 3:42:38 will be executed so one same for 2 3 4 the moment I becomes five five is printed because we have break after we are printing the five or printing this um percentage debation now the moment five gets printed what will happen is this will evaluate to True since this condition evaluates to True we'll encounter a break statement and since we are encountering a break statement we will come out of the loop okay so this is how it works and it will not go all the way till 1,00 or triple 9 it will not go till 999 it will go only till 5 okay so 0 to9 will not be executed so this is this is something uh which is wrong I should have written 0 to 999 because naturally this Loop will go from 0 to 9999 so I should have written 999 here but I have mistakenly written 100 here so my bad um anyways you got the point you got what break statement in C is let's talk about continue statement 3:43:39 now the continue statement is used to immediately move to the next vitation of the loop and listen to me very very carefully when I say this a lot of people confuse between break and continue statements but they are very different okay in the way that brake statement will stop the loop forever but continue statement will not stop the loop forever rather what it will do is it will stop the loop just for this particular iteration it will stop the current iteration of the loop and it will continue with the next iteration let me tell you what continues statement means so let us say we have a program uh in skip is equal to 5 I is equal to 0 while I is less than 10 and I is not equal to skip let us say we have this program how will this program execute I is less than 10 yes it is 0 is less than 10 if I is not equal to 5 continue okay so what will happen is this particular program will keep on executing the 3:44:41 instructions inside the V Loop is I not equal to skip for I is equal to Z yes this condition is true so continue continue means that do not execute anything after this just continue with the next iteration same goes for two same goes for three four but the moment I becomes five what will happen is five is not equal to 5 is false so this thing will be executed let me explain this to you by taking you to vs code and uh let me write a program myself and explain this to you and then you will understand this concept like anything thing so I have put this boilerplate code for you what I'll do is I'll simply put a very simple for Loop here let me save this program and now use for and yes I got this snipp it now what I'll do is I'll simply say uh for I is equal to0 I less than 7 I ++ means I'm going from 0 to 6 okay now what I'll do is I'll simply say the value of I is and I'll say percentage D back sln and then I okay 3:45:45 and once I do this what will happen is I will be printed from 0 to 6 nothing fancy here 0 1 2 3 4 5 6 now what will happen is if I say if if I is equal to three continue what does this mean this means that if I is equal to 3 we are going to continue continue means that whatever is written below this inside the for Loop we have to keep inside the for Loop we are inside the for Loop keeping the control inside the for Loop skip everything which is below continue so this statement will not be executed let me show you so if I run this program zero is printed one is printed two is printed but the moment I becomes three continue means stop no Don't Stop the Loop just stop stop this current iteration do not continue from here on rather 3:46:45 you go directly here and update the value of I and continue like you were continuing okay so what continue means is do not execute anything below me but at the same time also do not stop the loop I hope you got the point so this is all about continue statement and how is continue different from break statement break statement simply says that just stop this Loop whatever be the case stop this Loop we don't want to deal with this Loop anymore continue means we want to deal with this Loop but we want to skip whatever we were doing in the current iteration so we were about to print this but no we are not going to do that the moment C compiler CES continue what it will do is it will go directly here and update the condition and start executing the loop and if it encounters continue once again it will again do this and it'll keep on doing that okay so I hope you got this point and this is 3:47:45 all you know I'm trying to explain here I have written all these things in the notes so you can have a read and I hope that this chapter 4 notes will definitely help you and I hope this chapter four Loop control instruction notes will definitely help you let's let's see these notes which I've written for you sometimes the name of the varable might not indicate the behavior of the program and you'll see this a lot okay okay sometimes they'll use index inside the loop but that particular thing will not be the index sometimes they will say integer variable but actually it will be the float variable so they'll do all these things to confuse you for the interviews okay you have to see what kind of variable it is so if I say int integer variable is equal to 3 the value of integer variable is three but if I say float integer underscore value is equal to 3.1 it doesn't mean that it is going to be an integer variable integer variable is the name of the variable but actually it is float so sometimes people will try to 3:48:47 confuse you this way you don't have to get confused okay breake statement completely exits the loop we have seen this and continue statement skips the particular iteration of the loop the current iteration will be skipped that's it I hope you got the point I hope these notes were useful let's now move on to chapter 4 practice it and solve some more problems and definitely we'll learn uh these Concepts better there let's move on to chapter 4 practice it now now so here is chapter number four practice set which I have prepared for you let me open this practice set and after I open this practice set we'll solve some problems and definitely you'll get a good hang of the concepts which we have learned in this chapter so the first program says that write a program to print multiplication table of a given number n and yes I have a choice I can either use V loop I can use for Loop and I can also use dowi Loop but I'll smartly use the loop that solves this problem in the easiest way for me let me put the boiler plate code syntax here 3:49:48 and um I prefer to use for Loops in most of the cases but if you want to do it with v Loop you can definitely do it so what I'll do is I'll simply say in I is equal to 0 I less than 10 I ++ I'll press the Tab Key and what I'll do is I'll simply say uh percentage d x percentage D is equal to percentage D and I'll put three integers here first will be n the number whose multiplication table I want to print second is going to be the index I and third one is going to be the value n MTI by I so n * by I and also the semicolon and what I will do here is I'll say int n is equal to say three so I want to uh use the value three in place of n so I'll also do one more thing I'll simply say back sln so that you know when whenever I'm am uh I'm printing one line I want a new line 3:50:48 right after that so if I run this program you'll see something like this 3 * by 0 all the way till 3 * by 9 but I don't want my I to start from zero all the way till 9 I want to start my I from 1 and I want it to go from 1 all the way till n so what I'll do is I'll start my I from 1 and I'll make it I less than 11 what this is going to do is it will start I from 1 and it will take the value of I from 1 to 10 because I less than 11 uh for the value of 11 this particular condition is going to be false for the value of 10 it is going to be true so let's run and uh let's see if we were able to achieve what we want to achieve here 3 multi by 1 multip by 2 3 4 5 6 7 8 9 and yes we were able to print the multiplication table of a given number in case you want to take the user input what you do is you simply do something like this in n and then you can use scanf you can use percentage D and you can use M percent n and the 3:51:51 moment you do this and run this program let me show you something I'll wait for this program to run okay so it's waiting for my input U the moment I press seven I get the multiplication table of seven I can also say something like this just so that user can know that he needs to enter a value for this program to continue enter the value of n back sln if I run this program I'm getting this prompt enter the value of n the moment I say four I'll press enter and you'll see something like this 4 multip by 1 2 3 4 5 6 7 8 9 and finally 10 I hope that you were able to understand how this works I hope that you were able to get a hang of this thing we'll now move on to the next question and um it is really very interesting because it says write a program to print 3:52:51 multiplication table of 10 in reversed order so we have a multiplication table and we want to print it in reversed order so we are going to use the same program but in a Twisted fashion what I'll do is I'll say 10 uncore and pr02 doc I'll press enter I'll put the boilerplate code then what I'll do is I'll or or rather what I'll do is I'll simply copy the previous program and I'll paste it here once I do this I have a value and the value is 10 if I'm not wrong I'll get rid of this scanf and all uh cancel I'll save this program I want to start my I from 11 and I want it to go all the way till 1 okay so I'll reverse this condition why am I reversing this condition I'll tell you and I'll make it I minus minus so I'm starting my I from 10 not 11 and also I'll say I greater than Z not 1 why did 3:53:52 I do that I want my I to start from 10 then after decrementing it will become 9 then it will become 8 then 7 6 5 4 3 2 1 the moment it becomes zero 0 is not greater than zero so it will start from 10 and go all the way till 1 so if I run this program you can see the multiplication table has been printed in reverse order let me explain you how this works because this is something that you need to understand although I've already explained you this thing in chapter number four but I'm going to explain it once again just so that you have a good hang of it so what we are doing here is we are starting the value of I from 10 then what we are doing is we are simply running this for I is equal 10 because this condition is true so initially what happens is I is initialized to 10 then this condition is checked is I greater than Z yes indeed it's greater than Z now what happens is this gets printed now what happens is this particular update condition is ran now the value of 3:54:53 I becomes 9 after this what happens is this condition is checked for the value 9 is it true yes it is true this will run now I becomes 8 because i- minus makes the value of I change from 9 to 8 because IUS minus will reduce the value of I by 1 okay I hope you are understanding how these things work and this keeps on going until the value of I becomes zero and the moment value of I becomes 0o 0 greater than 0 is false and this will not run for the value of I is equal to 0 so if I run this program the value of I starts from 10 and it will go all the way till 1 it will not become zero okay so I hope you understood this now what we'll do is we'll move on to the next question problem number three a doy Loop is executed there are three options and I need to choose one of these three options is it executed at least once is 3:55:54 it executed at least twice or at most once and I want you guys to comment down below with the time stamp of the video telling me what the correct answer is I want answer from each and every one of you I'll wait for you to comment I hope everybody commented now let me tell you the answer it's at least once and why why does it execute at least once because in doy Loop we first run the loop body and then we arrive at the condition and if the condition is true we run that um code inside the do blog again and we keep on doing that until the condition is true so initially it will execute no matter what the condition is so it is executed at least once okay moving on to the next question what can be done using one type of loop can also be done using the other types of Loops is it true or is it false it is indeed true because we have already talked about it this same thing can be done using a v Loop because I can always write this outside my V loop I can put 3:56:56 this condition inside the condition body of the V Loop uh let me show you where uh let me open these notes and explain you what I mean when I say that so while I can put the condition this for Loop condition inside the while loop here inside the condition and what I'll do is whatever is inside the follow Loop I'll write inside the body of the V Loop and even the update code I can take this update code and I can actually write it after I have written the body of my V loop I hope that you were able to understand this I hope I explained it pretty well to you uh and you can definitely do whatever you want to do with one type of loop with other type of loop okay so this is true okay moving on to the next question question write a program to Su first 10 natural numbers using Y Loop let's solve this problem so I'll say problem five so I'll simply say 11 uncore 3:57:57 p05 do c I'll put the boiler plate code and what I will do is I have to print some of 10 first 10 natural numbers so I'll start the value of I from one I'll say while I is less than or equal = to 10 and what I'll do is I'll simply say Su plus equals to what Su plus equals to I I ++ and let me explain you what I've done do I remember all these programs no I don't I don't remember all these programs then what is happening how am I able to write this program so fast it's all about practice but let me explain you how I wrote this program I started I with one and what I'm doing is I'm taking this I from one all the way till 10 so so this condition will be true for the value of I ranging from 1 to 10 and the moment I becomes 11 this condition will be false okay so I'm updating the value of sum I'm keeping on updating this value until the value of I becomes 10 and finally when it becomes 3:58:59 11 the loop body will not run and finally if I write a printf statement I'll say the sum of first 10 natural numbers is percentage D and I'll simply print sum and if I run this program you'll see the value is 55 let me Google this what is the sum of first 10 natural numbers it's 55 yes it's 55 so I was able to find out the value of first um value of the sum of first 10 natural numbers and it was 55 great write a program to implement program five using for Loop and a dowi loop can you do this can you implement the same thing in question number five which I implemented using a while loop can you do the same thing using for and dowi loop I want you to give it a go I want you to give it a go because it will be very um satisfying for you to solve the same problem using a for Loop and 4:00:00 using a dowi loop do give it a try and in case you are not able to solve it definitely you'll be able to solve it once I solve the other problems because we have done enough practice of for loops and dowi Loops the next question says that WR program to calculate the sum of the numbers occurring in the multiplication table of 8 so we need to consider 8 multip by 1 all the way till 8 multip by 10 so we need to add 8 16 24 and so on let's see how we can do it so what I'll do is let me create a program for problem number seven so I'll say 12core p07 do c I'll put in the boiler plate code and I'll use a fall Loop let me close the terminal so I'll save this and after saving I'll write for I'll get the code snippet I'll say in I is equal to Zer I is less than 11 and I'll make it one because I want I to start from one and go all the way till 10 also what I'll do is I'll say it sum is equal to zero what 4:01:01 this will do is this will simply help me initialize sum and I'll keep on adding to this sum now what I'll do is I'll simply say sum plus equals to and I'll add I multiply by 8 to this and let me see if I'm able to print the sum the I need to use double quotes not single quotes so I'll say the value of sum is percentage D and I'll print the sum let me run it the value of sum sum is 440 so if I add 8 16 24 and so on it will add to 440 okay so it will be 55 * 8 and I'll tell you why why did I say that so the sum from 1 to 10 is 55 and we can take eight common from the equation so if I have 8 * 1 + 8 * 2 and so on I can take 8 4:02:05 common and what I'll get is 8 MTI by 1 + 2+ 3 + 4 plus all the way till 10 which is 55 and if 55 uh is the sum of 1 till 10 the sum for 8 MIP 1 + 8 MIP 2 and so on will be 55 mtip by 8 which is nothing but 440 okay we just got that and we just cross checked that the problem that we solved is indeed correct let's move on to the next question now what is the next question it says write a program to calculate the factorial of a given number using a for Loop let's do this this is an interesting problem so what I'll do is I'll say 13or PR oops I forgot to add this p08 do c I'll put in the boiler plate code by the way what is a factorial factorial of 1 is equal to let me explain you what is a factorial in case 4:03:06 you don't know uh in case you know you already know just try out this program in the meantime and I'm explaining people um what what factorial exactly is so when I talk about n factorial it's nothing but 1 * 2 * 3 multiply by and we'll go all the way till in okay now what will happen is if I talk about this equation and let us say I want to calculate four factorial what will be the four factorial I'll go all the way till four so this is the value of 4 factorial which is nothing but 4 * 3 which is 12 * 2 is 24 so 4 factorial is 24 in a very similar way I can always write n factorial is equal to 1 multi by 2 multi by 3 multip by and I can go all the way till the number whose factorial is being calculated now what I'll do is I'll simply say um 0 factorial what is 0 factorial if I put it inside this 4:04:07 equation I'll not be able to calculate so 0 factorial let me tell you is defined as one so by definition 0 factorial is one so this is all about factorial how will you calculate the factorial using a for Loop pretty simple just start with one and go all the way till that number and you need that number as well in the equation if you if you look at this equation four is there so we need I less than equal to 4 because I want to start from one and I want to go all the way till four including four I want to include four as well now what I'll do is I'll simply say uh prod multiply is equal to what multiply is equal to I and what is product product is nothing but one make sure you don't put product as zero because if you make it as zero then the answer will be zero always okay and let's also Define n let me try it for four and what I'll do is I'll play some copy paste game here I'll copy a line 4:05:07 from here just to save some time I'll say the value of factorial is uh let me call it factorial I just want to name my variables correctly I want to name my variables such that it is easy for you to understand what that variable is doing I'll save this let me run this program the value of factorial is 24 if I change this to five you'll see the value of factorial is 120 if I change this to one the value of factorial is one what if I change this to zero what is the value of factorial it's one great so why is it one for zero because this Loop will not run at all for n equal to0 I is 1 one is not less than equal to zero this Loop will not run keeping the factorial uh value as one and you'll see the value of factorial as one uh whenever uh you try to print this okay so coming back to this question WR a 4:06:07 program to calculate the factorial of a given number we already solved this repeat it using Y Loops you need to uh solve this problem using while loop and it is pretty simple all you will do is you'll simply write while you'll say while the value of I is less than equal to n and you'll write the code inside even the updation code you'll write inside the while loop and yes it is very easy to solve I want you guys to comment below if you can solve this problem using while Loops or not make sure to put the time stamp while you are commenting right a program to check whether a given number is prime or not using Loops this is very simple I'll solve it using file Loop and then as part of problem number 11 you can solve this problem using other kinds of Loops okay let me solve this problem for you so we need to find whether a given number is prime or not this is problem number 14 pr10 do c boilerplate code so we need to find whether a given number is prime or not let's call this number as n when is 4:07:07 a number prime a number is prime when it is divided by the number itself and and one there should not exist any other number which can divide n okay so how will I solve this problem I'll write a for Loop I'll start I from one I'll in fact start I from two because one will definitely divide that number and I'll go all the way till that number and what I'll do is I'll simply say if i%c or rather n% I equal to equal to 0 which is nothing but if I divides this number n so if the value of I is say five and N is 10 then yes 5 divides 10 so we need to find out whether I divides n or not if that is the case we simply say Prime is equal to 1 okay so let me call it is prime so I'm saying S Prime is equal to 1 and we'll break out of this Loop why am I breaking out of the loop I'm breaking out of the loop because that's all I want to know I want to know 4:08:09 whether this number is prime or not I'll by default make my is prime variable as zero why am I doing that I'm doing that because I'm assuming that this number is not a prime number and I'll keep on assuming that I made a mistake here I'll simply say S Prime equals to zero because it will not be a prime number and by default what I'll assume is that it is a prime number until it is proved not to be a prime number so I'll say if n% I is equal to Z is prime is equal to Z so if any number happens to divide this number what I'll do is I'll simply say that this is not a prime number and the that happens I'll break out of this Loop because I now have this information that this is not a prime number and that's all I wanted to know so I'll simply say print F the number is prime back sln when will I print this I'll print this when uh the value of is prime is true so if is prime I'll print this otherwise 4:09:09 what I'll do is otherwise I say this is not a prime number number is not prime okay let's see if this program works and I'm going to explain this program once again to you so if n is five yes it is prime let's check for six no it's not prime so yes it's not prime let's check for two is it Prime two is prime yes two is prime let's check for 9 9 is not prime no it's not prime let's check for 91 it's not prime 91 is not prime let's check for 925 it's definitely divided by five so it's not prime it's not prime okay so I hope that you understood how this program to find out whether number is prime or not um I hope you understood the logic of this problem basically what we are doing is we are starting from Two and going all the way till that number to find out whether there exists a number which can divide this number and 4:10:11 the moment we find that number what we do is we break out of the loop and we declare the number as not Prime and if we are not able to find such number we assume uh or rather we stick to the Assumption which we initially made that the number was Prime because we were not able to find any number which can divide this number n which clearly proves that N is a prime number okay so this is how the program to write whether a number is prime works now I'll tell you something there is a way to make this program better by changing this n rout 10 but I'm not going into those details I'm not going into data structures and algorithms and making programs efficient I just want you to solve this problem so that you have a good understanding of how C language IFL statements conditionals loops and all these things work I hope you enjoyed this practice set I'll save this Pro uh program and I'll give you this program uh in the form of a zip also I'll give you the notes in the form of a zip so that you have all the notes by end of this chapter so Implement and using other types of Loops is something that you 4:11:11 need to solve by yourself I hope that you enjoyed this practice set now we'll move on to chapter number five where things will get even more exciting and we'll learn about more concepts of C so let's move on to chapter 5 now from this video onwards we are going to look into chapter number five which is nothing but functions and recursions what is function and what is recursion we are going to study all these topics in this chapter so I have written something in the notes let's see what it is sometimes our program gets bigger and bigger in size and it's not not possible for a programmer to track which piece of code is doing what so far the programs written by us were very basic they were not very complicated they were around 29 at Max 30 35 lines of codes but what happens is when things get complicated when you have something which is doing a lot of work when you have a code B which contains say 450 to 480 lines of code then you unknowingly might be repeating a piece of logic again and again for 4:12:12 example examp let us say you are calculating average of seven numbers inside your program and you are doing that at 21 different places so all those 21 different places which contain say four lines of code to calculate that average can be replaced with a single function call what is a function call we'll see uh what a function call is in this chapter but I'm just giving you an idea of how functions promote code reusability so we want to reuse the code again and again the code which we have written has to be reusable we do not want our code to be inconsistent or say we don't want to repeat our code in our program again and again we'll look into all these things in this chapter so what is a function a function is a block of code which performs a particular task so a block of code performing a particular task is function a function can beused by the programmer in a given program any number of times for example you have written a function which calculates average of three numbers you can reuse that part particular function any number 4:13:14 of times and we'll talk about some of the syntax and some of the terminologies associated with the functions and then we'll use functions and see how they can make our program more efficient and effective so here is example syntax of a function so here is a code which I've written for you let me write this code once again in my vs code and that way you'll be able to understand what a function is so I'll simply say 01 function. C I'll add the boiler plate code and let me write something called void display you might be thinking what this void display is and what this void display is doing this is called function prototype this is telling me that display is the name of the function it is going to return void and it doesn't take anything as the arguments so we are going to look into these things if you don't understand what this is we are going to understand this very soon but let me complete this program by saying int a display and return zero so I'll simply say int a then display and then 4:14:18 return zero is already here okay now what I'll do is I'll write what is called a body of the function so this is the body of the function I'll simply say print hello word back sln back sln and I'll save this program and let me run this and show you what happens I'm waiting for this program to execute and hello word is printed on the screen how does all these things work let me tell you how all these things are going to work this is something which is called function prototype okay this is something this entire thing from line number five to line number nine uh is the main function so this is the main function this is also a function so this function basically contains the lines of codes which are executed from the beginning so any program or rather any c program starts from the main function now what happens is I'm writing the body of this function so whenever I write 4:15:19 function prototype for any given function I'll write the body of that function later and inside this body what I'll do is I'll add some set of instructions and printf hello word backslash is one of those set of instructions so now what I can do is I can use this display and these set of parenthesis to Simply run these set of instructions isn't it cool I I I can say display any number number of times to print hello word you must be thinking why are we saying display to print hello word why can't we simply print a hello word hair itself okay you can do that but what now what if I want to print something like this what if what if I want to print hello word hello two hello word 3 hello word 4 hello word 5 something like this what if I want to print this I can simply say display and this will run these set of instructions so if I run this program now you'll see that hello word 1 2 3 4 and five has been printed on the screen so this is the magic of functions you can have multiple lines of code inside the 4:16:19 function body and you can say something like this display and this display when you say display this will execute all these lines so there is something called function prototype and there is something called function definition there's also something called function call which we are going to look into in a bit so function call so this is a function call uh display so basically what we are doing here is we are simply calling the function we are saying that run the body or rather run the instructions inside the body of the function so void display whatever is written inside this set of parenthesis just start executing those lines so hello word one hello word two hello word 3 hello word four hello word five these lines will start getting printed because we have a printf statement now coming back to the notes because I have done a very good job in putting all these Concepts together I've talked about function prototype call and definition inside uh these notes but before we look 4:17:20 into those definitions I want to tell you how basically this works in a nutshell so here is the function prototype so void display basically I'm telling that display will be a function it will take nothing as the input and it will output nothing void means nothing okay now whenever I'm writing the body of the function it's called function definition so I'm defining what this function will do do so in function prototype I'm telling what this function will return and what this function's name's going to be and what this function will take as input function definition basically jots all those statements down which will be execut it whenever I call the function and what is the function call and how do I call a function I simply write the name of the function and give the arguments I can pass a number I can pass the things that this function needs luckily this function doesn't need need anything so I don't have to pass anything this simply displays I am printf whenever I say display and whenever I say display and the set of parenthesis semicolon this is 4:18:22 a function call and this will execute IM display and IM am display will be printed now let's look into the definition and see how I have explain prototype call and definition in the noes so function prototype is a way to tell the compiler about the function so I'm basically telling about this display function the display is going to be a function it's not going to return anything and it's not going to take anything as an input let us say you have an average function which is going to take three integers so you can you you'll have to write something like this you might have another uh function called uh average which might take int float and int so you'll have to pass these three numbers whenever you're calling the the average function but we don't have any such function let me keep this line I'll simply comment this out so this is the function prototype I am not telling what this function will do inside the function prototype but through function prototype which is a single line I am simply telling that I want a function which is whose name is display it is going to return nothing 4:19:22 and it doesn't take anything from the uh user so there is no user input so function prototype is a way to tell the compiler about the function we are going to Define in the program here void indicates that the function returns nothing so this void indicates that this function is not going to return anything okay so I hope that uh that that explains what function prototype is in a good detail uh function call is a way to tell the compiler to execute the function body at the time the call is made so whenever compiler Encounters this display and this parenthesis what compiler will do is compiler will simply start executing these instructions sprintf hi I am display all these instructions will be displayed by the compiler whenever compiler sees such a thing so I hope that you are able to understand how these things are working and how this display function works okay coming back to the notes um function call we have just talked about function call note that the program execution starts from the main function in the sequence instructions are written so the 4:20:25 program execution will start from here and whenever this display call is encountered what will happen is that this this function will start executing all these instructions inside this function will start executing Now function definition this part contains the exact set of instruction instructions which are executed during the function call so whenever you call a function you are definitely going to execute some set of instructions what will be those set of instructions those set of instructions are nothing but the lines which are written inside the function this is the function definition it is also called function body and this part contains the exact set of instructions which are executed during the function call when a function is called from meain the main function falls asleep and gets temporarily suspended so let me tell you what happens so whenever this program starts executing what happens is let me show you here whenever this program starts executing what happens is this line will be executed and whenever this line starts to execute the main function 4:21:25 will temporarily sleep the main function will be given the instruction to sleep by the compiler compiler will tell the main function that hey main function please rest for some time you need some rest let display function do the job for some time and then we'll come back to you so display function will be active and it will run all these instructions and the moment this display function stops executing the control will come back to the main so main function will be uh awaked by the compiler compiler will tell the main function hey main function please wake up you need to wake up and you need to start your execution and since the display call has already ended this line will be executed and finally this body or rather the main function will stop its execution and hence the prr program will be terminated I hope that you were able to understand uh how this function works okay coming back to the notes we talked about function prototype we talked about function call we also talked about function definition here is a quick quiz 4:22:27 for you you have to write a program with three functions the first function is good morning function which prints good morning uh second function is good afternoon function which prints good afternoon and the third one is goodn night function which prints good night so let's see how we can solve these three problems all you have to do is uh you have to write the body of these function and then there is something else which is given in the question which is main should call all of these functions in this order so first one will be executed first then second one will be executed then the third one will be executed so let's write this program so what I'll do is I'll say 02 uncore quore quiz. C and I'm going to write the boiler plate also I'm going to write the function prototype so I'll simply say void uh let me see the names of the function uh so good morning good afternoon good morning is a function good afternoon is going to be a function 4:23:28 good afternoon and then good night is going to be a function so good night is a function these three are functions now I'm going to write the body of these functions so what I'll do is I'll simply copy this then I'll paste this this and then I'll start writing print F and what I'll say is I'll say good morning back sln back sln simply means that I want to give a new line after this line is printed so good morning will be printed and then a new line will be printed and what I'll do is I'll repeat the same thing for all these functions so I'll write the same thing for good afternoon I'll write the same thing for good night so I'll say afternoon and and I'll say night okay so good afternoon good night good morning okay so these three are the functions which we have written let's execute them one by one and in order to execute them one by one what I'll do is I'll copy this uh right here I'll press the Tab Key now I'll simply click here and I 4:24:33 will remove this void I'll save the program let me run this good morning good afternoon good night so these three functions are hence executed in order okay I hope that this explains the concept of functions and you were able to understand what function definition call in Prototype is now I have joted down some important points for you which are really very important because you would want to know about how to use and when to use functions in C so execution of a c program always starts from Main never think that any function which is at the top will be executed first no that's not going to happen always and always the C compiler will will start looking for the main function first and it will execute it first so execution of a c program starts from the main function main function is the function which is actually executed as soon as the program starts executing okay we have some more important points every function gets called directly or indirectly from Main so whenever you run a c program main is the function which 4:25:33 will start executing first so naturally any function which gets executed will be directly or indirectly be called from Main so there are two types of functions in C we'll talk about them there are two kinds of function the first one is Library functions and the other one is user defined function what is a library function commonly required functions group together in a library file on disk so Library function is something that C programming libr already has to offer you you don't have to write the logic of those functions what is user defined function user defined function is a function which is declared and defined by the user so if I want to write a custom function to calculate the number of shipments in my uh say corer company I will have to write that function nobody's going to provide me that let us say I'm going to uh you know start making cell phones from tomorrow and I have to write a function which is going to tell me that how much money I'm making per cell phone per single sale of cell phone um is making me say 10 Rupees 4:26:34 I want to write a function which can look into the different parameters and automatically calculate this number for me I will have to write this function so it has to be a user defined function I cannot expect a c compiler to ship with this function naturally I'll have to write it so these are the functions which are custom and they are custom tailored for your needs so you'll have to write those but there are some of the functions which you might need for example there's a function called P pow we have talked about pow which is used to raise a number to the power of another number so these kinds of functions are Library functions and the these are readily available uh inside a library file you might also want to talk about uh averaging three numbers inside your program and if you want to average three numbers you might get a library function for that inside your program let us say you want to sum 10 numbers you want to find the sum of 10 numbers you might get Library function for that so anything that is not specific to your 4:27:36 needs anything that is very very generic you will get it in the library functions some of the other Library will give you that function we'll talk about some of the library functions later in this course but uh we talked about user defined functions in fact our display function was a user defined function so this is about user defined functions and Library functions I hope you were able to understand what these are now we'll talk about why we need to use functions why use functions to avoid rewriting the same logic again and again so let us say I want to print good morning or let let me come back to this one uh let us say I want to print this particular um pattern hello word one hello word 2 hello word 3 4 and five that is I want to print this 10 times so I don't have to copy these five lines and copy them again 10 times making them 50 lines I can actually do this by simply copying this any number of times I want it to execute so if I wanted to execute 10 times I can copy 4:28:37 this particular line 10 times I hope that you are able to understand this and I hope that you you are able to see what I mean by uh by by saying that you will avoid rewriting the same logic again and again if you use functions functions will also help you keep track of what you are doing in a program so if we have uh a logic divided into different functions we can focus on one function and start debugging that function if that function is not performing as usual the third thing is to test and check logic independently so I can actually have say a person called rohand Das write this function and a person called Jacob write another function and a and and a person called uh say sonali write another function and these three people can work independently on three different functions and what I will give them is a function prototype I'll tell them that I want this particular return return value from my function and I want to give these two or these three or these four values as an input to this function and those people will have to manage the logic that will be written 4:29:39 inside the body of this function if that makes sense I I hope that you were able to understand this now we'll talk about passing values to the functions passing values to functions is something that you will definitely want to learn what does it mean it simply means that there is a function it takes some values as an input and gives an output you need to receive output by giving some inputs and uh you will definitely want to use a function like sum where you are supplying two input values and you are expecting an output value from your function let's see how all these things work uh we can pass values to a function and we can get a value in return from a function for example let us say sum is a function and we have two values which we are supplying to sum as an input what are those two values those two values are A and B if these values are three and four I will expect my sum to return an integer which is nothing but seven because 3 + 4 is 7 similarly if a is five and B is three I I I would expect this sum to give me eight in return because sum of these two numbers is eight now the above prototype means that 4:30:42 sum is a function which takes values A and B of type int so a is an integer B is an integer and whatever I'm getting in return is an integer now it is not necessary that if you're supplying integers you will get an integer you might get a float as well for example um in case of a function which calculates average of two numbers you might expect a float or you might expect a double in return but again uh in this case I'm giving A and B which are integers and I'm expecting in an output which is an integer okay so this will return a value which is of type int so sum will return a value of type int Now function definition of sum can be something like this so I'll say something like this I'll say int sum int A B A and B are input to sum in C will store the output I'll simply say C is equal to a plus b I'll make the calculation and finally I'll return return the value of C so A and B are called parameters I'm supplying a and B to sum and I'm 4:31:43 expecting a value C which is calculated by this function sum let's see how this thing works what I'll do is I'll simply say passing underscore values. C I add a boiler plate template and let me do one thing I'll simply uh you know copy a function prototype from here or let me write it uh write it down once again I'll simply say sum int int so what this means is that sum is a function it returns an integer and takes two integers now I'll write the body of sum I'll simply say int a int B sum is a function which takes a and b as an input and what does it return it returns a plus b so we don't even need a variable C we can simply say return a plus b and this will take care of everything now if I do something like this and I say the value of 5 + 7 is I can do something like this I can say sum 5 5A 7 and this 4:32:43 will actually calculate this 5 + 7 for me and it will print 12 for me so 5 + 7 is I should have written percentage D my bad so this value will actually come here so it will return me to L let me show you the value of 5 + 7 is 12 how does all these things work so the execution of a program always starts from the main function so when we are starting the program we'll start it from here this line will be the first line to get executed but while this line is being executed we'll encounter another function call so what will happen is main call will be suspended compiler will tell main hey main you were executing but I want you to wait for some time because now sum is executing and let sum bring its value and once sum brings its value we are going to put that value here and we are going to um then continue your execution then execution of the main function will continue so what happens is this 4:33:43 function uh the main function will get suspended for some time and some function will uh start to execute I'm talking about this suum function this function will start to execute and what happens is this calculates the value A+ B and it Returns value 12 so 12 is returned back to the main function so this can be replaced with 12 so I can do something like this I mean in my mind obviously in the program uh we are not going to change the program but during the execution what happens is this value is replaced by 12 and what what will this line print then this will print the value of 5 + 7 is 12 so percentage T will be replaced by 12 I hope you got the point I hope you understood how uh we can pass values to a function and we can expect a value in return from the function now we can call sum 2 3 from Main to get five in return so if you say in D is equal to sum 2 comma 3 sum 2 comma 3 will return five and D will 4:34:45 become five because this value will be replaced by five I have written some notes here so the note number one says parameters are the values or variable placeholders in the function definition for example A and B so parameters are nothing but values or variable placeholders what are values and what are variable placeholders values can be 2 three or something it can also be a variable placeholder for example I can do something like this I can say int a is equal to 5 and int B is equal to 7 and I can actually say sum a comma B that will also do that is also valid so a is 5 b is 7 so this thing remains unchanged the output will be the same but just in case you want to use variables you can always do that okay note number two says that arguments are the actual values pass to the function to make a call for example 2 and three okay so I want you to uh understand the difference between parameters and arguments now let me tell you one thing 4:35:46 if you are talking about code and programming or C programming with somebody they might interchangeably use parameters and arguments but again you should know the core key difference between parameters and arguments what is the difference between parameters and arguments I'll tell you using um an example okay so two and three are the actual values which are being passed so which values are passed five and seven are passed so five is an argument s is an argument in this case Okay so actual values which are passed to the function are arguments but what are parameters A and B are parameters so if I talk about this function A and B are parameters for this function but if I talk about this function called A and B or rather five and seven are the arguments so when I'm looking here I'll use the word argument A and B are arguments but when I'm looking here at the function body I'll use the word parameter now again nobody's going to ask you this question until unless you are sitting in some interview and your interviewer really takes some interest in this question but 4:36:47 just in case you wanted to know the difference between parameters and arguments this is the real difference and you should definitely know it the third Point says that function can return only one value at a time so one function can return only one value at a time in C language this is something that you should know as well if the past variable is changed inside the function the function called doesn't change the value in the calling function what does this mean mean let me give you an example okay now if the past variable is changed inside the function for example I have a function called change and I change this value a is equal to 77 no this will not change the value in the main let me give you an example if I say something like this uh oops if I if I say something like this C is equal to 9 and if I have int C isal to 8 here and if I print the value of C is and let me print the value of C also I'll put a back sln just so that my uh 4:37:48 code looks good now if I run this uh or let me even pass C I know I don't have to pass C but again I'm passing C uh let me pass C and C but I'm only operating on A and B now let us say I am changing the value of C inside this function I'm making it 9 from 88 what will be printed 9 will be printed or 88 will be printed the answer is 88 will be printed let me run this program for you okay so I need to change this function prototype as well because I have two integers here I need to make it three integers now the value of C uh 5 + 7 is 12 and the value of C is 88 so we are not able to change the value of the main function inside the sum function so this is something that you need to keep in your mind this is really very important I and why does this happen whenever a function executes it has its own set of variables so a belongs to the main function B belongs to the main function C belongs to the 4:38:49 main function now once again inside the sum function this C belongs to the sum function so this is maintained separately from this C so this is a c of main function this is a c of sum function so we also call these values as local variables so these are local variables of sum and this is a property of mean so we cannot notot change the property of mean from sum just like you cannot change um change the volume of the radio of your neighbor sitting at your home if that makes sense you cannot do that definitely if he gives you the knob you can change if if he you know ties a thread to the knob and you know throws at your house and you can pull the knob somehow then definitely you can change but again directly you cannot change merely sitting at your room you cannot change the TV channel of your neighbor so this is maintained differently your TV is different your neighbor's TV is different you cannot change your neighbor's Channel by using your remote on your TV okay I I've given enough an analogies and I've also 4:39:51 written this in the notes changes a function which changes a to 77 now it should be now now if we call it from mean like this then uh this will print the value 22 so we cannot change the value just like we saw in example here we cannot change the value of main function variable from some of the function this happens because a copy of B is passed to the change function so what happens is the these a B and C are not directly passed to sum the copy of these variables is passed to sum so this this and this will remain unchanged if you try to change it from the sum function okay so this is something that you need to keep in your mind now we have a quick quiz here use the library functions to calculate the area of a square with side a so we need to use some Library function to calculate the area of a square with side a how do we do that we can use a user defined function in this case we can simply say a square because we know that a square is the area but since we have to use 4:40:52 Library function we can use a library called math.h so I'll say C programming matt. H so all you need to do is simply include this matt. H so I'll use the first result that comes up on Google let me show you how um P pow function from matt. works so uh p is a function double power xou y so I'll show you how this works so firstly what you need to do is you need to include this Library matt. so let me make a program 04 uncore Quake quiz 04 uncore quore quiz. C let's enter boilerplate code let's include m.h and I'll simply use Sprint F the area of the square is all I'll do here is I'll say the area of the square is I'll say percentage D and what I'll do 4:41:53 is I'll use the power function and all I'll do is I'll simply say a to the^ 2 and what is a a is the side of the square so if I have two uh I should be getting uh four as the area okay let me use a double variable okay let me use a double variable so that we can use values with decimal Precision so able a is equal to 2 and we don't use percentage D for double we use percentage LF for double and if I run this program you'll see the area of square is four so even if I have 2.2 you'll see something like this so the area of the square is 4.84 I hope that makes sense and you were able to understand how we solve this quick quiz using Library functions now if you are getting confused uh with what these Library functions are and how we going to use it uh in the later chapters do not get worried about about all these things let me tell you all you need to know at this point is that we have used m.h and a function from m.h which simply raises a number with the power of two in 4:42:53 this case we can even raise it to the power of three four or any other number but in this case we are simply raising it to the power two so PO is a function from matt. H this is all you need to know at this point coming back to the notes we'll talk about recursion now recursion is really a very important topic when it comes to C programming and we are going to discuss it in great detail let's talk about what we have in recursion a function defined in C can call itself this is called recursion a function calling itself is also called a recursive function okay so this is something that you need to know a function defined in C can call itself so if I have a function called sum it can call sum once again and this is called recursion in C language so a function calling itself can also call um is also called recursive function so we can call a function which calls itself as a recursive function now you must be thinking what this recursion is and why should I even bother about recursion when I can write programs in other ways recursion is sometimes the easiest way to solve a problem because the formula 4:43:55 contains recursion for solving the problem I I'll tell you what this means uh and we'll take an example of factorial in this case and this is going to explain you everything because factorial is an example which explains recursion very well so let's come to the notes sorry the code and what we'll do is we'll create a program called factorial doc let me write a boiler plate code and I told you what factorial is already but let me tell you once again factorial what is factorial factorial 5 is nothing but 5 * 4 * 3 * by 2 * 1 okay and factorial n is nothing but you start from n you keep on reducing number to nus1 then you keep doing this for n minus 2 then you do this for n minus 3 and you go all the way till 1 so n n-1 4:44:55 n- 2 nus 3 and you go all the way till one okay so this is how factorial works and uh if it is five here you'll go from five and you can you'll keep on reducing the integer until it becomes one if it is nine you'll keep on reducing the number and keep on multiplying those numbers till it becomes one so 5 4 3 2 1 and once again if the value is 13 13 12 11 10 9 8 7 6 5 4 3 2 1 and so so you understood how factorial works now now the same thing has been written in the notes as well I've written factorial n is 1 * 2 * by all the way till n so you can either write it like this or you can write it in a reverse manner 1 2 3 4 all the way till n uh it's the same thing okay but we have a special case of factorial 1 which is very clear that the value of factorial 1 is one this one is clear but we have a special case of factorial 0 what if the value is zero so factorial 0 is defined as one this is something that you need to keep in mind 4:45:56 so this is a mathematical formula and we Now understand how factorial works now listen to me very very carefully let us say we have this formula factorial n is equal to this thing and I want to change it I mean I want to write it in a different way how can I do that can I not write factorial n is equal to n * by this thing yes we can we can write it it's it's what it is written okay this is what what is written here now what is this value what is the value n-1 * nus 2 all the way till 1 isn't it not factorial N minus1 I hope you understood that this is factorial n minus one so I can replace this with factorial n minus1 I can do that right I can yes I can do that because by definition of factorial n this is the formula and factorial n minus one will be this value so factorial n is equal to n into factorial n minus1 and we are going to use this to our advantage we are going to use this 4:46:56 formula to calculate the values of factorial let's see how we can do that okay so what I'll do is I'll write a function called factorial and this simply takes a number a or rather n let me call it it n because I'm calling it n here as well also I'll write a prototype factorial n that's it okay now what I'll do is I'll simply write this formula I'll say return what is factorial n factorial n is n * factorial n minus1 I'll say return the value n multip by same function for n- one okay and that's it that's it so what will happen is it will keep on reducing the value of N and it will keep on calculating the factorial I'll show you the tree how this will work so a very good example of recursion is factorial I've written factorial n's value then I've showed you that factorial n is equal to n * factorial n minus 1 now 4:47:57 since we can write factorial of a number in terms of itself we can program it using recursion how will we program it using recursion let's see let us say I I've supplied four to this so it will calculate 4 multip by then it will wait for factorial of 3 it will say 4 * by then it will it will start calculating the value of factorial 3 then it will be 3 * by it will start calculating the factorial of two and so on so there will be a time that this value will start going into negative factorial minus1 factorial min-2 we don't want that so what we do is we write a base condition we say if the value of n is 1 or the value of n is what or the value of n is zero simply return one so we say return one if this is the case okay and that's all that's the program to write factorial I'm not saying that you understood how factorial program works I'm going to explain it even more but I'm telling you that this is all I need to write to calculate factorial let's see if this program 4:48:57 works the value of factorial 4 is percentage D so I'll simp please say uh factorial 4 uh now what I'll do is I'll run this program the value of factorial 4 is 24 let's check 4 * 3 is 12 m 2 is 24 yes it is correct let's change this to five and let's see if it turns out to be 120 because 24 * 5 is 120 let's see if this works and yes it works 24 * 5 is 120 great now if I make it six I'll have to multiply this by six and it it should turn out to be 720 let's save this program and run it 720 yes this works but how does it work let let's try to understand it even more how this function is working so whenever a value n is received let us say four is received what happens is this condition is not true because n is not one n is 4:49:58 not zero n is four so what happens is let me show you here I'm saying factorial 4 what happens is that the C compiler will start running Factor function for the value 4 but what I'm returning here is I'm breaking it down to 3 * factorial of 3 so I'm simply saying sorry 4 MIP factorial 3 n * factoral n minus 1 so I'm doing something like this but this is not yet resolved I need to further calculate the value of factorial 3 so now it becomes 3 * factorial 2 okay now factorial 2 is still not resolved I'll try to resolve it once again and it will be 2 MTI factorial 1 but as soon as I reach one what will happen is this condition will become true and instead of returning this what will I return I'll return this and let me tell you something as soon as a function encounters a return statement it will not execute anything below that so as soon as I see return it means stop 4:51:00 the function we are done we got the value and we don't need to run this function any further so as soon as factorial 1 starts running this condition will be true because the value of n is 1 it's n = to 1 or Nal to 0 is true because n is 1 so what happens is one is return and now all the values are resolved so compiler can now uh now start to calculate this value which is nothing but 24 okay 3 * 2 is 6 6 * 4 is 24 so this is how this program is working so this is a dry run this is called dry run of factorial 4 and I'm going to give you all these programs in the form of a zip file make sure you download all the programs that I'm giving you it is really very important for you to download notes as well as all these programs which I'm giving you because all these programs also contain a lot of good Concepts like dry run and I've written a lot of comments in the programs just for you okay so this is how this works now let's come back to 4:52:00 the notes and see how this works so i' I've I've written the same thing in the notes as well a program to calculate factorial using the recursion so this is the program in case you want to see see the notes and type it out now how does this work I've explained the same thing factorial 5 will be broken down into 5 into factorial 4 now further factorial 4 will be broken down into 4 into factorial 3 and so on until we encounter this I've explained it here I've explained it for the value of factorial 4 in the notes I've explained it for the value of factorial 5 I hope all this makes sense okay now I have written some important notes for you and the notes are um recursion is sometimes the most direct way to code an algorithm just like we saw in the example of factorial there is another example very classic example of recursion it is called Fibonacci series we are going to see that in the practice set but again I'm just telling you sometimes a mathematical formula can directly be written inside the function and we are all done so recursion is some sometimes the most direct and easiest way to solve 4:53:01 a problem now the condition which which doesn't call the function any further in a recursive function is called as the base condition what is the base condition here this is the base condition base condition ensures uh that we are not calling the function infinitely we don't want to do that we always want to stop somewhere and base condition takes care of that okay sometimes due to a mistake made by the programmer a recursive function can keep running without returning result uh resulting in a memory error so sometimes you might make your program throw a memory error just because you have made a mistake for example let let us say uh I forget to write this base condition what will happen is this particular function will keep on running and running and running uh until the memory is full and we don't want that so we don't want memory or errors from our program so we need to be very very careful when we are writing programs using recursions I hope that makes sense and you were able to understand functions and recursions this chapter 4:54:03 was about function and recursions I hope I was able to explain all the things in a very good way make sure you download the notes make sure you download the code uh it is going to really help you with the revision and I also advise you to download these notes and take a print out from your nearest zero Center that would be uh better than the PDF but if you are a PDF person you you can download the PDF as well I have everything in the description let's now move on to chapter 5 practice set and see what are the problems that we can solve uh for functions and recursions let's move on to chapter 5 practice set now so here is the chapter 5 practice set which I have created for you this practice set contains a lot of problems which can help you understand functions and recursions so let's move ahead towards Visual Studio code and start solving some of these problems so question number one says that write a program using functions to find average of three numbers so let's start writing this code uh so what I'll do is I'll simply say 06 pr01 doc I'll add the boilerplate code and all I have to do at 4:55:05 this point is I have to write a program using functions to find average of three numbers so what will be the return type of my function so if the name of my function is average and let us say it takes three integers what will be the return type of this function I want you guys to comment down below and tell me what will be the return uh return type what is the ideal return type that I should write here I'm waiting for your comment all right in case you commented I I will tell you what should be the return type it should be double or float float is also the correct answer double is a bit more precise because if you have a very large number double will take care of that but why did we use double here we use double here because we might get the average as a decimal number so let us say the average of 1 2 and 3 3 + 3 is 6 divid 3 is 2 it's an integer so no problems but let us say I want to find average of 1 1 and 1 it's 1 by3 or 33 and 33 cannot be 4:56:06 stored in an integer it can be stored in a double or in a uh floating Point number okay so I can either use a floating Point variable or I can also use a double type variable but I cannot use integer uh I might lose Precision in case I use integer so I'll simply say int a int B and in C so I have three numbers and it's returning a double so what I'll do is I'll simply return a plus b + C / 3 and also just to be sure that A + B plus is calculated before this this number is divided by three I'll put on the parenthesis so this is the average of three numbers and we all know about averages average is something which is uh very very easy thing and we all know about it but again we have to write a program to calculate average of three numbers using functions okay so I'll simply say print T the average of 4 and three is percentage I'll say 4:57:10 percentage LF and also what I'll do is I'll simply write 1 4 and three because I want to calculate the average of these three numbers let me put a semicolon here return zero and let's see what answer we get so it's 2 so 4 + 1 is 5 5 + 3 is 8 you must be thinking why is this the case why are we not getting the correct answer the average of 1 4 and 3 is definitely not two but why are we getting two it's because this and entire enti is going to be an integer a plus b plus C is going to be an integer because it's a sum of three integers and three is an integer and integer divided by integer is going to return an integer and you're going to lose Precision so in order to fix this I'll simply say 3.0 and I'm going to get the correct answer I hope you understood how uh you know very basic things in C can get you some error C is a very basic programming language and you need to take care of each and every bit if you want your program to do the intended t task I hope 4:58:11 you enjoyed this problem coming back to the practice set the next question says that write a program using function to convert Celsius temperature into fahrenheit so if I say Celsius to Fahrenheit there is a Formula that I'm going to use I'm going to directly use this formula and uh once I use this formula I will be able to write a function which will convert celsus to forite okay so let's do one thing let's uh let's try to uh you know create another program and keep the things in a very nice order so that you know when you come back to these programs you can simply see where I have written which program and you'll be able to understand the entire course very well if I have all these programs in a good structure so let's do one thing uh let's start solving this problem and what I'll do is I'll simply say uh int Celsius or let me call it in C and I'll say int f and what I'll do is 4:59:11 I'll simply write down this formula but I have to use functions okay so I'll simply say uh float let me use floats this time Celsius to Fahrenheit and uh Celsius to Fahrenheit or fhe to Celsius convert Celsius temperature into fahrenheit Okay cool so I need to convert Celsius to Fahrenheit and what what will be the input of this function Celsius and c will be the input now what I'll do is I'll copy this code here uh definitely it's not a code I'll simply say int f is equal to and let me make this as float as well let let's keep everything in float uh degrees Celsius multiply will be replaced with star and C 9x 5.0 just in case um and here will be C uh and what else what else what else I need to remove this okay so what I'll do is I'll simply put a semicolon here and 5:00:12 this is forhe temperature using uh Celsius uh temperature so let me check the formula once again so if I want to convert a Celsius to forhe I simply put C here and uh that will give me the forhe temperature cool I think I'm doing it right I I need to return F now and also I need to write a print F uh and and I'll say the value of zero degree in foren heit is percentage LF or percentage F because we are using float here let me call it percentage f as well so of uh the first value is Celsius C and the second value is f and yes I'm going to use this formula I'm going to use this function I'll say int f is equal to C to F and I'll pass in 5:01:14 the C that's it and also I'll convert this to float and let me say float C is equal to Z so C is z let's see what we get okay so I I I need to put this uh let me do something like this I'll say 0.0 I'll simply use uh float here and this is the function prototype let me run this program now and the value of this much in forhe is this much how much is uh 37ยฐ C in Fahrenheit let's check that so it should be around 98 and yes it's around 98 and yes the program works and uh works pretty great now let's um start with the third problem which says write a function to calculate force of attraction on a body of mass m exerted by the Earth so how much is the force exerted by Earth uh on a body of mass m the force is equal to mass into 5:02:15 acceleration and here the force is going to be mg so what we'll do is we'll simply multiply M with G and I want you to solve this problem so simply uh use your Visual Studio code and and write this program the function should return the mass multip by G whose value is 9.8 I hope that you'll be able to solve this and this is really very simple and straightforward problem as well so I want you to solve this on your own own uh and I'll move on to the next problem and this is the problem I was waiting for the problem is super simple it simply says that write a program using recursion to calculate nth element of Fibonacci series now in case you don't know what is Fibonacci series let me tell you so Fibonacci series starts from 1 1 2 3 and any next number is going to be the sum of previous two numbers so if I were to find out third element of a Fibonacci series it will be two how did I get this two let me zoom in a bit how did I get the get to this two I simply added the first two numbers I simply added first two 5:03:17 numbers one and one and here is the next number of Fibonacci series how to get the next number I simply add the previous two numbers how to get the next number I simply add the previous two numbers how to get this 8 5 + 3 how to get this 13 8 + 5 how to get the next number it's going to be 13 + 8 which is 21 we have to automate this and we we are going to use recursion to do this how do we use recursion we'll simply write a function that calls itself and we are going to create a formula for calculating and term of a F Series so let's write a program which is program number eight of this um of this chapter and this is problem number four if I'm not wrong and I'm going to write dot C and yes it's problem number four so what I'll do is I'll simply put the boiler plate code first I'll write a function I'll say int Fibonacci series element number n is going to be Fibonacci nus1 plus Fibonacci n minus 2 so this is 5:04:21 the previous number and this is previous to previous number so if I add previous two numbers I should get uh this um the nth Fibonacci number and uh let's start this n with one because I want to count it from one uh the first number is this second number is this third number is this so what is fib one it's one what is fib 2 it's one so what I'll do is I'll simply write a base condition where I'll say if n = to 1 or or n is equal to 2 what should I do I should simply return one because Fibonacci 1 is going to be one Fibonacci 2 is going to be one so I'm returning one otherwise I'm adding the previous two numbers now I can simply say uh print F I'll say the nth element of Fibonacci series is percentage 5:05:22 D now what I'll do is simply um simply put uh FIB n and I'll create a variable n I'll simply say n is equal to 4 and now what I'll do is since I since I've written FIB 4 how will it calculate it will simply break FIB 4 down to FIB 3 uh plus FIB 2 okay now it will start to calculate FIB 2 as FIB 1 plus okay so FIB 2 is already one it will return one and so on let's see if I'm able to get the fourth element of Fibonacci series I have made some mistake it seems uh what is the mistake no I actually selected okay so I haven't written function prototype so let me write function prototype and let me tell you something if you write the function before main function you don't even have to write function prototype but it's always a good practice to write function prototype let me save this and let me run it again and the nth element of Fibonacci series and let me do one more thing instead of doing nth I'll simply say percentage 5:06:24 D and I'll simply say so that I can say that the element four of Fibonacci series let me get rid of this percentage and from percentage D from here and let me format it like this so element four of Fibonacci series is three is it true let me check so 1 and 1 then 1 + 1 is 2 then 2 + 1 is three so fourth element is three indeed uh how about let me generate Fibonacci series then 3 + 2 is 5 then 5 + 3 is 8 uh 8 then 8 + 5 is 13 then 13 + 8 is 21 and so on this is a Fibonacci series let me now run this program for n is equal to 8 okay let me write uh run this for n is equal to 8 and eth element is 21 1 2 3 4 5 6 7 8 yes it's right so this way we are able to find the next elements of Fibonacci series using recursion so recursion is a function that calls itself in case you 5:07:25 are still wondering how I got to this equation FIB n minus one is the previous number and FIB n minus 2 is the previous to previous number okay so if we add these two numbers we are going to get the next number and this is the base condition which simply says that the first and the second element is going to be one and since I've already told you that return one will simply stop the F function execution so as soon as a function sees return it will halt there itself it it will not execute any further instruction so in case you are wondering if this is executed I mean if return one is met by the C compiler will this line also get executed or something else which is Below return will get executed or not no it will not get executed so if I'm writing anything below return it's not going to get executed just because return means finish the function call that's it we got the value we needed and we are not going to run this function anymore okay so I hope you were able to understand 5:08:27 this let's now move on to the next question which says what will be the following uh what will the following line produce in a c program so we have to find what will the following line produce in a c program let us assume the value of a as three and let's let's try this out so what I'll do is I'll simply copy this program and uh I'm going to write program number nine which is problem number five and I'll simply add boilerplate code here and also what I'll do is I will uh I will simply type this print F 3 percentage D I'll say percentage D percentage D percentage D and what I'll do is 3% D then back sln back sln back sln now what I'll do is A++ a and A++ 5:09:27 A++ a and A++ so this is how the question uh asks us what will be the value of this so a then Plus+ a then A++ okay so let's start this so initially what will happen is a will get printed for sure so we'll get a four okay we'll get a four and then Plus+ a Plus+ a will initially increment the value of 4 to 5 and then print the value of a which is five but since the value of a is now five a ++ will first print the value of five then increment it to six so now the value of a is six but we are not printing the value of a and anymore so if I comment this out now you must be thinking that this is the final answer I'll show you something if I run this program you'll see that 664 is being printed on the screen and you must be thinking what is happening what is this 5:10:27 and let me tell you something why this happens and also I want to tell you something U which is also very important if you are tackling such questions in an interview okay first of all write WR in such a statement in your C program is not at all recommended because different compilers are going to deal with this particular line in different ways one way to evaluate this was this where we got four five and five as the answer but it turns out that the compiler which is installed on this machine is decoding this as this so we'll we'll uh try to understand why 66 and 4 is the output of this program for this compiler what has this compiler done to get to this output uh and then we'll talk about whether you should answer these question in an interview in this way or not okay okay let's get started so what this compiler is doing is this compiler is basically receiving A++ a and A++ uh as an argument of print F what happens is this 5:11:29 compiler is designed in a way that it evaluates all these three lines from right towards the left okay so A++ will will be evaluated first so A++ will be evaluated first and a will be printed four will be printed then since the value of a is now five A++ means that print value as four and make the value of a as five now when I say plus plus a the value of a becomes six and six is printed because we are incrementing first and then printing so five becomes six and six is printed and when I print a the final value of a is printed which is nothing but six so hence the output but what if you get this question on a whiteboard you should reply that compiler might either choose to um you know either choose to resolve these three lines from right to left or left to right one of the smartest way to solve this problem is you can tell your uh interviewer that I can solve it from 5:12:32 left to right and most of the people are going to solve it left to right so I'm going to solve this right to left and show it to you this way your interviewer will know that you know all these Concepts you understand that a compiler might deal with this particular line in a different way but whenever you are writing a c program never ever use such kind of thing inside a printf statement because different kind of compilers might look add this code in a very different way so I hope that this question helped you a lot and you were able to understand uh intricacies of printf and increment operators the next question says that find a write a recursive function to calculate sum of first and natural numbers recursive function right okay let's write it let's solve this problem this is problem number six program number 10 10or pr06 06 do c let me write the boiler plate code and let me try to see what sum of first 5:13:32 and natural number is it's nothing but 1+ 2+ all the way till n so we'll have n minus one somewhere here and then we'll have n okay this is how sum is calculated okay can I not write sum of N is equal to sum of N - 1 + n can I do this yes definitely I can do this and a recursive function is ready so what I'll do is I'll simply say In Sum n I'll say return sum of nus 1 + n so that's all but I'll have to define a base condition I'll say if n = to 1 what you do is you return one so the moment we start receiving one as the input what will happen is um the moment it becomes sum one we are going to get one as the output let's see if this works I'll say print f um and then 5:14:33 I'll say sum of first five natural numbers is is percentage D and I'll simply say sum five and I'll put a semicolon let's see if this works I'll save this program as well uh the moment I run this okay so I need to write function prototype and also I should write int here my bad uh I should write int here like this and yes this should work now let's see what it gives me so is 1 + 2 + 3 + 4 + 5 15 let's check so 1 + 2 is 3 3 + 3 is 6 and then 6 + 4 is 10 and 10 + 5 is 15 and yes we are right okay let's try to find sum of first three natural numbers and I know for sure that sum of first three natural numb is six so if I run this program you'll see some of first three natural numbers is six so all it takes to solve a problem using recursion is to come up with a recursive 5:15:33 relationship and plug it into the function and that's it you just type the mathematical formula in a very smart way Insight a function with a base condition this is important always use a base condition because if you don't use base condition you you are you are going to deal with a lot of problems your function might keep on running indefinitely and you would definitely not want that okay now the last problem which is write a program using functions to print the following pattern so you have to print first end lines of this pattern what is this pattern by the way by the way so the first line contains or rather first row contains only one star the second row contains three stars the third row contains five stars so I I I'll have to generate the odd number 1 3 5 7 9 and so on and I'll have to print those many stars let's see if I can do it I'll give it a try 11 5:16:34 p07 do c boiler plate code uh I'm I'm going to give it void star and int and I'll say void star intn all the logic will come here and I'll simply call Star four let us say okay so I have to write first n line so definitely I'm going to use a for Loop I'll say in I is equal to Zer I is less than n i ++ and after that what I'll do is I'll simply print a back sln and you must be thinking what I'm doing how I know that I have to print a back sln uh so the thing is all I need to do is I just have to you know print I'll simply write down in uh very plain and simple English I'll say print uh 2 N minus one 5:17:35 stars or rather 2 n + 1 sorry because I'm starting my from zero what does it mean here I'm saying print 2 n + one Stars so whenever you have to write a odd number in the form of n what you do is or let me change it to 2 I + 1 because we are starting I from zero 2 I + 1 Stars means when I when the value of I is zero this will be one when the value of I is 1 this will be three then when the value of I is 2 this will be five so we are able to generate 1 3 5 7 9 using 2 I + 1 I hope you understood that now I'll simply replace this with a loop and I'll simply say for J is equal to Z J is less than 2 * I + 1 what is this 2 * I + 1 this will result into uh 2 I + one star so if I have to print one star this this will be one and then for the next iteration it'll be three then five then seven then N9 and here what I'll do is I'll simply 5:18:37 say print F star semicolon and the moment I run this you'll see that I am able to get the pattern that I wanted so for the value of n is equal 4 I'm getting four lines once again if I were to tell you how this works I'm calling this function here and inside the function what I'm uh doing is I'm having a nested for Loop so a for Loop inside a for Loop what this for Loop is doing is it is generating the value of I all the way from 0 to n minus one okay so so if the value of n is 4 it is generating 0 1 2 and 3 now what this for Loop is doing is it is using those values of I which is 0 1 2 and three and generating one star three star five star Seven Star Nine Star odd number of stars for me okay so how this works is I'll use inj is equal to Z J is doesn't 2 I + 1 2 I + 1 means odd number so when the value of I is zero I get one for 1 I get three 5:19:37 for two I get five five and so on for three I'll get seven if you plug three here you'll get seven so this is the odd number so we are printing Stars odd number of time 2 I + 1 will yield let me write it here 2 I + 1 will yield 1 3 5 7 9 and so on for I is equal to 0 1 2 and so on okay so this is how it works I hope I was able to explain this to you and and you were able to print this star pattern uh yourself so if you get some other star pattern in the future I hope you'll be able to print that so this was all about the star patterns the uh functions recursion we learned a lot of stuff in this chapter I hope you enjoyed this chapter let's now move on to the next chapter and we'll understand more concepts of C let's move on to chapter 6 now now before we move on to the next chapter I have a surprise for you we are 5:20:38 going to to solve this project and this is going to be our first game if you're are new to programming this is going to be the first ever game of your life so congratulations once you complete this game you'll be able to uh showcase it to your friends or maybe you can put it inside your resume let's see what this question has to say so this project number one number guessing game says that this is going to be fun indeed we will write a program that generates a random number and ask the player to guess it so what we going to do is we are going to generate a random number and then we'll ask the user to guess it so let us say I'm generating a random number between 1 to 100 and I ask you to guess it let us say the number is 50 uh you you might guess 60 or 80 or 90 the actual number is 50 now what will happen is if the player's guess is higher than the actual number so let us say you guess 66 the number was 50 what I'll do is I'll tell you whether your guess was greater than my number or lesser than my number this will help you guess it correctly the next time so if the number 5:21:39 is 50 and you said 66 so next time you will not say 67 or 70 for that matter because you know the number is lesser than 66 so you might give it a try and say 45 now I'll tell you that no the number is not lesser than 45 it's actually greater than 45 so you'll say okay so it's between 45 and 66 now so you might guess 49 I'll again tell you that you need to increase it and even eventually you'll reach that number you'll tell me 5050 and yes 50 will be the number so the number of guesses that you took will tell me your ability to guess a number correctly and the number of guesses you took will be your score now if somebody else has guessed it in lesser number of guess guesses he'll be the winner okay I hope that you understood what this problem is about the fun is not in the game itself to be honest it is in the code it is uh the fact that you have created the 5:22:40 game and you are the creator of the game that you are playing that feeling that feeling of you know the creator of the game that you are playing is going to be absolutely brilliant believe me so if the player guesses the number is higher than the actual number the program will display lower number please similarly if the user's guess is low the program is going to print higher number please when the user guesses the number correctly the program displays the number of guesses the player used to arrive at the guess okay so the hint ISS use Loops use a random number generator now you might say har you didn't tell me about random number generator so what we are going to do is we are going to talk about random number generator. c how do you generate a random number in C so let me put in a boiler plate code for you and now I'll discuss about how to generate a random number in C so in order to Generate random number in C you need to include couple of libraries so the first one that we are going to include is STD Li and the other one is time. Ed now what 5:23:41 I'm going to do is I'm going to say it number this is the number that I'm going to generate randomly and I'm going to use a function called sren and sren is going to take an unsigned integer and since I want a random integer I'll take the system time so Time Zero is a function from time. H which is going to give me a random number time the current time is going to be very random okay so why do we do this why do we say s r Time Zero because this is the seed this is the seed of my random number generator so if the seed is same I'm going to generate exactly the same random number so let me show you how to generate a random number first and then I'll come back to this asent call so all you need to do is you need to say Rand per 100 + 1 and I'll tell you the logic for this as well let me print the number for you first you'll say the number is percentage D and I'll say number okay let me run this program and show you that this actually generates a random number okay and this is going to 5:24:42 generate a random number from 1 to 100 let me tell you since the number is 29 49 so every time you run this program you're going to get a different number between one and 100 so between this number and this number you are going to get a number okay if you if you talk about Rand function what does Rand function do you can always Google search you can say Rand in C and uh Rand is actually a function it is used us to Generate random number in the range zero to Rand Max Z is included Rand Max is not included okay so this is how random function works okay so why did we use this sren time Z let me show you what happens if I put a constant value inside sren so if I put a constant value inside sren what happens is 42 is generated if I program uh if I run this program once again once again 42 is generated if I run this program once again 42 is generated so if you put a constant inside Sr this is going to be the seed of the random number generator and for the same seed the number generated will always be the same I don't want that I 5:25:44 want something random so that I can make this game fun so something random is a system time I can use the system time I can use time zero this will give me the system time as an integer and I can use this as my random number seat I hope the logic is clear to all of you so I can generate a random number starting from 1 to 100 okay so let us see how we can use this particular logic to program our game so let's start our game so I'll simply say game. C I'll put the boiler plate code and the very first thing that I'm going to do is I'm going to generate a random number let's do one thing let's create a function I can create a function to generate a random number but let's not do that let's keep the program simple so I'll simply say in number then I'll simply say time zero srand this is something that I'll have to do now this is the number that I have generated okay I'll create another variable I'll say guess I'll create another variable 5:26:46 which will be number of guesses n guesses and I'll initialize n guesses with one okay so initially the number of guess that you're going to take no matter what will be one so you need at least one guess to guess the number correctly okay you cannot guess it in zero guesses obviously what I'll do is I'll use a dowi loop and why am I using doy Loop because I'm going give you one chance for sure okay so this is a very good use case of do wide Loop I'll simply say guess the number I'll say guess the number between 1 to 100 and yes back sln is important to format and I'll use scanf percentage D and I'll say am percent guess because this is going to be your guess okay now what will happen is I'll simply see if your guess is greater than the number so there are only three cases possible your guess will be either greater than the number it might be lesser than the number or it might be equal to the number so in case 5:27:48 your guess is greater than the number I'll say lower number please and what this will do is this will tell you that you need to you you need to punch in a lower number I'll use lsf I'll say LF the number is U lesser I'll say greater number please I'll say greater number please okay now if both of these scenarios are not true what will happen is this particular L Cod code is going to run and what it will do is print F the spelling is wrong print F and let me zoom in a bit for you I'll say yay you guessed you got it okay I'll say yeah you got it in percentage D attempts or let me let me do something like this I'll say you took percentage D attempts how many attempts n guesses and every time you are guessing what I'll do 5:28:49 is I'll increment n guesses so I'll say n guesses Plus+ okay so this is how I'll increment and guesses and this while loop will keep on running until the guess is not equal to number the moment guess becomes equal to the number I want to get out of this loop I hope the logic is very clear for you if it is not just pause the video and look at the logic you will definitely get it this is super simple logic let's try to play this game and see if this works let me save this program and close it oops uh let me save this program and close it and let me run this code and it gives me some errors I think I forgot to include the these header files the ones that I included in this one I'll replace all these header files and I'll I'll once again run this program from scratch let me close this random number generator do c and this time it compiles without any warnings so guess the number so I'll say 67 is it 67 5:29:50 no it says lower number please 40 okay lower number 23 lower number um let me say 12 greater number 19 oh wow you took it in one attempts which is wrong obviously I didn't I I I didn't guess it in uh one attempt so what I need to do is I need to put this out outside this else so initially it was inside the else I need to put n guesses Plus+ outside else another thing I need to do is I need to format my program such that it actually prints a new line after these lines because the program looks a bit ugly let's run this code once again and this time hopefully everything will be right 65 greater number please okay 77 lower number please okay okay that's interesting 70 greater number please 73 lower number please is it 72 yeah I got it in five attempts 1 2 3 4 and five so I got it in five attempts yes correct 5:30:51 so my first attempt was this then second was this third fourth fifth I got it in five attempts this game was fun let me know how is this game in the comments everybody should write a comment below and tell me how was your first ever game if this was your first ever game even if it was your first ever C programming game right congratulations for all of us in the comments it's going to bring up the energy please do that please go ahead and drop in your comment do your bit let's now continue this course and move on to the next chapter so now is the time that we discussed one of the most confused topics in the C programming language and the name of the topic is pointers and I have a dedicated chapter to this topic why because this is one of the most confused topic and we are going to see how easy the concept of pointers is and you're going to appreciate it a lot so a pointer is a variable which stores the address of another variable so far we have looked into int we have looked into float we also looked into double we also looked 5:31:52 into car which was able to store character variables now there is another type of variable which does nothing but it simply stores the address of another variable let's try to understand this okay so let us this is a variable called I what is the value inside this I it's 72 so 72 is the value stored in I I hope so far it is clear what i is I is a variable let me also open VSS code and I want to open uh this particular folder in vs code so I'll simply right click and click on open with code and I'm going to create a program called pointer. C okay I'll put in the boilerplate code here and I'll simply write in I I so the moment I say int I and the value is 72 so I'll say in I is equal to 72 what happens is in my main memory in my ram in my Random Access Memory what happens is this particular variable is 5:32:54 stored inside some address in the memory so memory has some address just like you have a locality that locality has different houses each house has some address in a very similar way we have main memory and inside the main memory we have a lot of blocks each block has its own address try to understand this very very carefully try to listen to me very very carefully because this concept is really very important and if you listen to me I can guarantee that you going to understand so the moment I say inai is equal to 72 four bytes in Ram are reserved four bytes in memory are reserved and I will start from some address and I'll keep on going till four bytes are covered in memory and those four bytes will be used to store this integer let me try to explain I'll simply say RAM memory and I simply need some images so that uh so this is how it physically looks like but 5:33:55 I want to show you some schematic representation of memory so that I can explain you how u a variable is stored inside a memory so let me let me Google how variable is stored okay and we should get something yes uh okay so here here here is an image that I'm going to use to explain this to you so this image is a bit blurred so we we can see this image by the way so these are nothing but blocks in the memories each block can store either zero or one and we have a lot of blocks in our memory now what happens is one block is one bit so one bit can store either zero or one now 1 2 3 3 4 5 6 7 8 eight bits will form a bite eight bits will form a bite and 1224 bytes will actually form a megabyte which is called an MB you must have heard about MB 12 Mb 30 MB 512 MB 5:34:57 and 1024 MB is going to be 1 GB we we all use memory we all use hard disk but what happens is when we have so much of memory we can store a lot of things inside the memory but at the very basic level what a memory can store is simply a zero or a one so let us say I say in I is equal to say u in this case I said I is equal to 72 I am storing an integer and in order to store an integer I need four bytes integer is four bytes in some of the compilers it is even two bytes but let's look at an integer as four bytes for now okay so if integer is 4 bytes how much memory do I need to actually store this integer I need 1 2 3 4 I need these four rows to store an integer because one row is nothing but one bite and I need four bytes to store an integer so I need this much of Space 1 2 3 4 I need 2881 all the way till 5:36:00 2884 to store this integer okay now how will this integer be stored in the form of zeros and one is is another question I'm not not going to discuss that question uh just yet what we are trying to understand here is how this variable is going to store in memory now whenever we store a variable in a memory there will be some address of the memory where that variable has been stored so let us say in this case 2881 is the starting address where this integer is going to be stored I can say that this integer is stored at 2880 you can see that in I is marked 1 2 3 four bytes are marked and this int I will start from 2880 if you look at this image very carefully uh I is a variable which starts at 2880 and takes four bytes from there so 2881 is occupied 2882 is occupied 2883 is also occupied you can see that in I has been marked and these four rows have been marked which simply denotes that these 5:37:01 four rows are holding this integer how are these holding this integer that is a question for another day uh um just understand that zeros and ones can be used to store an integer so 72 is stored in this space okay now if I say what is the address of this integer what is the address of I it's 87994 in this case it is 2880 you can see that address of I is 2880 that depends upon how compiler is storing this variable it might start the address from 2888 it might start the address from 1788 22994 it can be anything we don't have to worry about it you will get different types of addresses uh in fact different address locations on different computers as and when you run the program so in order to see the address we use am percent operator okay now I know that this might sound really very complicated to some of the people who are just starting their programming Journey but 5:38:01 believe me you are going to understand each and everything as we move ahead towards this pointers joury okay so for now what we are doing is we are saying that I is a variable I is simply a variable it stores 72 it's an integer variable now what I'm saying is the address of I is 87994 this is for the fact okay 87994 is the address now I'm creating another variable to store this value 87994 I can create a variable to store this value 87994 is a value which can be stored in another variable will it be an integer no it will not be an integer you might argue that why why can it not be an integer but when we are storing addresses in C we simply use pointers pointer is a special kind of variable which stores address of some other variable so I holds 72 and J holds 87994 I is an integer J is an integer 5:39:02 pointer J is an integer pointer this is something that you need to understand listen to me very very carefully this is an area where you cannot sleep you you will have to listen to me please rewind if you were sleeping while I was talking about pointers and listen to me very very carefully what pointer is so I is an integer variable J is an integer pointer integer pointer means a pointer which stores the address of an integer so J is an integer pointer so J will hold address of integers only integers okay so I can simply assign 87 994 to J and I can even assign uh address of some other integer to J and so on okay I hope you understood this much now J is a pointer and J points to I so we simply say in C language terminology that J points to I when I say J points to I it simply means that J is holding the address of I so I is holding 72 J is pointing to I pointing to I means it's holding the address of I okay I hope 5:40:04 that this is clear so far and I was able to understand I also Creed this visual to better explain you what J points to I means you you'll be able to remember all these things uh if I create these funny visuals for you okay now let's talk about what is address of Operator %c Operator okay is this the first time we using ENT operator no we have already seen ENT operator where we looked into this ENT operator while we were talking about scan a function we discussed there that in order for scanf to work properly we need to supply the address of the variable we want to take as input from the user so we use am perent in order to take the address of a variable and Supply to scanf but let's see what this amp percent means amp percent will simply return the address of the variable So when you say in I is equal to 72 I will be located somewhere in the memory and it will hold the value 72 and 5:41:07 wherever this I is in the memory m% I will tell me this value in this case m% I will simply return 2880 I hope that this is very clear so in order to in order to understand or rather in order to look into the address of I the address of I is percentage U and what is this percentage U yes youve guessed it right it's a special format specifier for pointers okay so I'll simply say am per I and I'll get the address of I okay let me also make this TS capital and run this program for you the moment I run this program you'll see the address of I is this so this is the address in memory which was provided to uh this particular variable I okay so I was uh stored here starting from here and it will occupy four bytes or two bytes depending upon the architect if you are uh in some 5:42:09 other computer which uses some different architecture integer might be two bytes there okay so this is something that you need to remember you you you don't have to memorize that integer is four bytes and it cannot be two bytes it can be two bytes it can be 4 bytes that depends upon the architecture okay now coming back to the notes the address of operator is used to obtain the address of a given variable so address of operator address off is an operator which is nothing but am percent this am percent is used to obtain the address of a given variable so if I was to obtain the address of a given variable I'll use amp person just like this okay now for different variables this will give different addresses so if I have in J is equal to something let me say in J is equal to 723 and percentage U and I'll say m person J the address of I and J is this and this so the address of I is 5:43:10 this and address of j is this so this is how address of I and J can be returned using a c program okay so far so good I hope you understood uh how we can take the address of a given variable and print it on the console format specifier for printing pointer addresses percentage you we have seen format specifier for integers which was percentage D we saw format specifiers for floating Point numbers perc for we saw it for double we saw it for a lot of other variables you can even Google for format specifiers but percentage U is something that you need to remember just like you remember percentage D for integers you need to remember percentage U for the pointers now just like we saw ambers and operator we look into a star operator and this is something that might confuse you but I'm going to make it as simple as possible for you and if you listen to me very very carefully I can give you a guarantee 100% guarantee that you will understand this because when I started 5:44:11 learning this I was very confused so I can tell you that how your confusion can be removed and what are the things that will confuse you listen to me very carefully the value at address or Star operator is used to obtain the value present at a given memory address so it's kind of an inverse of ERS operator let me tell you how so if you have a variable I and you have the value stored inside the variable 72 and you use am% operator you'll get to address what if I give you the address and I want you to tell me what is the value stored at this address so if I hand you 87994 and then I ask you tell me the value stored inside this address how will you tell me which value is stored inside this address you might say I don't know I don't know I cannot look into the memory I cannot open the screws of my laptop and look into the memory and read which values there you cannot read but you can always write a program 5:45:12 and I'll show you how that is done so just like we have ERS operator to get the address we have star operator to decode that address and get the value at that address so if I enclose this with with with a star let me show you so if I say star of m% of I and star of m% of J let me show you something star means value at address of I value at address of I let us say you are living alone at your house inside your room and let us say your house number is 24 what if I say house number of Harry and the person inside it so if I ask you person inside house number 24 of Harry what will happen who will be that person it will be Harry okay so you know a very similar way if I say value at address of I it will be I what is the value 72 in in a 5:46:13 very similar way value at address of j will be J so let me change this value of I andj is I'll say percentage D percentage D okay so value of I andj will be printed using this definitely you will never use this kind of concept to you know print the value of I you will simply use percentage D and I to print I but just in case you are given a question and you are asked what will this Line Print in a c program you should be able to answer okay so if I run this program and if I show you the value of I and J is 72 and 723 let me write a back slash and and beyy this so if I save this program and if I run this you'll see the value of I andj 72 and 723 and the address 220 and 216 Okay so so these are the addresses which were allocated to I and J and 72 and 723 were the values so I hope you understood am 5:47:15 percent and star operators M percent is address of and star means value at okay so this this was pretty simple so far and if you listen to me carefully definitely you should have understood how these things work okay so the image that we were talking about uh I I I clicked on this image and I opened this uh I I don't know which link is this where is this coming from but I gave it a red and it was actually good and it will make you understand how computer memory works so just in case you want to Deep dive into computer memory and look into how memory works I think you can use this resource because I I gave it a read and uh it it really explains it well so let me paste this link for you here I'll write a comment here you can click here and you can go to this particular page and yes it is good uh now we'll move on uh further and talk about the further Concepts okay so now that you know what ERS operator is what star operator is we look into how to 5:48:16 declare a pointer how is a pointer declared so a pointer is declared using the following synx so this is a centax which we can use to declare a pointer in Star J so do not confuse a star with this value at address operator when I say in Star J I'm simply saying that J is a point poter that's it now this might be very confusing for you because you you'll tell me that hey harry you just told me that star means that decode this address and give me the value of this and you are saying that inst starj can be used to create pointers so yes let me show you something creating pointers let me add boiler plate code let us say I want to create a variable which is going to store address of an integer so if I say in I is equal to 72 now let us say I want to create a variable which will store the address of I so if I do something like this in J is equal to m% i j is an integer I cannot 5:49:21 store address as an integer in C in order to store the address I'll have to create a special kind of variable which is a pointer variable and in order to mark this J as a pointer when I Mark J as pointer I I'm simply saying that J Will now store address of some integer when I say int J I'm saying that J Will store an integer when I say int star J I'm saying that J Will store address of an integer so if I say int star J is equal to ENT I it simply means that J is a variable which holds the address of I now I can do something like this now I can say that the value of I is percentage D and what I can do is I can say star J okay so the moment I say star J what it means is it means that decode the value at address J okay so if I run this program I'll get the value of I is 72 let me show you another example and also I'll 5:50:21 add a back so that things look good uh if I say value or other address of I is percentage U and I simply say J so J is nothing but address of I so when I say int star J is equal to to m% I it simply means that J will now hold address of I now this might seem really very complicated to some of the people who try to map uh you know the the use of the star operator with Declaration of a pointer this is a pointer declaration and star when used with a variable simply means what is the value at this address okay so this is something that you nearly need to wrap around your head now the address of I is this the value of I is this what else is the value of I I can simply write I this will also give me the value of i j will give me the address of i m percent I will give me the address of I so I can say percentage u m percent I this will give me the address of I I can say percentage U and 5:51:24 directly J since I have used a pointer variable and I've said in start J is equal to mcent I this will give me the address of I this will give me the value of I because what I'm doing is I'm using value at operator and I'm simply using J and I can also say the value of I is percentage di this will also give me the value of I now if you come back to the notes you'll find out that I have told you how to declare a pointer here so a pointer is declared using the following syntaxes what I've written here int star J declare a variable J of type in pointer you can simply declare an integer pointer by typing in Star J So when you say in star J J will be an integer pointer now you can use J is equal to m i to assign the address of I inside this J we have we have already seen this in the program and once you run this program you'll understand a lot of things I've written those things in the notes as well so store address of I in J using this just like pointer of 5:52:26 type integer we also have pointers of type character loow double you can have pointers to any data type now we'll also learn about custom data types in later chapters and you can even have pointers to those custom data types but let's start easy and let's look into integer pointer character pointers and Float pointer so when I say inst star chptr this can be any name I'm using chptr this is pointer to an integer if I say Car Star chpd it's pointed to a character because I have written car here now if I say float star chpd it's pointer to a float because I've written a float here okay in Star chpd C star ch PDR float star chpd now what happens is a lot of students they get confused with the name of the variable you should never look and rely on the name of the variable so if the variable name says amount of sugar in coffee that doesn't mean that the amount of sugar that that person has drank in his coffee in the morning uh 5:53:28 will be same as the value of that variable people will write anything in their programs and it's your duty to verify whether a given point poter is a character pointer integer pointer or a float pointer merely naming a variable as chptr doesn't make it a character pointer what exactly that variable is matters okay so I've written the same thing here I'm I'm saying here that although it's a good practice to use meaningful variable names we should be very careful while reading and working on programs from fellow programmers so if you are working uh on programs from your fellow programmers who have written some code and you want to make sure that whatever they have written is right or not you need to verify from the ground truth and not from the name of the variable in this case if you say that this is a character pointer it will be wrong because it's an integer pointer I have used in Star simply naming a variable as chptr doesn't make it a character simply naming a variable as character one doesn't make it a character you should verify whether it's 5:54:28 an integer whether it's an integer pointer or some other type of variable so I hope you got this point now I have a program to demonstrate pointers I have already demonstrated you pointers using this program but I have written this program in the notes as well let's try to see what this program is doing so int I is equal to 8 simply assigns the value 8 to this variable I and when I say int star J I'm simply creating a pointer which is an integer pointer and the name of that variable is J now I can assign address of an integer inside this J by doing J is equal to m i so this J will now hold the address of this this variable which is I okay so what will happen if I say print F address I is equal to percentage u m percent I percentage you m percent I will print the address of I in a similar way since J stores the address of I what we can do is we can simply say address of I is equal to percentage U Back sln and J 5:55:28 what this will do is this will also print the same thing as this this will print the address of I but what if I print the address of j now J is also a variable it's it's a pointer variable what is J J is something which stores the address of I does it has its own address yes it does it does has its own address now this is something that gets confusing to some of the people so let us say I have this variable 72 it is stored at address 87994 if I say address of I this is the address of I but if I create another variable J and I store the address of I inside this variable this in itself is a variable which will have some other address so address of address also exists so this is a variable I it has this address this is a variable J which is a pointer which means that J is made to store the address of integers this will also has its own address which is 5:56:31 87998 okay so in a very similar way when I say m per J this will be some other another address and it will be address of J's J is a variable at the end of the day J is a variable it is a pointer variable which stores the address of an integer variable but at the end of the day it in itself is a variable so when I say something like this I'm perc J this will print address of j in a very similar way I can print value of I just like the traditional way I can also say m percent I and wrap it uh and put a star operator which will decode the value by using the address and this will also print the value of I in a very similar way this will also print the value of I because J is nothing but amp percent J is nothing but address of I and if I decode this address if I find the value at this address this will give me the value of I so this program will return this this is the address of I this is the address of I this is the address of j which is storing the address of I but at the end of the day J is a variable it will have some address 5:57:32 so this is the address of j and the value of i is8 as we have already seen so type this program inside your Visual Studio code and see um C see this output okay now uh what we'll do is uh we'll talk about Pointer to a pointer so this program sums it all you if you understand it you have got the idea of pointers so make sure that you understand this program because I don't think you need anything else to understand pointers in case you haven't understood what pointers are you need to watch this chapter once again it means you are not paying attention if you have paid attention and you've listened to me carefully and if you have coded along with me I can give you 120% guarantee that you will understand what pointers is this is the amount of confidence that I have in myself now pointer to a pointer just like J is pointing to I or storing the address of I we can have another variable K which can further store the address of j what will be the type of K tell me it will be pointer to 5:58:33 an integer pointer okay so let's let let's try to understand this and in order to create this pointer to an integer pointer I'll have to put double star so in Star J simply means that J is a pointer to integer in star star J or rather in star star k will mean that K will store address of an integer pointer okay and we can take this chain as long as possible but in most of the cases you will not see pointer to a pointer you'll definitely see pointers but even pointer to a pointers are very rare but we need to understand this let us say I created a variable I I stored 72 I has a value 72 this I will hold some address in memory what is the type of I it's an integer I've done nothing so far I've simply created an integer I and assigned a value 72 to it now if I say in Star J and if I store the address of I inside J what is the type of J it's in start which means pointer to an integer this 5:59:36 variable this J variable stores address of an integer and it will definitely have its own address in memory since it storing in memory it will have some address in memory what is that address let us assume that that address is 87998 now what I want to do is I want to go a step further and I want to create a variable K which will further store the address of j what will be the type of this variable it will be in star star in star star means pointer to a pointer of an integer so pointer to an integer pointer so this is pointer to a pointer okay so K will store address of something which was storing address of an integer okay so we simply denote it as a in triple sorry in double star and this in itself will also have some address and we can have another variable K uh sorry L which will store this address and we'll have to 6:00:37 write in Triple Star but again that is very rare and you don't have to you know think that much we can go even further one level and create a variable L of type in Triple Star to store the address of K we mostly use in star in in most of the cases you will encounter pointers even pointer to a pointer is sometimes um you can see in real world programs but it's very rare in Triple Star is really very rare okay but again you need to understand that pointer is not nothing but a variable it's simply a variable which stores address of another variable I hope you understood what pointer is now we going to look into the two types of function calls which is call by value and call by reference this is something that you'll definitely need when you are programming in c and this is something that you're going to encounter a lot of times even if you're writing a c program with even a little bit of complexity you are going to look into these types of function calls so it gets really very important for us to understand what these types of function calls are so based on the way we pass 6:01:38 arguments to the function function calls are of two types call by value and call by reference you must be thinking we have already looked into functions and function calls but let me tell you something you can either pass the value of arguments or you can pass the address of the arguments let me tell you what it means let's talk about call by value so 03 uncore uhu it's called call by value do C call by value. C let me put the boiler plate code and what I'm going to do right now is I'm going to say in sum in int and this is a function that's going to return an integer and it's going to take two integers so all I need to do here is I'll say int a and then int B and then I'll say return a + b and this is my function pretty straightforward okay what I'll do do is I'll say print F I'll simply say the value of A+ B is 6:02:45 and I'll say percentage D and what is my percentage D it's sum say 3 comma 6 and if I run this program you can see the value of a plus b is 9 nothing surprising so far what if I have two variables here I'll say int X is = 5 in y is = 4 and I'll simply say x comma y now if I do something like this and if I run this you can see that the value of a + b is 9 so it's simply adding five and four so how how does this work let's try to understand how this works so whenever we are giving a function called like this Su X comma Y what happens is that X and Y both of these values they are passed to this particular function sum so X is is five and Y is four so five and four are copied into a and b I'll repeat this for you the values X Y 6:03:45 which are nothing but five and four are copied into a and b so a becomes five and uh B becomes four okay so now this function will return return 5 + 4 which is 9 so basically what happens is when when I say in sum in a in b a will be equal to this value and B will be equal to this value and a will simply be assigned whatever the value of x is B will simply be assigned whatever the value of y is so we are creating a copy of X and Y so will X and Y be changed what if I say uh the value of X and Y is uh X and Y is let me make it X and Y I I wrote it as a and b but let me write it X and Y is percentage D and once again I'll say percentage D and I'll simply say X comma y because I want to I want to print the values of X and Y so I'll save this program and I'll also use back sln so that I can see a new line after 6:04:45 this so the moment I run this you can see the value of x + 5 is 9 x + y is 9 and the value of X and Y is 5 and 4 okay now what if I do something like this I'll say a is equal to say 56 can I do something like this let's try to run this program when I try to run this program you can see the value of x + y is 60 but the value of X and Y is 5 and 4 what is happening in this program let's try to understand let's try to dissect this program you'll see that the value of x is five here and value of y is 4 here and when I say the value of x + y is percentage D basically 5 and four are copied into a and b so a is five and B is four but here I've changed the value of a so now a is 56 and B is 4 initially a was 5 b was 4 but now a is changed to 56 B is unchanged which is 4 so 56 + 4 will be 60 but since the 6:05:48 values X and Y were copied the copy of X and Y were changed so X and Y will remain unchanged so X and Y will be nothing but 5 and four because I cannot change one function's value from other function directly I can do this directly I can't say a is equal to 56 and get this reflected here that doesn't happen but this is the normal scenario this is called call by Valu we have already seen this scenario we were just understanding how the scenario works so if you come back to the nodes you'll see based on the way we pass arguments to the function function calls are of two types call by value and call by reference call by value means sending the values of argument call by reference means you are sending the address itself now let's talk about this one what happens when I'm sending the address itself now let's talk about call by value here the value of the arguments are passed to the function we have seen this this is a normal scenario you can consider this example when I say in C is equal to sum 3A 4 uh 3 is copied as X 6:06:49 and Y is copied to sorry four is copied to y u and simply what happens is uh the sum is defined as sum in a in B these two values are copied into a and b and the values three and force are copied to A and B now even if we change a and b nothing happens to the variables X and Y we have seen this we have seen this here nothing happens to X and Y okay why why because I cannot change the copy this A and B are different variables X and Y are different variables when I say sum X comma y the value of x will be copied to a and value of y will be copied to B and now if I change a and b it has nothing to do with X and Y I hope you got this one now uh in C we you usually make a call by value so far we have made call by value now listen to me very carefully call by reference this is something new this is something that we have not seen so I'm going to write a program to describe call by reference so let me say something like this I'll say uh call by reference. 6:07:50 C I'll put the boiler plate code and what I'll do is I'll simply copy this program and I'll paste it now in call by reference listen to me very carefully and then we'll go back to the notes and see what is there in the notes instead of sending the values we send the address of the values listen to me very carefully instead of send sending the values we send the address so if I do something like this m perc X and M perc y if I do something like this and accept these addresses here so I can say in Star a now a is an address B is an address because we are passing an address so a this is an address this is an address so this has to be be an address this has to be an address and what what type of address integer address so it can be an integer pointer and uh this is a is an integer address so integer address will be passed into this variable which is an integer 6:08:50 pointer is this valid definitely this is valid you you can definitely um use this kind of syntax to pass the address of X and Y variables okay so far so good so far so good now in order to change the value of a what will I do a is an address a is an address so I'll decode this value I'll say star a is equal to 56 I'll say value at address of a is 56 and I'll simply say value of a plus value of B okay now the sum function will definitely work but my question to you is will the value of X and Y change a very simple question in front of you and then we'll try to see what what happens and why this thing happens okay I want you to write down comments and tell me what will happen will the value of X and Y be changed if you have commented uh well and good if you said that it will not be changed listen to me very carefully you are wrong it will be changed the value 6:09:52 of X and Y will now be 56 and 4 how come this function was able to change the value of x we'll see let me run this program for you and show you that actually the value will be change so it is saying conflicting types for some okay I'll have to say in Star and star here my bad I'll have to pass um the pointers in the Prototype and you can see the value of x + y is 60 and now the value of X and Y is 56 and 4 so the value of x actually changes why does this happen so let me try to explain this to you using a whiteboard okay so let me show you something so let us say this is the main memory of my computer this is the main memory of my computer and let us say this red region this red region is variable X so this region contains variable X okay and let us say this green region this contains variable y okay now let us say the address of this is one I'm just assuming address to be one for Simplicity it's not going to 6:10:53 look like this but let's assume the address is one here and address is two for this one so the address of X is one address of Y is 1 and the value of x is what it was uh me check five and Y was four so this was five and this was four now what happens is if I pass these addresses if I say one and two if I say take one and two and somebody says that change the value at one change the value at two this is my main memory I have one main memory I have the main memory of my computer if somebody goes to that main memory and changes the variables it will be reflected everywhere and this is what happens in call by reference instead of copying the value when I'm using call by value I'm simply copying these values I'm saying whatever is there in five and four copy it in A and B so I'm simply saying a will also be five b will also be four and now if you change these values these values these X and Y will 6:11:54 will be unchanged but in call by reference what we are doing is we are actually taking the address of this variable itself and we are decoding this address and saying that the value at this this address must be changed and when we are doing something like that then definitely definitely this particular program will be able to change the value of X and Y now let's come back to notes and try to understand this even further so call by reference here the address of the variables is passed to the function as argument so arguments are passed before we were passing the values but now we are passing the address and now that we have the address we can alter the value at that address and value at a given address will be reflected everywhere because that is the physical value of that variable okay so when you say value at this address it it has to be changed because we are taking the address of variable X in main memory and we are 6:12:54 changing the value at that address now since the addresses are pass to the function the function can now modify the value of a variable in calling function using ASX and ERS operators I've given an example here I've shown you how this works and this function is capable of swapping values passed to it and this is a typical function using pointers which is used to swap two variables so let me show you this logic and uh this will definitely help you understand call by reference more clearly so I'll say swap using call by reference so I'm saying swap using CBR CBR means call by reference reference remember this okay so I'll put a boiler plate code here and I'll simply say void Swap and swap will simply put the value of uh X into Y and Y into X let's see how and I'll Define my function somewhere here and also I don't 6:13:56 need X and Y here this is a prototype I don't need X and Y so I'll simply do this now I'll simply say int a is equal to 5 and B isal to 7 uh values sorry print f u I'll say value of a and b before swap um they were percentage D and percentage D and I'll simply say a comma B and then I'll do the same thing after the swap what happens after the swap uh so let me put back sln here and I'll simply run swap I'll say swap a comma B so let's see the first scenario here when I say swap A and B what I can do is there is a very simple swapping logic let me tell you I'll simply create a temporary variable I'll say the value at a but the value at temp will be equal to B the value of sorry the value of temp will be equal to a the value of a will 6:14:59 be equal to B and the value of B will be equal to Temp so what will this do this will simply create a temporary variable and store the value of a so at this line the value of a is equal to uh 5 the value of temp is equal to 5 and the value of B is equal to 7 okay so I'm going to do the dry run of this program for you now when I say A is equal to B basically I'm assigning the value of B into a so the value of a is 7 temp + 5 and b is 7 at this line now the moment the control comes to this line the line number 18 I'm saying B is equal to Temp so what is there in the temp will come into B and you can see the value of B is five now so using this what I've done is the a was five b was seven I made a as 7 B as five so this is a typical logic to swap two integers but there is a catch here there is a catch here let me show you what that catches let me print these 6:16:01 values right after this function runs and right before this function runs so when I talk about this function inside this function I'll be able to swap so let me show you I'll I'll comment these two lines I'm simply running swap a comma B and I'm trying to swap it inside the function so when I look at these two variables from inside this function the variables will be swapped let me show you um it says b is Undeclared let me see okay so I need to use a and b here I wrote X and Y um okay now it should work so the value of a and b before the swap uh okay before the swap before the swap will be uh 5 and 7 and after the swap it will be 7even and five okay so when I look at these two variables from inside the function I was able to swap the values but what will happen if I look at these two values from from the main function in the main function the value 6:17:02 of a and b will remain unchanged because we are passing the values of A and B by value when we pass by value a copy of these two variables will be made inside this function and the values of A and B inside the main function will remain unchanged also note that I have used a and b in the main function as well and I've used the same nomenclature of these variables in the swap function as well and it's totally okay to do that swap will maintain a separate copy of A and B and Main will maintain a separate copy of A and B so if I run this program you can you can see the value of a and b before swap 5 and 7 after swap 5 and 7 it doesn't swap the values A and B so what to do if I want to swap the values A and B in the main function I will have to pass the addresses of A and B addresses of these two variables literally I'll have to pass address of this particular variable and then I'll have to pass address of this particular variable and then what will happen is I'll be able to change these two variables let me show you let me show 6:18:02 you how this can be done okay so what I'll do is I'll accept pointers inside my swap function and I'll simply say inst star a and star B so A and B are now addresses now that A and B are addresses I'll have to use the values by using the SX operator which is nothing but value at address operator so value at this particular address will be star a value of B will be star B value of U this particular will be uh let let me call it aptr so that you are easily able to understand that this particular a PTR is the pointer to this a okay it'll be easy for you you can name the variables whatever you want but I'm just trying to make things easy for you to understand so I'll also make it B PTR B PTR and let me show you that this changes the values um A and B to b and a so I've said instar it said instar but 6:19:03 argument is of type in um install bbtr okay so I'll have to say m perc a m perc B I'll have to pass the addresses of A and B and now this will swap the values no surprises whatsoever so five and seven 7 and five okay why why did this happened because we passed the address of the variable so when we pass the address of the variable what happens is we can find the value of that variable using star operator so this particular a can can be called by using star a this address is passed to swap and not the exact value is copied to swap previously we were copying the value of a to swap now we are copying the address but when we use Asis operator Asis at that address will literally be this variable and it will be changed okay so this is how call by reference works I hope that you are able to understand how call by reference works and how these things are used in C programming language so if I come back to the notes I've written the 6:20:05 same thing that function is capable of swapping the values passed to it because we are now using pointers we are now using the addresses and we are decoding those addresses to find the value at those addresses and we are altering those values at that address so we are literally able to change the variables of the main function so I hope that you were able to understand all this this finishes a chapter number six I hope you had fun this was one of the most important chapters of the C programming language I hope that you were able to understand and even if you have to you know watch this chapter more than once feel free to do that it's worth it watch it once more and make sure you read the notes more than once because these are the concepts that will definitely see these concepts are famous for being difficult but actually they are not difficult because if you understood all the things that I taught you all the things that I told you all the concepts of am person operator star operator you'll be able to understand it's not like this is something that that is 6:21:06 difficult for people to understand it's just that it takes time if you don't have the right resource but I hope that I was able to explain this to you pretty well so we'll now move on to chapter six practice set and solve some questions let's move on to chapter 6 practice set now all right so now that we have some concept of pointers in our head let's try to solve some of the questions uh on pointers so the very first problem of this chapter six practice set says that write a program to print the address of a variable use this address to get the value of that variable so this is a pretty simple program let me show you and let me code this for you so I'll simply say pr01 doc I'll put in the boilerplate code and what I'll do is I'll simply say in I is equal to 3 and what I'll do is I'll simply use print F and then I'll simply use the address of I is and I'll use personent U and I'll say m% I 6:22:07 because we know that the m% operator is going to give us the address of I okay so let's try to run this so This prints the address of i as this so now that we have the address of I we can actually find the value of I so I can say value of I is percentage D and what I can do is I can simply say star % I and what this will do is it will find out the value at address % I which is value at the address of I which is nothing but the value of I so it's going to print three which is why I have used a percentage D here so I'll save this program I'll run this program and you can see the value of I is three and also just to format a programs nicely what we'll do is we'll use these back slns and um let me come back to the practice set so this problem is done pretty 6:23:07 straightforward simple problem write a program to print the address of a variable we printed the address of the variable we also used this address to get the value of this variable using the asteris operator let's look into the question number two write a program having a variable I so this program is going to have a variable I and will print the address of I now we going to pass this variable to a function and print its address are these addresses the same y so let's see uh what this problem is asking us to do so I'll say 07 pr02 py uh oops I I meant c not py so this is C programming course not Python Programming course my bad C boiler plate code and um what I'll do is I'll start this template I'll use this template to start with and what I'll do is uh this question set write a program having variable I we already had variable I which is why I used that uh program for 6:24:08 this question as well now print the address of I so we are printing the address of I yes we are doing that indeed so this part is done now we have to pass this variable to a function and print its address so in order to pass this to a function what we'll do is we'll create that function we'll say funk and then we'll say instar so Funk is a function which returns an integer and it takes a pointer so I'll say in Star PTR and I'm not sure about the return value of this funk but for now I've written Funk uh sorry int but I'll change it uh if need be so what I'll do is I'll see what this uh function say so we have to print its address in the functions so let me do one thing let me change this to void because I don't want to return anything back from this function so what this function says is simply print the address PTR so I'll simply say say uh the value of address PTR from function Funk is I'll say 6:25:13 percentage U and I'll simply print PTR and as part of the best practice what I'll do is uh I'll use back sln at the end of the line so that all the things are formatted very nicely with the new lines printed so we are passing the address to a function so let me uh call this function and let me give ERS I to this function so this function uh takes the address of I and it simply prints that address of I just like this okay let's see what it prints so the question says that will the address which is printed by the function and the address which is printed by the main be the same will it be the same so are these addresses same which addresses we have to pass uh we have to print the address of I in the main function and then we have to pass the address of I to the funk and we'll have to print that address inside the funk to see whether 6:26:15 the addresses will be same or not do you want to guess whether the addresses will be same or not I want you guys to guess in the comments uh leave the Tim stamp so that I can know which question you are answering in which comment and post your answer I want all of you to post your answers and I'll wait for um for you all to you know post post your comment I hope everybody posted the comment so the answer is yes these addresses are going to be the same because this value and the value we are passing to this function is the address of I is the address of this particular variable in the main memory which is going to be the same so let me run this program for you and show you that these addresses are going to be the same so this value and this value is going to be the same so this is the value which we printed from the function and this is the value which we printed from or main so this is how uh this question was structured this is what it wanted to tell you that uh the address of I is going to be the same in the function as 6:27:16 well as in the main in fact main is also the function when I say function I mean this fun function the function that we created that user defined function that we created coming back to the practice set the third question says that write a program to change the value of a variable to 10 times of its current value write a function and pass the value by reference so this problem says that create a function pass the address of a variable to that function and change its value so let's start from this function itself let me uh create the eighth program I'll say 08 pr03 Doc and what I'll do is I'll copy this particular program and I'll start from here now I'll rename my funk as change because it better reflects what this function is doing so I'm simply saying void change in Star and inside this function what I'm doing is I'll simply say value at this address is equal to 6:28:18 value at this address multiply by 10 because we have to change the value to 10 times the value of um of the variable okay so let me run this function so I'll say the value of I uh let me remove this I'll say value of I before uh change ran is and I'll say after change then is and let me also call change with the address of I and U let me not use this fancy syntax let me straightforward uh say I so I'll simply say I I don't want to use this fancy syntax um star of SX of I which is going to be the same as I but I just want to use this clean synex I so let's see if the value changes so the value was three and it is now 10 * of three which is 30 what if the value is six it should be 60 after the function change runs yes it is 60 6:29:20 let's see if this works for some um larger value like 676 yes it does let's see if it works for zero yes it should and it will because 10 * of 0 is 0 what is 10 10 * of 30 it's 300 so we should get 300 and we are getting 300 so this is how we can pass by reference to a function and change the value uh of a variable inside that function without actually having to have that variable inside this function so did you notice one thing this I was inside the main function this variable I was inside the main function but it was changed by a function which is totally different from Main how using the addresses so if you get the address of a variable you can even change it from some other function so this is what this problem teaches you coming back to the practice set and question number four says write a program using function which calculates the sum and average of two numbers so you have to create a function which can 6:30:22 calculate sum and average of two numbers use pointers and print the values of sum and average in main so this program says that you have to use pointers and you have to print the values of sum and address of two numbers so we have to use pointers and then we'll have to print the value of sum and average in main so this is a very interesting problem and I want you to pay attention so we are going to create a program called 09 pr03 uh pr04 I guess yes do c and what I'm going to do is I'm going to use this as boiler plate code or let me write it from scratch I want to write it from scratch so what I'll do is I'll say int average and sum this is the name of a function and what I'll do is I'll take two pointers inside this function and you'll ask me why am I doing this I'll answer you I'll answer you shortly but for now just assume that it is required for us to take two pointers inside this 6:31:23 function also I'll U remove these from here because this is function prototype this is the function body so let us say the numbers are A and B so I'll simply say uh int a is equal to 6 let me call it float because we are um we are actually calculating the average and I want some sort of precision so I'll make it float and this as float two and what I'll do is I'll simply pass the address of a and address of B to average and sum now what happens is this average and sum function will take the values a b as well as the address of two more variables which are C and D so I have created two additional variables C and D now let me explain you what I'm going to do so I'm going to add int int as well and I'm going to take the value of uh sorry uh I I meant float so I'll say float a float B let me make it float 6:32:24 because we are dealing with the floating Point numbers okay and Float here as well so we are going to deal with float pointers now what I'll do is I'll say call this function with the values A and B and also with two pointers which are nothing but the address of c and address of D so listen to me carefully now what I've done is I've passed the value 6 and 423 which is going to get copied inside this a and this B and also what I'm doing is I'm passing the address of c and I'm passing the address of D to this function why am I passing the address I'm passing the address so that I can update these two variables which are C and D from this function so I'll set C to the average and I'll set D to the sum to make things more clear let me call this float sum and Float AVG so I'll pass the address of sum and address of AVG so that I can update sum 6:33:24 and AVG variables from this function now listen to me what I'll do is I'll simply say the value at ptr1 which is nothing but the Val value of sum should be equal to a + b and the value at ptr2 should be equal to a + b / 2 which is nothing but the average and now here I can print the value of sum is percentage F and the value of average is percentage F so I'll simply say sum and I'll simply simply say AVG and I can print the values of sum and address which are set using this function so did you get it we are not returning anything from this function let me return void from this function we not returning back anything from this function but what we are doing is we are essentially setting the values of sum and average from this function I hope that you understood this let's see 6:34:26 if the program works so it says the value of sum is 429 which is correct because is 423 + 6 is 429 and the value of average is 24.5 so if you add these numbers it'll turn out to be 429 and if you divide 429 by 2 it'll turn out to be 24.5 how was this program let me know in the comments was this something that you saw for the first time in your programming career uh or was it something that you have seen before uh if if you are a c beginner or programming beginner for that matter this is something that you might have seen for the first time and you should definitely appreciate the power of pointers see we didn't even pass sum and AVG we simply passed the addresses of sum and AVG to this function and these addresses were captured inside ptr1 and ptr2 and the values at ptr1 and ptr2 were updated inside this function I'm 6:35:26 not returning anything from this function directly I'm not returning anything from this function but indirectly I'm kind of returning something from this function what is that I'm setting the values of variables in the main function from this function so this program is actually very important mark this program as important and if you haven't understood what we did in this program I want you to watch this question once again coming back to the practice set and to the question number five write a program to print the value of a variable I by using pointer to pointer type of variable so uh what we'll do is uh we'll simply write a program uh which uses pointers to pointers so let's write a program which uses this pointers to pointers so this is 10th program problem number five 10or p05 Doc and I'll put in the boilerplate code so I is a variable it has three inside of it let me create a pointer one I'll say it's an integer pointer which contains the address of I uh so far so 6:36:30 good I'll say I want a pointer to pointer which is ptr2 which contains the address of ptr1 so ptr1 is a pointer variable and its address is stored inside ptr2 which is a pointer to pointer variable so ptr1 is a pointer variable ptr2 is a pointer to a pointer okay now let me show you something what is the value of I yes it's it can be printed this way but uh no surprises this will print the value of I what's new I'll show you what's new in fact not even new because we have already talked about all these Concepts in the chapter so I can uh say the value of I is star ptr1 can I say this or not why can I say this why is the value of I star ptr1 ptr1 is simply storing the address of I ptr1 holds the address of I and since pd1 holds the address of I I can always 6:37:31 dreference ptr1 and I can say star p R1 and star ptr1 will give me the value at this address which is the address of I so what is the value add the address of I it's the value of I which is three now I can also say the value of I is star of star of ptr2 okay so I can actually write ptr1 as star of ptr2 can I do this yes I can do this I can always do this because ptr1 can be written as star ptr2 now if you if you haven't understood why this is the case try to map this with why the value of I was star ptr1 then you'll be able to understand why the value of ptr1 is star ptr2 and if I run this program let me show you all these uh lines will print the value of I is three I can also get rid of this the these parenthesis and this will actually 6:38:31 print the same thing so this is how we can print the value of I using pointer Direct Value pointer to a pointer okay I hope you understood this so this was simply the question which was created by me to explain you pointer to pointer variable so problem number six now says that try to attempt problem number three using call by value and verify that it doesn't change the value of the set variable so what was problem number three uh problem number three was um let Let's see we solv problem number three so let's create a program and we'll copy the code of problem number three so I'll say 11 uncore 11 uncore P um pr06 doc I'll put the boiler plate code in in fact I'll not put the boiler plate code I'll copy the problem number three and I'll paste it inside problem number six so problem number six is talking about problem number three it says that what if we use call by value instead of reference it simply says that you are 6:39:32 actually passing the address of I what if you actually pass I and not the address of I so what if I do something like this and let me not even call it pointer in that case because it's not a pointer and what if I do something like this I'll say I is equal to I * 10 can I do this yes obviously I can do this but it's not going to change the value of I inside main function let me show you so if I run this program you'll see the value of I before uh change ran is 30 and even after change R it is 30 uh we were expecting it to be 300 because we are actually changing the value of I to be I mtip by 10 why does this program not work why did this uh fail to update the value of I this happened because what we are doing here is we are simply saying in I is equal to 30 now we are printing the value of 30 now we are passing a copy of I to change function a copy will be passed so if I have this keyboard and I create copy of this keyboard and I distribute it to my 6:40:33 friend and he breaks the keyboard will this keyboard also get broken no it will not get broken because why why why will this keyboard get broken so if you are buying two iPhones one for your friend one for yourself and your friend actually throws the iPhone um inside um inside say Inside the Fire and he burns his iPhone will your iPhone also get burned obviously not so this this is why the value of I is not changed inside the main function but if I'm passing the address or if I'm giving the reference of my iPhone and I'm saying this is my iPhone and your friend actually throws your iPhone inside the fire so your iPhone will get burned I hope that this analogy makes sense I know this is funny but again I hope that it it will help you in some way to understand all these Concepts so with that said the chapter number six is finished so we'll move now to the next 6:41:34 Concepts and we'll explore C programming language even further so now that we have completed so many concepts of C programming language we are going to look into arrays what exactly is an array an array is a collection of similar elements and this is really very important these elements have to be similar because you cannot add an integer and a character inside an array but since we just started out I don't want to confuse you I'll show you some of the very basic uh syntax of uh arrays and then we'll raise the bar from there so let's get started with what an array is so sometimes you might want to store marks of say 100 students how will you do that you might be tempted to create 100 variables saying Marks One marks 2 marks 3 marks 4 but C programming language provides you a very elegant way of doing the same thing what is that elegant way you can add uh all the marks of 100 students inside one variable 6:42:36 which is nothing but an array collection of similar items in this case the marks of students let me give you a very brief idea of what array is I'll say 01 underscore array Basics do c I'll put in the boiler plate code I'll zoom in a bit at close the Explorer and let me show you what an array so if I do something like this uh this is a syntax of writing an array so basically it's a single variable and it's capable of storing multiple values so I just gave you an example of marks let us say you want to store 100 marks and you want to store 100 marks of 100 students with RW number ranging from say 1 to 100 you might want to store all these values inside one single variable instead of um storing it in 100 variables because creating 100 variables might be very cumbersome task you cannot do that and even if you are able to create 100 elements it will be really very difficult for you to create thousand uh 6:43:36 variables for thousand elements or say 10,000 or one uh 1 million variables for 1 million values you got the point so what is the syntax of creating an array so let us say I want to store marks of marks of 30 students or let me make it uh uh eight students because 30 is just too much okay so I can do something like this I can say in A1 in A2 then A3 then A4 then A5 then A6 then A7 you must be thinking what I'm doing here but I'm trying to create eight variables for storing marks of eight students so these are eight variables and all of these eight variables will store marks of eight students um I can do something like this I can say A1 is equal to this and then I can say A6 is equal to say uh 87 and so on I I can assign marks to these variables but 6:44:37 there's a better way and let me tell you what that better way is so I'll say better way and and a better way to store marks of eight students is something like this you create an array you say a and you simply say the number of values you want to store and that's it that's it and how do you store uh or rather assign marks let me assign marks for all the variables let me make it uh 49 I'm just taking random values for a182 A3 A4 A5 A6 and so on um I I could have actually added values here itself but again um I just want to show you how this maps to the better way of storing marks so let me say 37 and let me say 67 for A8 okay here what I'll do is I can actually start accessing all these variables starting from index zero now 6:45:39 this is important here I started writing A1 A2 A3 all the way till A8 but here instead of doing this A1 is equal to 78 and let me copy copy this so that I can actually show you how this really maps and I'll press alt shift on my keyboard alt and shift I'm pressing together on my keyboard and I just just clicked here and I'm getting all the cursors this way so what I'll do is I'll put in this square bracket and I'll use arrow keys and once again I'll put another square bracket now this is a syntax this is part of the syntax uh if you don't know how the syntax works you might want to manually do this if this is not supported by your code editor if you're using some other code editor other than Visual Studio code your editor might not support this type of syntax and in that case what you will do is you can simply uh use you know manually put all these uh square brackets now I can do something like 6:46:40 this but this doesn't directly map to this now you must be thinking that we can uh say A1 A2 A3 A4 A5 A6 A7 A8 let me let me show you let me run this program for you I'll get rid of this part I'm getting rid of this part because this part is going to throw an error I'll tell you why but again I can assure you that this particular syntax is correct and uh we can actually run this so let me enable code Runner I think I disabled code Runner uh so code Runner is by default disabled I want to enable it in this workspace and I'll run this code and this code is not throwing any error which means we are good and this code works so what I'll do now is um I'll simply say is 1 is equal to 45 and I'll I'll show you I'll run this and show you and I can keep on doing this so the problem with the syntax is instead of starting from one we actually start from zero so 6:47:41 we start from zero and then we go from 1 2 3 4 5 6 7 so for eight variables we start from zero and we'll go all the way till 7 the numbers from 0 to 7 are actually eight numbers if you if you count it 0 to two are three numbers 0 1 and 2 0 to 4 are five numbers 0 1 2 three and four so these are five numbers so this is something that you need to uh understand and we don't start the index from one we start the index from zero so you might have heard this in the memes and in the jokes that index starts from zero counting starts from zero in most of the programming languages and that's the case with c as well so we do something like this and I can actually use this variable by doing something like this I can say the value of student number zero is so we start the counting from zero and the value is going to be a0 so this is going to print uh let me also put back sln for better 6:48:42 readability this will print 45 for me and I can do the same thing for student one student 2 3 four five six I can do this for all the students so I'll put in one here then I'll put in two here then I'll put in three here four here five here and you can print all all of these variables but um okay let me me let me do it for all the variables so I'm simply using multic cursor functionality of vs code um I'll run this and you'll see all the marks that I have assigned here are printed so I can actually use this as a single variable I can use A7 as a variable I can use A5 as a variable now we have marks of eight students at this point so this approach will actually take a lot of lines of code it will take a lot of typing if you try to create say thousand variables but if you try to create thousand variables using this approach all you need to do is you have you need to replace this eight with thousand and you can keep on using your variables as and then require also you 6:49:42 can use a for Loop and you can assign all these values but in this case there is no way to assign values using a for Loop we'll see that we'll see that in U in this chapter itself so coming back to the arrays it's a collection of similar elements in this case it was collection of integers it can be a collection of characters it can be a collection of floating Point numbers and so on so we can create a float array we can create a car array and so on okay so an array is a collection of similar elements it's a single variable capable of storing multiple values so this is a single variable in this case the variable was a but it was actually capable of storing eight values so the syntax looks something like this you can do something like in Marx 90 this is an integer are you can store values inside marks zero marks 1 marks 2 marks 3 marks four 5 six all the way till 89 not 90 make sure uh because when we had array of size 8 the maximum index we can go up to is s and not 8 if you try to use A8 you'll get 6:50:44 into troubles you'll get into memory leaks and all we don't want that syntax of declaring an array looks like this this is for integer array this is for character array this is for floating Point array and so on the values can now be assigned to marks are like this I've written this in the notes so that you can easily revise this in the future now there is a note here there's a side note I've written here it is very important to note that the array index starts with zero this is really very important for you to understand that if we have total of 90 elements the counting starts from zero and not from one if you start it from one we can go all the way till 90 but if you start the counting from zero you can go all the way till 89 for 90 elements you can do this you can try this for eight elements and you can start writing 0 1 2 3 4 5 6 7 8 for eight elements let me show this for you for six elements so these are six elements 1 2 3 4 5 6 but if you start the counting from zero you can only go till five and not six you can see that 0 1 2 3 4 5 because one additional space 6:51:46 will be taken by this zero if you start the counting from one you can go all the way till six but in this case you can go all the way till five starting from zero so this is something that is very important and straightforward as well start the counting from zero and go up to n minus 1 if there are n elements so in this case if there are 90 elements you go till 89 which is n minus1 90 - 1 is 89 okay so we understood what array is how this works let's try to understand how we can assign elements to an array so elements of an array can be assigned or accessed using this and assigned using this so you can use scan I percentage D and you can say Mark zero and what this will do is this will actually uh put the value entered by the user inside Mark zero note this ERS sign we are using this you know exactly like an integer variable not even similar to an integer variable exactly like an integer variable so max Z is like an integer variable Max one is like another 6:52:47 integer variable and we'll use m per to give the address uh so this is something which is important so this is to input first value and this is to Output first value of the array why first value because the index is starting from zero so zero is the first value one is the second value and Marx two is the third value if you count from 0 0 1 2 third value so this is something that you need to uh you know understand and put in your heads now quick quiz quick quiz for you write a program to accept marks of five students in an array and print them to the screen so let's let's solve this quick quiz this is very interesting so uh Co quiz uh oops I forgot to write do c I need to mention the extension then um then I'll come here and put the boiler plate code and what I'll do is I'll simply do it the old school way Marx one Marx 2 Marx 3 Marx 4 Marx 5 okay I'm 6:53:49 doing it the old school wayes to show you how easy using arrays is and U how how much time it saves you you'll get a good idea of this so I'll say Max one is equal to um or ra let me use scanf function I'll say scanf percentage D and I'll say m perc Marks One and also what I'll do is I'll say print F enter the marks of student one so in order to enter the marks of student one um you'll enter it and also I'll put a back sln here let me put a back sln here so that it looks good do not forget the semicolon and now what I'll do is I'll replicate this for for students 1 2 3 4 and five so let me put two in here three here four here and we need one more which is five and this is getting cumbersome till 5 I don't know 6:54:51 how cumbersome it will get if we have 100 or 200 students but again I'm I'm trying to do this the old school way to show you how uh how much time the arrays are using the the array is save so I can do the same thing I can uh take the values and then print them on the screen so I can do something like this uh I'll say print F the marks of the marks coded by student one is student one is percentage D okay and what is percentage D it is Marks One and let's do the same thing for uh for the five students so what I'll do is I'll simply use my ALT key and I'll keep clicking wherever I want multic cursors and then I'll say three four and five this is this is why I recommend Visual Studio code because of all these functionalities and the light code nature lightweight nature it has because 6:55:52 even if you are on a computer with very low memory this works like a charm so let's try to run this program I'll run this program so they are flying a plane about my house I don't know why they have to fly a plane above my house every time I don't know that so let us say the marks of student one is 67 34 54 uh let's make it 87 let's make the last students marks to be 100 and you can see all the values are printed on the screen so this program works no surprises but is this the best way to do this is this the best way to achieve this and the answer is no so what I'll do is I'll I'll rename this this program to Quick quiz uncore old school so this is the old school way and now we'll use the modern approach to code this so what I'll do is quick quiz better solution so I'll say better solution. C let me put in the boiler plate code let me close this and 6:56:55 once I close this I can actually do something like this um I can create an array so firstly what I'll do is I'll create the marks array and how many students do I need I need five students now what I can do is I can use a for Loop so I'll save this program I'll use a for Loop come on come on C language suggest me the for Loop come on you can do that I don't know why it's not suggesting me the fall Loop I'm not sure I need the snippet for fall loop I think I forgot to uh turn on the C++ extension so recently I had a lot of extensions enabled so I disabled all the extensions by default because it sometimes takes a lot of your memory so which is why I'm not getting the for Loop now I should get it so I'm getting the suggestion of for Loop I'm saying in I is equal to Z in I is equal to z i is less than five and i++ and in the code what I'll do is I'll write the same thing but I'll play a bit cloudly here 6:57:56 what I'll do is I'll write this I'll say enter the marks of uh student percentage D and my percentage D is nothing but I so I'll say I and here what I'll do is I'll say marks I so this is something that I can do this is totally fine valid uh no surprise is there and I can use another for Loop to print the marks so I can do something like this so I can say um the marks scored by this student is this this and also instead of saying marks one I can say marks I here and instead of saying student one I can say percentage D and this percentage D maps to this I and this percentage D maps to this Max ey in a very similar way this percentage D marks to this I and scan f everything works this program should work let's try to run this program and yes it's asking me for the marks of 6:58:57 student zero what I'll try to do is I'll try to put the same marks or let me use 34 23 then we have um 3 54 54 and then um 87 and 100 okay so you can see that this program behaves in a very similar way uh to this program and uh writing this program was 22 lines of code writing this program was actually 17 lines of code but even less than that because you know we are using a for Loop and uh we used some new lines here so if I try to get rid of all these new lines and show you you'll see that you maintaining this program is very easy so if this was 21 lines of code uh 22 lines of code this is just 13 lines of code and maintaining it is super simple because we just have two for Loops here look at this and look at this um and this program is small I mean just 22 lines of code because we have only five variables this program is 6:59:58 capable of handling thousand variables maintaining the same lines of code so this is much better using fall loops and using arrays so many of you might think that why are we using student zero why are we not saying student one here why did we say student zero if you want student one here you can print I + 1 and actually store the value of uh student one in Mark zero student two in Marks One and so on so let me cancel this let me save this program and show you this works and this will say marks of student one is this student two is this but I'll also print i+ 1 here because I want it to go from 1 to 5 and not from 0 to 4 so I'll use I + 1 here so this program and this program will exactly run um with the same output okay so let me run this for you and let me show you I'll simply punch in some random values so you can see that the marks coded by Str one is this two is this three four five so this program works in an exactly the same way 7:00:58 as this program uh the difference being that here the program is not maintainable I'll show you an example of how this program is not maintainable why this old school program is even more worse than it looks like so let us say I want to change the marks to uh enter the score of student let us say somebody instructed me to use the word score and not marks so I'll use oops I I'll use score I'll use score scored by student percentage D is percentage d so here I just need to change these two lines and if I run all the lines will say score instead of mask Max okay so if I do something like this uh you'll see that all the values are now saying score and not marks but if I was to change the same thing here I'll have to actually go to each and every line and change marks to score I'll have to say enter the score score now you might argue that hey harry 7:02:00 why are you not using find and Place simply do this and change this to score but here is another problem if if I try to change this marks to score these variables will also get changed and there might be something in the program which might get changed accidentally and I might get into trouble so I don't want to write program this way okay this is the gist of it um yes I can always use some automated means to do find and replace but here the number of variables is really very uh small which is five but if you have say 20 variables or 30 variables or even uh 1,800 for that matter how will you maintain the code uh so this is why arrays are actually very useful so uh we spend quite a good amount of time in this quick quiz but I hope that you understood how this uh syntax works and why arrays are helpful okay now let's talk about initialization of an array what are the ways to initialize an array so there are many other ways in which an array can be initialized a lot of ways can can be 7:03:02 used to initialize an array so if you look at this in cgpa 3 is equal to 9 8 and 8 this is nothing but a very quick way to initialize arrays so if you say in cgpa 3 is equal 988 it's going to say cgpa 0 is 9 cgpa 1 is 8 and it will set cgpa 2 to 8 once again so let's try the syntax let's create a program and I'm going to call this program as initialization of uh array so I'll say initialize array. C I'll put in the boilerplate code uh note that I have named this program as two and this as two as well because these are the same problems uh solved using different approaches so let's try to create an array called marks and let's try to use this syntax uh and let's say the marks is an array of three marks and let's see if the syntax works so what I'll do is I'll come back to my this syntax I'll copy this for Loop and what I'll do is I'll say simply paste this code in here 7:04:04 and I'll simply format this like this and what I'll do now is simply I'll use a semicolon here I have made a mistake I am not using semicolon here uh which is why it was throwing some error so now what I'll do is I'll simply fix my indents and now what I'll do is I'll simply put in three a so that the value of I goes from 0 one and two and let's see if it is printing the correct values of marks so it is indeed printing the correct value of marks so I'll call it marks and not score so I'll say the marks scored by student this is this which is stent one is 34 2 is 23 3 is 53 so this is indeed printing the correct values of marks now let us try this synext let us try uh float instead of int and definitely I'll have to use percentage F instead of U this percentage D because this value will be a floating Point number make sure that this is percentage D because index is always going to be an integer okay so 7:05:04 I'll simply make this float and let me see if these values are working and I'll say 5.3 here let me run this program and yes these values work okay so these values are working in a very perfect way uh now what I'll do is I'll simply come back to my notes and try to explain you how arrays look in memory so consider this array it is an array of three in integers and it contains 1 2 and three as the values uh so you need to make sure that one integer is 4 bytes in this architecture now it might be two bytes or it might be some other value in some other architecture it depends on the machine but let us assume that we are on a machine where one integer is 4 bytes okay this will Reserve 4 multip 3 is equal to 12 bytes in memory uh now I want to tell you something about the memory memory is contigous which means that you can simply store 0 1 0 1 0 1 01 in this manner now if you want to store an integer you need four bytes which is 7:06:06 nothing but 4 * by 8 Bits one bite is 8 Bits so this is how memory works so what this will do is this will uh save or rather Reserve 12 bytes in memory four bytes for each integer so if you try to create a create an array like this this will Reserve 12 bytes in memory like this okay so this is how it will work so 62302 will hold this integer four bytes will uh be used by this particular integer then again uh 62306 onwards uh this integer will store this two uh this place will get four bytes this place will once again get four bytes and this is how um this is how memory management of are works okay now let's talk about Pointer arithmetic which is something you might not know or you might know but let me tell you something about array and pointers okay so let's create a program called array and pointers and I want you to be very attentive when I 7:07:07 teach you this arrays and pointers do c so let's put in a boilerplate code and let me tell you something if I say in marks is equal to 1 2 3 and I have to make it Max 3 and I'll put a semicolon here when I do something like this this m person Mark zero which is the address of first element can be written as marks so this particular thing and this particular thing is exactly the same thing which means that if I want to change this value I can do something like this I can say Max star marks is equal to 89 if I do something like this and I try to print say Mark zero let me try to print Mark 7:08:08 zero I'll say marks stor by student Z is uh percentage um percentage D and I'll simply say Mark Z if I run this program I'll not get one as Mark Z rather I'll get 89 as Mark Z why because I have used this pointer syntax to change the value of Mark zero 289 so what I'm saying is I'm saying the address of mark0 is nothing but marks I can actually use value at operator which is star operator I can say value at marks is equal to 89 when I do something like this what happens is value at this particular address which is m per Mark zero which is address of this particular uh place where this number is stored uh this changes the value of Mark Zer which was 1 before to 89 now so this is how all the syntax works and you 7:09:09 can actually change the value of uh this two as well and how do I change the value of two I can increment this pointer and if I say marks Plus+ it will not be 1 plus the original address it will actually go to the next address this might sound strange so let us look into this uh more uh in a more systematic way I should say so what I'll do is I'll say in Star PTR is equal to m% mark0 but I not do something like this I I can actually do M PTR uh sorry star PTR is equal to M Mark zero but in order to explain you I'll do something like this I'll get rid of this because I have told you and let me write important here this is this is the core concept here that m% Mark zero can be written as marks so I simply said in Star PTR is equal to Marks I could have said this and the program would have worked in exactly the same way but I want to teach 7:10:10 you this synex I want to teach you that marks is nothing but m per Mark zero this is important this is very important which is why I've marked this as important this concept is important now instead of using star marks I can use star PTR because PD is a pointer which is pointing to this so I'm saying star PD is equal to 89 and this changes the value of marks 0 is 89 what will the values of marks 1 and marks 2 and I know that you must be thinking why would these values change and you are right these values will not change these values will be two and four but listen to me carefully now if I do something like this PTR ++ PTR ++ means point to the next element now this is something that you need to understand pointer arithmetic will not add one integer to the but rather what it will do is if we have an integer pointer it will point to the next integer because the first four bytes are reserved for this particular 7:11:11 integer so it will not touch those four bytes if I say PTR ++ it will point at the starting of next integer in the case of arrays and even if we are not dealing with arrays PTR ++ will point to the next value so if the first value is one next value is two this PTR ++ will actually point to % marks one so PTR is now ENT marks 1 which is why I can do something like this I can say PTR is equal to 65 and this will change the value of Marx 1 to 65 let me show you so the value of Marx one is now 65 can I do the same thing again can I do PTR ++ once again and will the this PTR point to four this time yes you are right this PTR will now point to 4 and you can do something like this let me make this as 82 and yes Marx 2 is now 82 you are right let me save this and run this for you Marx 2 is now 82 so this is 7:12:14 how pointer arithmetic Works instead of changing an address by adding a number to it it will change the address by adding four to it it will make that address point to the next integer or point to the next character or point to the next floating Point next Double next long double and so on so in this case the pointer is of type integer so it will actually increment it by four instead of incrementing it by one because integer in this architecture is taking four bytes okay let's now come to the notes and try to understand this thing so a pointer can be incremented to point to the next memory location of that type note that if the address is 87994 PTR ++ will not make it 87 995 but it will check what is the type of a if it is an integer it takes four bytes it will add four to it and not one okay let's understand this example thoroughly now so in I is equal 32 simply we are assigning the value 32 inside I in Star 7:13:15 a is equal to m i so the value uh of this pointer which is a a is an integer pointer is nothing but the address of I so let us assume this address of I is 87994 so address of this integer is 8799 4 and this integer contains 32 the name of the variable is I I hope you don't have any doubt so far let's try to Deep dive now if I do something like A++ you might think that this address will change to 87 995 but no it will not change to 87 995 it will change to 87998 why why were these four uh points added to this particular address because this is of type integer integer takes four bytes so it will point to the next integer so it will add four to it and not one okay I hope this is clear now let's try to understand the same thing using character so if I say car a is equal to a and I say Car Star B is equal to m% a so I'm creating a pointer B 7:14:17 which is actually the address of this character a and let us assume that b is 87994 B is the address of a let us assume that it is 87 994 now since character takes one by in memory this address will now be incremented to 87 995 not because we are adding one to B but because character takes one bite so since character takes one bite one is added to this address since integer takes four bytes four is added to this address if we have another data type that takes say 9 bytes for some reason We'll add nine to this address I hope you now understood how pointer arithmetic works so so if we have something like this float I is equal to 1.7 and Float star a is equal to m% I a is actually holding the address of I and since float takes four bytes in this architecture A++ will change a from 87994 287 998 so it will add four to it 7:15:19 why because float takes four bytes in memory I hope you are clear with all these things now so following operators can be performed uh sorry following operations can be performed on The Point uh you can add uh addition of a number to a pointer you can add a number to a pointer if you add two to a pointer it will point to the next to next integer if it is of integer type subtraction of a number from a pointer is also possible subtraction of one pointer from another is also possible comparison of two pointer variables is also possible so you can do all these operations uh you can compare two pointers you can add and subtract two pointers and so on so I hope you understood pointer arithmatic now now there's a quick quiz for you try these operations on another variables by creating pointers in a separate program demonstrate all the four operations so you need to demonstrate all these four operations so let's try to you know demonstrate this using Code so what I'll 7:16:21 do is I'll create a new file I'll say 5core and I'll say quick quiz do c I'll put in the boiler plate code I'll zoom in and what I'll do is simply um try to demonstrate all these things to you so the very first operation says addition of a number to a pointer but before we do all this let's say in I is equal to 5 and uh let's say in Star PTR is equal to m% I so now PTR stores the address of I and I'm going to write a bunch of print of statements what I'll say is the value of I is uh percentage D back sln back sln and I'll simply say I and I'll say the address of I is I'll say ENT I or what I'll do is I'll say PTR or rather let me say ENT I but uh what I'll do is I'll say the 7:17:22 value of PTR is I'll also print the value of PTR okay so the value of PTR is percentage U and percentage U here as well because is this is an address and I'll say PDR okay let me run this program for you and see uh how this prints uh the address of and the values of I so value of I is five address is this address is this sounds great now what I'll do is I'll simply say PTR ++ uh let me let me do something like this let me copy these two lines and let me replicate and I'll say PTR ++ okay so I have in fact added one to PTR Okay so let's see the value of PTR after increment after PTR is incremented is so let's see what the value of I is after PTR is incremented what the value of PTR is so the value of I is not going to change it will be five just like it was before um the address of I will not change because it's directly taking 7:18:22 address of I and the PTR will change and four will be added to it can you see that so the previous value of PTR was this now four is added it's uh 20 previously it was 216 at the end now it's 220 because four is added and what about the address of I address of I is not going to change address of I is the address of I it's not going to change just like your address is not going to change if you live in say Mumbai your address is not going to change if you live in Hyderabad your address is not going to change unless until unless you are vacating the house and moving to some new place so I'll say save this program and what I'll do is I'll say PTR is equal to PTR + 1 so I can either say PTR ++ or I can say PTR is equal to PTR + 1 so we'll get the same results the value of PTR after PTR is incremented is this let me get rid of this line because the address of I is not going to change uh let me keep one line uh to demonstrate that PTR is changing now the PTR is 220 what if I say PTR is equal to 7:19:25 PTR + 2 if I add two to a pointer 8 is going to be added to the address of the pointer because it's going to shift the pointer by two places and when I say shift the pointer by two places it simply means that it's going to shift it by 2 multiply by the number of bytes taken by integer which is 2 * 4 which is eight and hence eight is added to this previous address I hope that makes sense so we have added a number to a pointer now let's subtract a number from a pointer I can subtract a number from a pointer so what if I say PTR minus 2 it will be decremented from the pointer let me show this to you so it was 216 before 216 - 8 is 208 so it's 208 now I hope that makes sense as well subtraction of a pointer from another now this is very interesting so let us say I give you an array okay marks and I tell you that there are 100 items in this array so marks is an array there are 100 items in 7:20:26 this array I want to ask you how many bytes does this array element takes so you can say Marx one minus Marx Z that will be the address of Marx one minus address of marks zero and if you you know decrement U the value of marks Zero from Marks One you'll get four if it is an integer so the difference between two addresses will actually tell you how many bytes each enti is consuming let me show this to you so if I say uh in I is equal to 5 and if I say PTR is equal PTR minus 2 what if I say PTR minus instead of printing the PTR what if I print PTR minus m% I it will actually tell me that you have incremented the value of pointer by these many units so let me put a percentage D here and let me show you how this works so when I say percentage D it will show me two and it will not show me the absolute difference 7:21:26 between these two numbers no it will not show me that it will actually show me the ab absolute difference divided by the number of bytes taken by one unit which is four so it will tell me -4 / - 8 divid 4 which is nothing but min-2 so I hope this helps you so this is basically telling me uh how behind is PTR from the address of I because we have already decremented minus 2 so it's simply telling me that you are you have decremented the pointer by two so this is uh how subtraction of 1 pointer from another Works comparison of two pointers you can actually compare two pointers so let me show you how comparison of two pointer works so um let me get rid of this and let me say uh the value of comparing two pointers let me say comparing PTR with ERS I or let me get rid of this and let me simply say if PTR is greater than M 7:22:26 percenti I can do something like this and what I'll do is I'll say PTR is greater and uh I'll copy this particular line and I'll simply say else and I'll say PTR is not greater so this will actually tell me whether PTR is greater or not in this case is PTR greater no it's not greater because we have already decremented it by two so it should give me PTR is not greater but what if I make it PTR + 2 so I can actually find out whether my address is behind M perc I or is it not behind MC I so since I have incremented two which is I have shifted it by two values by two integers which means that eight units eight bytes rather have been added to the initial address so PTR has to be greater now so PTR is greater so we can do pointer comparison just like we did um pointer subtraction addition and subtraction of a number from a pointer I hope that this quick quiz helps you a 7:23:26 lot and it will help you understand all these Concepts more clearly now accessing arrays using pointers so this is something that I have already told you that you can actually access an array using pointers so if PTR points to index zero PTR ++ will point to index one and so on so if I say PTR is equal to am percent of let us say Mark zero which is PTR is pointing to the first element of this array I can do PTR ++ to make it point to this number I can do PTR is equal to PTR + 2 to make it point to this number and so on I can keep on incrementing this pointer and it will shift itself to the next value uh as in when I keep on incrementing a number to it so this way we can have an integer pointer pointing to first element of the array like this so I can have integer pointer uh which is in Star PTR pointing to the first element of the array so I can either say in Star PTR is equal to M ARR Z or I can simply say ARR I can also 7:24:27 say in Star PTR is equal to ARR some I told you here I've already told you in arrays and pointers so either you can do something like this or you can simply say marks this also is equivalent to saying m per Mark zero this will point to the first element of the array the name of the array will point to the first element of the array this is how you can remember it okay so PTR ++ will make it point to the next integer so if you do star PTR you will get nine because this is the next integer and so on you can keep on incrementing the pointer and you will keep on you know moving the pointer to the next next next next and the next element and so on okay now here comes the interesting part wherein we can pass arrays to functions we can actually pass arrays to functions and when we are passing arrays to the functions an entire copy of the array will not be created this is something that you need to keep in your mind so many people think that if they pass an array to a function a copy of the entire 7:25:29 array will be created no that is not the case just the address of first element is passed and I'm going to demonstrate this to you so arrays can be passed to the function like this so you can make a function called like this you can say print array a r RN and is the size of the array and you can say void print array in Star I int n and this is the function prototype okay this is the function prototype void print array in I int n this is the function prototype or this can be the function prototype you can either accept this as an address or you can simply accept the array okay so this will also work the same way I'll show you how this works so let's come to the VSS code and let's see how we can pass array to functions so let me create a file let's say 06 uncore array to functions. C let me put in the boiler plate code and also what I'll do is okay so I'll fix one thing with my snippet I'll show you what is the problem with my snippet so when I say C boiler plate and I press 7:26:30 enter and when I'm typing something can you see this highlighted stuff here this is basically telling me that when you press tab you are going to move to some other place so when I press tab I'm actually moved to this last line but I did not come to this last line so in order to fix this I'll go to my user Snippets c. Json and here is the snippet so in order to fix this I'll change this to Dollar Z and this should fix my snippet I'll close this and if I say C boiler plate code and I type something here pressing tab will not lead me here anymore okay so this was a basic problem with my snipet and I was just silent about it I'm glad that I fixed it so I can do something like this and I can start typing anyways let's try to create a function let me call it void Funk and it simply takes a an integer pointer and let me also do something like this and I don't 7:27:31 need this PTR here this is a function prototype with the semicolon and this is the function body so let us say I have an array called marks let me copy it from here and I need I need integer array so let me try to uh get some integer array from somewhere I'm too lazy to type I don't know why let me know in the comments if you are lazy as well I'm really very lazy I'm just searching for some array to copy from so so I unfortunately can't find an array okay I I just found an array I can copy this and I can paste it here and uh what I'll do is I can now simply use Marx Z Marks One Marx 2 to access the elements of this marks but what if I say funk and marks what will happen is the address of the first element of the array will be copied to PTR and now what I can do is I can write a fall Loop and I can do something like this I can 7:28:32 say in I is equal to z i is less than 3 uh and I ++ I can do print F and I can say uh the value of Max percentage D is percentage D and I can do something like this uh I and U PTR ++ okay so this will actually uh in increment the value of pointer and at the same time it will dreference it so I'll say star PTR ++ and this will make sense to you let me finish and this will definitely make sense to you and also let me add these square brackets here so I'll add these square brackets like this and let me run this for you once I run this uh okay so I need to say percentage D and not M perc D so the value of Mark Z is 1 2 and four so you can see um also I should write back sln here I don't know why I keep on forgetting uh to put 7:29:33 a new line so you can see Mark 0 is one then Marx one is two Marx 2 is four which is correct how is this working so basically what I'm doing is I'm copying the address of this particular thing inside uh inside of here okay now what I'm doing is I'm dereferencing this pointer and at the same time I'm incrementing it so that the next time the next iteration of the Fall Loop runs I will get the incremented pointer so initially the pointer was pointing to one I printed one in the next iteration of the Fall Loop pointer will point to two because I am saying p++ every time I running this iteration of the follow Loop so if I say PT ++ once again the pointer will point to this and so on it will keep on incrementing itself until it reaches the last item of the array and this is how we can pass arrays to the functions what if I do something like this what if I say star PTR is equal to um say 56 for some reason what if I what if I change the items to 56 um 7:30:35 what if I change marks 0 1 and 2 to 56 for some reason will they be reflected here what if I try to access the value of marks Zer marks 1 and marks 2 will they be changed here is the question and you need to answer this in the comments so I'll say markai tell me whether this value will be changed to 56 or not let me get rid of this printf statement so whatever is printed now will be printed from here so we are running this function will this function be able to make changes to this array this is the question answer mean yes or no uh and you can use time stamps of the video to tell me which question you are answering I'm waiting for your comment make sure you post the comment and one more thing I'll need to do here is p++ because I'm getting rid of this line so I'll do p++ now tell me whether this will change the are to 56 56 56 or not so I'll now run this program to show you that all these values will be changed to 56 56 and 56 as you can see the value of Mark 0 1 and 7:31:36 2 is changed to 56 because we are dealing in addresses this time we are passing the address of the first element of the array to this function and since we are passing the address to this function what happens is that this um that this address is actually dereferenced and the actual value of Mark 0 1 and two is changed so I hope this makes sense and these are the programs that I'll zip and you know put it in the description somewhere so that you can access and all these codes are going to help you a lot believe me so you can access these codes try to run them on your own and you'll definitely get a hang off all the concepts using these notes the codes and the video obviously so let's not talk about multi-dimensional arrays and then we'll conclude this chapter so multi-dimensional array is something that you might not use a lot of times if you're writing simple program s but if you want to say write programs on matrices or you want to visualize a 2d 7:32:37 Grid or a 2d table containing numbers you might want to use multi-dimensional arrays look at multi-dimensional arrays as a two dimensional grid let me search multidimensional array and let me go to images and let me see if I can actually show you something so this is a very good image to start with so row 0 Row one row two column 0 column 1 column two so the rows starts from zero column starts from zero so if I say a RR 0 0 this will or rather let us say the name of the variable the name of the array is X so I can actually reference this value as x0 this is x01 this is x02 and so on so I can visualize this particular two-dimensional grid and I can set all these values so let me show this to you by writing a program 07 multi-dimensional array.c boiler plate code and let me say in marks let 7:33:40 us say I want to express marks of four students from two classes so uh marks or let me make it to four so two classes four students so students are in the columns and the classes are in rows so let us say we have two two sections section A and section B section A has four students section B has four students I'm keeping these numbers as low so that you can um you know easily visualize what is happening now what I'll do is I'll simply say Mark 0 0 so uh mark0 is section A so first student of section A has got 98 marks now second student of section A got say 93 marks uh let us assume that the section A contains you know list of brilliant students so second student sorry third student of U section A let us say he got he was unlucky and he got 83 marks and this is the lowest score and let us say 7:34:43 the last student of section I got 90 sorry 88 marks now I'll do the same for Section uh section B and let us say all these people have no interest in studies they are back benchers they are uh they're lazy people just like me but I used to score a lot of marks by the way so these are lazy people who are not able to score marks okay now in marks 24 98 93 83 88 and so on okay now what I've done here is I've actually set all these values now I can use a double for Loop to you know I trate all these values so let me show you how this works so I'll put two here another tab another for Loop so this might really uh you know sound very confusing to you and you might think that is this something that I I even need is this something that I even need to study but let me tell you something 7:35:43 two dimensional array is something that you will ever need if you're writing some complex programs I'm not saying that you'll not need threedimensional five dimensional 10 dimensional areas I've seen people working with 50 dimensional arrays but again that is very very rare so two dimensional array is something that you need to Target initially and later everything will seem very easy to you so here what I'll do is I'll say Max IG Max IG is nothing but the um some value which is which this particular thing is currently at let me show you something I'll say print F and I'll say percentage D and I'll simply put this Max IG here okay so let me put Max IJ here if I run this program what will happen is all these values will get printed without any space without any new line which will really get very confusing so what I'll do is I'll add a space here after percentage D so if I run this program all these numbers will be printed with a space between them but I don't want them to print this way I want to print them in this way in this 7:36:45 fashion so how do I do that so let's try to understand how this double fall Loop is working so this follow Loop is for rows let me write a comment here for rows so we are saying for the first row you do this and and you once again do this for the second row and this is for columns this is painting the columns so this will paint column one for the first row column two for the first row then once again when the row is the second row the row being painted is second row this will print the first element then second element of the row then third element of the row and so on so this is for columns it's basically painting The Columns okay so what I can do is every time uh it is done painting all the columns I can say print backs slash so I can say print F and I can do something like this I can say back sln so I'm printing a new line every time entire row gets painted so this will run for the first row and once all the 7:37:47 elements are painted for the first row I'll print a back SL try to visualize this try to do the dry run of this program and if I run this program now you'll see for the first row these values are printed for the second row all these values are printed so so these people have no interest in studies they score you know relatively low marks and the first row is the list of students uh who actually achieve good marks so if I do something like this you'll see first row has four columns second row has four columns so two rows four columns this is what we you know initially declared and this is how this works so if you want to uh you know visualize this directly you can clearly see that Marx one 2 is is 43 and where is one two coming one two will come somewhere here let me show you here itself so first row sorry second row Marks One is second row and 012 third column so 012 this should be 43 and yes it's indeed 43 Mark 0 0 is 98 Mark 0 0 7:38:49 through Z column this is 0 through First Column which is 93 this is 0 through third column um sorry zero throw 01 2 second column which is 83 and so on so so this is how you can visualize a two-dimensional array now you can make it 20 and 40 you can make it as big of a number as you want but this is the idea so I hope I uh I hope that I was able to explain you all these things and you understood um how these multi-dimensional arrays work so I've written the same thing in the notes as well an array can be of two dimensional it can be a three-dimensional array it can be an N dimensional and can be a number as huge as thousand or 10,000 for that matter a two dimensional array can be defined something like this you can have multiple curly braces instead of doing this I could have done let me show you what I could have done uh this and let me try to use my visual studio code skills here so what I'll do is I'll simply take all the cursors here I'll 7:39:50 say shift home so shift home will actually get me here I'll press a backspace which will make me get rid of all these numbers so 1 2 3 4 5 6 7 8 and 4 * by 2 is 8 so we have all the numbers now what I want is I want to start this uh so I want to actually change all these to comma uh so what I'll do is I'll do something like this I'll I'll take U I'll do something like this so there are four elements in one row so I'll have to do something like this I could have replaced this uh semicolon with u with a comma but I don't want that it's fine so this is onedimensional array this is a onedimensional array then once again we'll have another one dimensional array here let me show you how so what I'll do is I'll simply replace all these semicolons with a comma and I'll do something like this and 7:40:51 uh and I think I'll simply use um keyboard okay so this was bit time consuming for me but again it's going to be worth it for you because I'm going to explain you how two dimensional arrays work so let me put this curly brace here and also let me put another curly brace here so basically what what is happening here is and let me put a comma here let me put another comma here okay I don't need a comma here so I'll Simply Save it and what is this error for expected a semicolon okay so I'll do something like this I'll say marks Max 24 is equal to or rather I can do this directly instead of writing the Declaration separately I'll do this directly I'll do something like this and this will work and also I'll put a semicolon here so what is happening here is try to understand this what is happening here is I I'm saying give me an array of arrays two dimensional array 7:41:52 is nothing but array of arrays so this is an array and inside this are a lot of arrays so there are two arrays inside of this so so basically I'm writing this grid as is so if I run this program I'll save it and run this program you'll get the same output you'll get the same grid as the output no surprises whatsoever so 83 88 38 63 43 68 and so on okay so this is another way of writing um all the values of a two dimensional array we can access the elements of this array as ARR 0 AR 01 and so on so AR 01 is four because uh zero throw and uh the this particular element 01 uh First Column starting from zero so this is how two dimensionales work and you can have three dimensional arrays you can have 10 dimensional arrays you can have 99 n dimensional arrays and so on although it's very rare to see uh an array which is greater than three dimensional three is the max and in fact two is something that is very rarely used onedimensional 7:42:52 arrays are very very common so let's try to see two dimensional arrays in memory so a 2d array like a one is stored in continuous memory blocks always remember that the memory in our computer consists of continuous memory blocks so the contiguous memory locations simply work this way that if we have 222 then the next memory location will be 223 and so on so if we have U an array which contains integers the memory locations will look like this so if we have an array of integers uh the memory locations will have a gap of four between them so if you have 147 9 1122 this is the address of this particular element this is the address of this particular element so we have a gap of four so a two Dimension are stored in continuous blocks like this a 0 0 will be stored here a 01 will be this so they are actually onedimensional but they appears to be two dimensional to us uh try to think about it so they are continuous memory blocks the first block is error 0 0 next is error 01 and they 7:43:54 are simply accessible using these two indexes 0 1 02 22 32 so you can actually visualize a two dimensional are like this but actually deep within the memory they are in a continuous block do not get confused with this topic today 2D arrays in memory I just wanted to tell you that all the data on your memory is stored in the form of continguous blocks so a 2d array like a 1D AR is stored in continuous blocks um and uh there's a quick quiz for you I hope you understood this concept you need to create a 2d array by taking input from the user write a display function to print the content of this 2D array on this screen we have already done this all you need to do is wrap this inside a display function and I know you will do it so we have already written this program to print a 2d array but you need to wrap it inside a display function and I hope that you understood this I hope that this made complete sense to you you can actually get rid of the number of spaces here um if you think that the number of spaces being used are too much so you can do something like this you can get rid of these spaces um if you add spaces 7:44:57 inside C programming language it doesn't mind so you can do something like this as well and the program will uh very well work so I hope that this chapter made sense to you we learned a lot of Concepts we learned about pointers how arrays and pointers work together how pointer arithmetic works so a lot of Concepts in this chapter in case you have not understood any concept I um urge you to remind this video and look at those Concepts before proceeding uh so we'll now move on to the next chapter and we are going to look into more Concepts but before before that we are going to solve chapter 7 practice set so let's move on to chapter 7 practice set now all right so time to solve the practice set this practice set is related to pointers and arrays so let's talk about this practice set we'll talk about arrays in this practice set we'll practice problems on multi-dimensional arrays let's see what all this practice set has to offer let's move to question number one create an array of 10 numbers you have to create an array of 10 numbers which is pretty straightforward 7:45:57 at this point if you are watching this sces so let me open this particular folder on vs code and let me also create a program for this first question we want to start this question with problem number one so let me say 08 uncore PR not pro I wanted to say PR pr01 Doc and let me put the boiler plate code here and uh after putting the boiler plate code what I'll do is I will simply create an array of 10 numbers verify using pointer arith that PTR plus2 points to the third element where PTR is a pointer pointing to the first element of the array okay so let's let's uh start discussing that uh so what I'll do is I'll create an array of 10 numbers I've done that I'll simply say ARR 0 is equal to 7 and let me also fill in the values of ARR 1 and 2 I need not fill the values of arr3 onwards let's skip that now what I'll do is I'll say instar 7:46:59 PTR is equal to so PTR is a pointer and this pointer is pointing to the first element of the array okay so I can either say this I can also say this this is also valid uh in Star PTR is equal to ARR ERS of error zero this is also valid this is the same thing ARR points to the address of first element we have already seen this if if you are if you still stuck why this is the case I think you have not watched the chapter carefully I want you to watch the chapter 7 carefully and if you haven't done that please go back to chapter 7 and watch it carefully now inar PTR is equal to ARR now what I'll do is I'll simply say PTR is equal to PTR + 2 which means I'm updating PTR to two so let's now print the value at PTR is and I'll simply say percentage D percentage D and I'll simply uh use the value at operator 7:48:02 which is star PTR and let me also say the same thing here so initially what will happen is the value at PTR will be seven and later it's going to be 8 okay let's verify this and this is very straightforward let me tell you so initially it is seven after that it is eight because this is pointing to the third element now we have incremented the pointer by two which means initially it was pointing to the first element then I shifted it by two which means it will now point to the third element okay this was pretty straightforward question if S3 is a 1D array of integers Then star s + 3 refers to the third element is this true is this true tell me is this true or not I want you to answer this question in the comments write the time stamp and answer true or false come on I'm waiting for your comment okay so if you have commented I want you to understand that if S3 is a one D array of integer s + one will point to the 7:49:02 second element if s is pointing to the first element s+1 will point to the second element s+2 will point to the third element s+ 3 will point to the fourth element but there is no fourth element there are only three elements verify this using pointer arithmatic you will find out so let me let me Google arrays let me uh you know take some image and tell you so let us say this is the first element this is PTR 200 is the PTR so this particular value which is B it is PTR + 1 PTR + 2 so third element will be achieved after I uh increment my PTR by two so if I increment my PTR by three that will point to the fourth element and not to the third element so this is false okay write a program to create an array of 10 integers and store multiplication table of five in it so you have to create an array of 10 integers and then you have 7:50:03 to store multiplication table of five in it so this is a very simple problem once again all you have to do is create an array of 10 integers and then all you need to do is populate that array using a fall Loop let's see how this can be done so I'll say 09 uncore pr02 Doc and what I will do is I'll copy paste the program that I've already written why did I do that I did that because already I've created an array of 10 integers now I'll use the fall Loop and I'll simply say in I is equal to 10 sorry I is equal to 0 I is less than 10 and uh in place of this code what I'll do is I'll copy this print F I'll say the value of I'll say the value of percentage d uh or rather uh 5 * by percentage D percentage D is uh and also let me do one more thing 7:51:05 I'll say I here so in place of this percentage D this I will be printed and in this place uh in place of this percentage D I want a rri to be printed so this is very simple and straightforward so what I'm doing is before I do this what I'll do is um I'll simply say AR I is equal to 5 * I so what this will do is it will start printing uh values of 5 * by 0 all the way till 5 * by 9 so let me show this to you so 5 * 0 is equal to 0 5 * 1 is equal to 5 so it's starting the multiplication table from zero onwards but I want to start my multiplication table from one onwards so what I'll do is I'll replace this I with I + 1 here as well as here so so I'll simply print I + 1 instead of I let's run this and you'll find out that multiplication 7:52:05 table of five has been printed you might also want to use double for Loop but again why would you do that you can simply assign uh the value to the array as well as you can print the value uh inside the same for Loop if you want another for Loop you can do that as well that also works so all you'll have to do is you'll first initialize the entire array and then you will print so that will also you know do the same thing so either way it works so I hope you were able to solve this problem and you understood this problem pretty clearly this was problem number three so I I would like to rename this program to pr03 because I want you to have all these codes for reference I'll give you the notes I'll zip all these Source codes notes and everything and I'll give it to you in the description so um coming back to the practice set repeat problem three for a journal input provided by the user using SCF all you have to do is instead of writing this five you'll replace it with n uh so let me solve this problem for you this is very simple problem so I'll say 10or p4. 7:53:09 C and what I'll do is I'll copy this code and instead of writing five I'll replace this five with n and um you know I I'll have to use another percentage D where I'm printing n and um I'll do that I'll do that and not null I want to use n so N is a variable which will be say six and let's see if we get the multiplication table of six so if I run this program oh yes I'm getting the multiplication table of six so this program works it was pretty simple and straightforward but the question asks for using a user defined value so I'll say enter the value of N and I'll simply use back sln and uh I'll use scanf percentage D so percentage D and M perc n so you can simply add the value of uh assign the 7:54:09 value uh 7 to n or any value that you enter will be assigned to n and you can see that the multiplication table of seven is printed on the screen so this program works I hope it was pretty simple straightforward let's move on to the next problem which says that write a program containing a function which reverses the array pass to it so all you have to do is you have to reverse an array so this is a very interesting problem this is very interesting problem so look at this problem very carefully I'll solve this problem for you so let me create another program pr05 doc boilerplate code and um problem number five yes so we have to reverse an array so what I'll do is I'll say in and and this is a very famous interview question as well look at add this really very carefully guys so let me copy this and also let me write the function body or let me tell you one more thing that you can directly write the function body skipping the Prototype that also works 7:55:11 okay so I'll do that um if I say in Reverse in Star PTR U I'm taking the first elements address into PTR and then I can operate on the array so what I'll do is I'll say int AR uh is equal to let me have 1 2 3 4 5 6 7 8 9 and 10 let me have an array of U one uh all the way till 10 and let me reverse this array but before I reverse this array uh I also want to print this array so I'll simply display this array and display is something that I will uh write it's a function I'll write this function so I'll say void display inst star or let me call it PTR because we have been calling it PTR uh I'll use a fall Loop I'll say n i is equal to zero I is less than um I I also need the size of uh this array which is 7:56:13 n so I'll simply say int n I'll say print F and then I'll say percentage D and I'll dreference PTR and then what I'll do is I'll say PTR ++ that's it so I'll D reference PTR initially then I'll say PTR ++ which will increment PTR and I'll get all the elements from this array so let's see if this function works so all I need to do is pass 10 to it because there are 10 elements let's see if this function works but reverse does nothing so far so you should get um 1 2 3 4 5 6 7 8 9 I'll also add a space here so that we have space between the elements of our array and also before the function returns I'll print a new line because if we are printing something else after displaying the array that should also get reflected so uh you'll not see 1 2 3 4 5 6 7 8 9 10 and the same thing once again you will see it in a new line this time so 7:57:13 that's why I have added this new line here so 1 to 10 1 to 10 because reverse does nothing now listen to me very carefully let me take an image I'll use this image to explain you uh or rather let me take some other image Okay so so this image even looks better uh so let us say we have 0 1 2 3 4 5 6 7 8 as the indices and these are the values okay so we have an array of size nine not eight note that the size of this array is nine if you count the number of elements they are nine because we are starting from zero not from one okay now what will happen is if I want to reverse this array what I'll do is I'll replace 7 with 46 I'll replace 11 with 96 I'll replace 6 with I'll replace six with 16 I'll replace 55 with 45 and at 98 I'll stop now I'll not stop I'll not go all the way till 46 I mean I'll not go all the way from this index to this index 7:58:14 and keep swapping because the moment I reach the midpoint if I swap 7 by 46 11 by 96 6X 16 55x 45 I will have to stop here if I want to reverse the array because if I go even further what will happen is I'll end up reversing the array two times I don't want that okay if I reverse this array if if I start from index zero and go all the way till four what will happen is I'll reverse the array the moment I reach four but if I go beyond four what will happen is I will reverse the array once again and what will happen is the array will not change so let me code this for you so how how do we code this how do we code this so this was the case when number of um elements in the array were odd what if the number of items in the array is even so if we have eight elements the index will go from 0 to 7 let's assume that this 46 and 8 is not there let's assume that we have uh this is this is all we have let's assume this is all we 7:59:16 have let us say this array starts at index0 and ends at index 7 let us assume this is the array now if I want to swap uh the first element with 96 uh I will do that I can swap I'll swap 11 with 16 I can swap 6 with 45 and I can swap 55 with 98 so I can keep swapping till the size of the array which is 8 / 2 - one uh starting from zero I'll go till this index and I can keep swapping let me show you this in code so what I'll do is I'll use the same array and um let me use the same array or or or let me not use the same array I'll use this array what I will do is I have this PTR what I'll do is I'll swap this element which is index zero with index n minus one so I'm swapping 0 with n minus one okay so let me write this here let me cut this from here let me write this 8:00:16 here I'll say swap 0 with n minus one and swap one with what one with n minus 2 and swap index 2 with n minus 3 and so on keep on doing till n by2 so you keep on doing till we'll find out till n by2 okay let's let's say in N by2 we are just assuming we might change this so swap zero with n minus one 1 with n minus 2 2 with n minus uh 3 I can write a General uh swapping equation you can call it I with n minus I min-1 or + one tell me minus one okay so I can say n minus i - one so if the value of I is 2 it will be nus 3 if the value of I is zero it will be n minus one so I'll swap 8:01:18 I the element at index I with element at index n- i - one let's do that let's do that so what I'll do is I'll write this swapping logic I've already told you about the swapping logic but before that let me write the follow Loop I'll say I is equal to z i is less than n let me also pass the size of the array which is n which is 10 in this case and what I'll do is I'll simply swap um I'll do this inside the fall Loop so I'll say swap I with nus i- 1 so what I'll do is I'll say let me also create a variable called temp I'll use this variable for swapping I'll say temp is equal to PT okay I'll say temp is equal to PT and also let me do this instead of you know using the PTR uh as a pointer what I'll do is I'll use ARR let me use or let me use a 8:02:21 itself if not ARR I'll say temp is equal to ai ai is equal to a n - i - 1 and a n - i - 1 is equal to what it will be equal to Temp so this way what will happen is I'll swap AI with a n minus IUS one I've already told you about the swapping logic so um I've already discussed this in previous chapters so I'll not discuss this again so this will simply swap AI with a n minus i- one and that's it that should swap the array so let's see if we are able to um Swap this entire array so let's start from um you know one and we are going all the way till 10 so if I run this program what will happen is you will see that the array is not swapped and the reason is that we started from zero and we went all the way till n but what we want to do is we want to go from 0 to n by 2 minus 1 so I'll simply say I less than n by2 um I'll run this program and you can 8:03:25 see the array has been reversed how does this program work let me explain you so if the number of uh okay let's check does this program work if the number of elements in the array is even so if we have um sorry if the number of elements in the array is odd so we have nine elements here let's see if this works also I'll say nine here not 10 so let me also say nine and let me run this so yeah it works it works for uh odd elements odd number of elements as well it works for even number of elements we have already seen that it works for odd number of elements as well so let's now try to understand how this works how going from n by2 minus one let me make it minus one because we are starting from zero and we are going um starting from zero so we'll say uh I'm going till n by 2us 1 starting from zero so I started from zero I'll go till n by 2 minus one so this is how it works now n by2 for odd numbers will not be a 8:04:25 floating Point number let me tell you so if you have nine 9 divided 2 will not be 4.5 it will be four because uh operation between an integer and integer will result into an integer in C language this is something that you need to understand so coming back to this figure uh the number of elements in this array is 9 okay so the value of n is 9 so if I say 9 by 2 it will be 4 not 4.5 so what will happen is I'll start from here I'll say uh 0 1 1 2 3 4 I I'll start from zero and I'll go all the way till 3 if I say uh I is less than 4 it will go from 0 to three okay so if I go from 7 to 55 this is the midpoint I can swap the entire array if if there is an extra element in the array or let us say if there is um if there are only eight elements in the array what will happen is index will be from 0 to 7 and in that case what will happen is that I can 8:05:25 start from um I can start from zero and go all the way till 3 once again and I'll be able to swap the area you can see so zero will be swapped with seven one will be swapped with six two will be swapped with five uh let me draw let me you know do a full screen snep and show you so what will happen is 7 will be SED with what 7 will be SED with 46 11 will be swapped with 96 6 will be swed with 16 and 55 will be swed with 45 that's it and then I I'll have to stop here I can swap 98 with 98 that will not make any sense I mean you can do that you might skip it um that will not add to add to anything um not even to the computational complexity because this is just one element and that will not grow for larger inputs anyway we are not talking about computation complexity in this video uh so we can start from here and we can you know swap it till here okay so this was one case now is there any way to clean the board uh I 8:06:27 wish there was a way to clean the board so I can simply say I can simply use this let us say that this is not there U let us say this is this is not there so let's assume that these two the this last element is not there so I'm just you know uh I just scribbled it off so let's say this element has uh let's say this array has only eight elements now in this case what will happen is I can swap 796 1116 645 and 55 98 so all I need to do is I need to start from zero and go all the way till three for eight elements as well as for nine elements so all I need to do is I need to go from uh I need to run my fall loop from 0 to n by2 minus 1 so I'll simply write n by2 here this is something that you can you know get a feel of after doing some dry run you can print the value of I you can see how all these things are working by visualizing them like I'm am doing here but I hope that you understood and this might sound a bit intimidating if if you are 8:07:27 starting out but this is a very common question and you will definitely encounter it if you are working with C if you are studying C in your college you might get this question in your exams believe me moving on to the next question which is uh problem number um six if I'm not wrong six yes write a program containing functions which counts the number of positive integers in an array this is something that I want you to do you need to write a function you need to pass an array and you need to see whether two divides a number or not so if two divides a number it is even if two doesn't divide a number it is odd uh but we need to find number of positive integers not positive even or positive odd integers so how do you find whether a number is positive or not simply find out whether it is greater than zero or not if it is greater than zero um it is positive if it is less than zero it is negative so please write your if else uh statements write your function this is very simple problem I want you to do it yourself now 8:08:27 question number seven says create an array of size 3x 10 containing multiplication tables of 2 7 and 9 respectively so this is something that I'll definitely solve for you um and uh this will definitely give you some idea of multi-dimensional arrays so let's solve this problem so 12core pr07 doc I'll put down the boiler plate code and we need to store multiplication tables of 27 and 9 so I'll say um and mul and how many what is the size 3x 10 3 by 10 okay now what I'll do is I'll simply write two for Loops I'll simply write two for Loops one will run from 0 to three I mean 0 to two because I'm saying I less than three and the other for Loop will start from I'll change it to J because I already have I in my outer loop uh I'll start it from zero all the way till 9 here I can use 8:09:28 multiable IG is equal to I'll simply say multiable IG is equal to um some value let us say uh n uh num I I'll tell you what num I is multiply by multiply by what multiply by J okay so I'll simply say this and that's about it but what is num num is an array I want to create an array num which will simply which will simply tell me which number I am on so 27 and 9 so I'll simply say 27 and 9 and I'll tell you how this is working how this program is working so num is a onedimensional array containing 27 and 9 so num zero will be two num 1 will be S and num two will be nine so whenever I need two 7 or 9 I can use num I so firstly what I'll do is I'll start from this number two I'll write multiplication table of two 8:10:28 so in order to write multiplication table of two I'll simply say uh multiable i j is equal to num I star J and uh remember we have written a function to print a multi-dimensional array if I'm not wrong it was here so this was the function so I'll copy this function and U I'll paste it here so after we are done what I'll do is uh three and 10 so three and 10 what I'll do is I'll use uh this function to print all these values so let's let's run this program and see if uh if it works so multiplication table of two is here multiplication table of seven is here and multiplication table of 9 is here but we are missing a number here I don't know why so I'm starting from zero going all the way till 9 so 9 * 2 is equal 18 u in case of 7 I'm St starting from um zero and I'm going all the way till 0 1 8:11:32 2 3 4 5 6 7 8 9 so what this is doing is this is the multiplication table of two this is for seven this is for 9 we have solved it but I don't want my multiplication table to start from zero I want it to start from one and in order to fix this problem what I'll do is I'll simply use j+ one here so I'll use j+ 1 here that's about it if I use j+ 1 here I'll start from two and I'll finish at 20 so let's see if this works so yes I'll start from two finish at 20 start at 7 finish at 70 start at 9 finish at 90 I hope that you are able to understand how this program is working so this was a program on multi-dimensional arrays you'll get all these codes I'll zip them up and I'll give all these codes as well as the notes to you so this is the folder that I'm going to zip I'm going to zip it and it will be all yours okay so coming back to question number eight you have to repeat problem seven for a custom input given by the user and this is very straightforward use scanf use n instead 8:12:34 of you know um or or or rather use uh scanf inside a for Loop to populate this num array okay so you can populate this numb array I will leave that on your creativity you can always do that you can create an array of size 2 3 10 um size of your choice okay let's move on to the next question create a threedimensional are and print the address of of its elements in increasing order and you can do that to print the addresses of the elements in increasing order you need to start from 0 0 0 then 0 0 1 then 0 1 0 and so on okay so if you write a loop like this so all you need to do is you need to write triple for Loop like this and you need to print the address and it will be in uh increasing order okay let me show you that the address will be in increasing order so if I if I do print F and I I do percentage u a space and if I do something like this am person multiple 8:13:37 IG this is going to be in increasing order let me show you so if I uh if I run this program for you so all these addresses are in increasing order 88 92 96 100 and then 104 8 12 16 20 24 28 32 36 you can see all these numbers are in increasing order I want you to see I want you to look and check all these addresses are in increasing order so if you want to print uh addresses of an array in increasing order all you need to do is um simply start uh from i j k or whatever is the dimension of your array and put it inside the fall Loop and simply print the address it will be in increasing order okay I hope that you understood it let me simply comment it down uh so this was about uh multi-dimensional arrays and uh arrays and pointers and the relationship of array and pointers so yeah we are done with chapter number seven let's move on to chapter number eight now and we'll look into some more concepts of C 8:14:39 programming language let's move to chapter 8 now before we move to the next chapter we are going to look at this project which is snake water and gun a very simple project but yet very powerful game to code in C programming language so if you don't know what snake B gun game is let me give you uh good amount of idea of what this game is and how you can play this game so snake water gun or rock paper scissor is a game most of us have already played this game during our school times so I I I sometimes even play it now to be very honest but we have to write a program capable of playing this game with you so basically if you select snake and the computer or the all the other person playing the game with you has selected say water snake will drink the water and snake will win so if you select snake and the other person selects the gun gun will shoot the snake off and snake will die and hence the gun will win if you play it with water and the other person is having the gun gun will actually get drowned inside the water and will stop 8:15:39 functioning and hence it will be a win for water so what we have to do is we have to make the computer play this game for you so how do we do it let's see how we can do it so the very first step in playing this game will be to receiving the user input so what I'll do is I'll open this folder in visual Studio code and I'll create a file called game. C I guess I have not enabled C++ extension I'll just check whether they are enabled or not so C++ extension is enabled and also code Runner is enabled cool everything is enabled we have we are all set so I'll put the boiler plate code in now what I'll do is I'll uh start by taking or receiving input from the user I'll ask the user to give me the character it can be snake s for snake W for water G for gun so let's start so I'll create two characters I'll say car U and Comm U for U Comm for computer okay now definitely I'm going to generate a random number why why am I 8:16:41 generating a random number I'll tell you why so we have already seen how to generate a random number but we are generating a random number to randomly select snake water or a gun for a for a computer so what I'll do is I'll say Sr time Zer this will actually set my random seat to Time Zero which will be a random number always and now what I'll do is I'll simply say in number is equal to Rand person 100 + 1 okay so what this will do is this will simply give me a random number starting from one all the way till 100 any random number can be provided to me inside number I also make sure that I have time. H included because this is the file that we need uh if we want to use all these functions also I'll include stdlib.h because this is also something that we need if we are generating a random number what I'll do is now I'll ask the user what do you want to uh choose so I'll simply say 8:17:41 uh select s or or rather enter I'll say enter s s character for snake and W character for water and G character for gun so you have to do this this is pretty straightforward now what you'll do is yes a good old scanner function and percentage C for character and person U it will be a character for you so you might be snake or water or gun you can be anything okay but computer can also be snake water or gun you will not know whether the computer is snake water or gun how will we decide whether computer is snake water or gun I'll use a bunch of IFL statements so if the number generated out of one and 100 is less than 33 I'll randomly give computer a snake otherwise what I'll do is else if 8:18:46 else if the number is C less than 66 if it is less than 66 which means that it is definitely greater than 33 if it didn't went inside this if Loop sorry if block uh it will definitely be greater than 33 and lesser than 66 in that case I'll randomly assign it water otherwise in other cases I'll assign it gun so I'll say else no condition inside else I'll say else computer is gun now you can shuffle them around you can do water gun snake or snake gun water whatever you want okay that's that's your choice now what I'll do is I'll simply say int result is equal to snake water gun which is going to be a function and that function will take you and computer as the input and this will either declare you as the winner or you as a loser and computer as the winner so if this 8:19:47 result is equal to zero I'll simply say that game Draw I'll say printf game drawn okay otherwise if this result is Cal to 1 what I'll do is I'll say print F oops I made a mistake here I need to close this if block here format document I have formatted this document I'll use contr S to save I don't like this I need my if statements and the curly bracket in one line now if this happens what I'll do is let people put a back slash in here as well I'll simply say if result is equal to one you win okay I'll say you win okay otherwise definitely you lost okay so I'll say you lost you lost okay or you lose okay now what I'll do is I'll simply show you at the end of the 8:20:49 program what character did you choose and what computer choose so I'll say print F I'll say you chose percentage C and the computer chose percentage C okay uom because at the end I'll definitely give you a good idea of why you won or why you lost now our entire task or the entire game revolves around this function which is uh snake waterer gun game it's going to return an integer and it will take two integers sorry characters U and comp now definitely If U and comp are equal I'll immediately return zero which means that game drawn because if you both selected the same characters the game needs to get drawn okay otherwise I'll say else I'll say if else 8:21:52 if you are Q are snake and let me add few more brackets so what are the chances of you winning what are the possibilities that you are going to win what are the possibilities that I should return one if you are snake and computer is say uh computer needs to be water for you to win this is a win situation for you I'll put in another win situation for you or another win situation for you which is going to be let me put in W here for water and U is a variable it says expression must be a modifiable l value what's wrong here okay so this should be double equals to this this will work now so this is a win situation for you another win situation for you is to select water to select water and if computer selects gun by luck you will once again win another win situation for you is going to select gun and in that 8:22:54 case computers should select snake for you to win in all these cases you are going to win otherwise you are going to lose so I'll say return minus one minus one for loss okay so let's run this program now and let's see if this program works so if I run this program so it says ENT s for snake let me choose snake it says you lost because computer chose gun my bad once again I'll say water W it says you lose because computer chose snake and it drank your water my bad why am I so unlucky I'll choose snake and it says you win because computer chose water and you drank computer's water now it's asking me for another thing gun game drawn computer also chose gun now once again W you win because computer choose gun and you actually drown computer's gun great great great so this was the game I hope you enjoyed it and uh you can format the you know code as you want I want my code 8:23:55 to be very very clean like this I don't want a lot of new lines inside my code which is why I'm formatting it like this so I hope you like this game enjoy playing this game and also play this game with your friends they will also enjoy it you can also refactor this game to play it with your friends so you you can always you know do something so that you you can play this game with your friends so now what I'll do is I'll move on to the next chapter and we are going to explore some more concepts of C so topic of today's string in chapter 8 we are going to learn about strings and strings is something that you are going to use a lot not only in C programming language but in programming in general you're going to use a lot of strings so let's talk about what a string is it's a collection of characters let's talk about string in layman terms what exactly is a string it's a collection of characters so if I write my name here it will be a string so if I say Harry it's a string if I say um let us say John it's a string if I say Kane it's a 8:24:56 string okay so you you know what a string is but let's talk about what exactly and how exactly are strings represented in C programming language so this topic is really very important so when I talk about C programming language when I talk about programming a string in C I can say that a string is a 1D character array terminated by a null we know that the collection of items or rather collection of similar items in C programming language is called string so a string is a 1D character um or rather 1D character array terminated by a null character okay so what exactly is a null character so back slash Z it's a null character and we are going to talk about what exactly is this null character and how we can use it but let's let's try to find out how we represent strings and see null character is used to denote string termination characters are stored in continuous memory locations so let us 8:25:58 say this is my memory so I'll simply Google array uh oops images and uh let's talk about this image let us say this is a string u b f d a e c one bite is going to take one character and it is indeed taking one character you can see that 200 2001 202 these are contigous memory locations so what exactly is the string if I start reading from here the string can be u in memory it can be U it can be ubf where does this string end I have no idea if I keep on adding character to this string so in order to Mark the ending point of the string we say that wherever you encounter a null character which is back sl0 the string ends there so if after c i store a null character which is nothing but a single character back sl0 is a single character this is something that you need to remember it is not two character it's uh a null character okay back slz is one single 8:26:59 character uh which is called a null character it's just that we need to type two characters in the keyboard to type this character but it's actually an escape sequence character uh let me search this for you uh escape sequence so what is back sl0 escape sequence let me also do this so This escape sequence is a single character it it is used to denote string termination okay so I hope that you understood how this uh Works Let's uh try to move further in the notes and let's try to understand more on strings since strg is an array of characters it can be initialized as follows and at this point I'll come to my visual studio code I'll write the first program character array.c I'll put in the boiler plate code I'll zoom in a bit and what I'll do is I'll simply say in ARR or let me call it string let me call it in s str1 so let's say this is St 8:27:59 str1 so what I'm doing is I'm saying um uh sorry I need to make it car because string is collection of characters so I'll simply say h I'll simply say a then I'll say R make sure that you are using single quotes because if you're not using single quotes then uh it will not be a character so in order to uh write a character literally in C you need to use single codes do not use double codes if you're coming from a language like JavaScript where single Cotes and double quotes can be interchangeably used do not get confused in C programming language you strictly use a single code for characters okay so h a r r y and this is the last character but no this is not the last character I need to write a back sl0 to tell C compiler that my string ends here I don't have to write anything else after y that's all I needed I needed 3 + 2 five characters but I will need six characters to store the string why do I need need six characters one character for marking the termination of the string one character 8:29:00 for storing the null character which is back sl0 so how do I print this now I can print a string by doing print F percentage s and I'll simply write St str1 and yes I I'll be able to print this string so uh let me enable the extension so I'll go to extension I'll enable code Runner by default I I have disabled code Runner so I'll simply enable it in workspace if you can't see this run button install this extension and enable it uh you can see that Harry has been printed on the screen so this is how um strings are represented in memory and this is how you can write a program to um print a string so percentage s is format specifier for Strings okay let's move further in the notes let's see what uh notes are saying so since string is a is an array of characters can be initialized as follows we just did that there is another shortcut for initializing strings in C language okay so this was a bit of a 8:30:04 pain because we have to write single quotes and then we have to write these curly brackets yeah this is a bit of a pain so is there any shortcut to Mark a string can I not directly write Harry yes there is a way so in order to directly write Harry you can do something like this you can say int St str1 is equal to double quotes Harry and this will automatically add a back sl0 at the end uh or rather at the end of this character array okay not at the end of this string but at the end of this character ARR so uh in the memory this is actually stored like this so if I save this and run this program I get the same thing which I got by running this this is just a syntactic sugar this is simply uh to make you love this programming language uh to make things easier for you make sure that single quotes are used to Mark a character and double quotes are used to Mark a string so this is how double quotes are used to 8:31:06 create strings so let me move further in the notes and let's see what else is there so there is another shortcut we just talked about this and in this case C adds a null character automatically now let's see how strings are represented in memory so one byte is one character in fact one character is one bite and one character just needs a single bite in memory so memory locations are contiguous blocks of memory when we talk about strings or character arrays so what happens is if I want to store Harry I'll simply say h a r r y and at the end will be a null character to Mark the termination of this string so if the memory location of H is 82210 the memory location for a will be 82211 and so on so the next memory location is previous memory location plus one because string uh contains characters and character takes single bite of memory so this is how strings are stored in memory really straightforward let's now talk about 8:32:07 this quick quiz where we have to create a string using double codes and print its content using a loop so this quick quiz contains a bit of a catch I mean you don't have to use percentages uh let me say quore quiz. C uh so you can easily print a string by using percentages we just saw that but this question demands you to print the same string using a fall Loop because we know that deep within this particular uh let me enable the extension C++ so C C++ intellisense this was not enabled now it is enabled so I can use the Snippets so what I'll do is I'll use this snippet for for Loop and I'll say in I in I is less than length of the string what is the length of the string I know that length of the string is six because um five for the characters one for the null character and the index will go from 0 to 5 now what I can do is 8:33:10 I can do something like this print F percentage C for character and I can say St str1 I this will print the string uh sorry the character so this will first print H then a then R then r then Y and then the null character so let's see how this works so uh oops I I need to remove this so if I run this you can see H then a then R then R then y has been printed so this is how the string is printed so h a r r y has been printed and finally the null character is printed so this is how you can you know solve this question now let's see how strings are printed I just told you how strings are printed a string can be be printed character by character using print F and percentage C so percentage C is the format specifier for printing a character you can print it character by character using percentage C so percentage c will print 8:34:10 one single character but there is another convenient way to print strings in C which is percentages we just saw that okay so you can either print a string character by character by iterating this array at the end of the day string is an array and you can also use percentage s if you want to give the address of first element of the array which is St in this case so this will print the entire string how this works behind the hood is this will simply keep on going to the next character next character and so on until it incounters a null character which is back sl0 so I hope that you understood how strings are stored in memory and how to print strings in your C program now let's move to the next topic which is taking string input from the user user how do you take string input from the user taking string input from the user is really very easy all you need to do is use percentage S as the format specifier and you can use scanf so this particular character array can store 50 characters but you can 8:35:12 create a string of length five 6 7 34 30 it will be working fine as long as the number of characters in your string are less than 50 because this particular array can hold a maximum of of 50 characters including the N character so this is something that you need to keep in mind so let's take input from the user input strengths. C I'll put down the boilerplate code and I will say car ch1 and let's say the max um the max limit of holding characters for this character array is 45 I write a print here I'll say enter your name okay enter your name and see the magic and also I will write back sln now what I'll do is I'll simply write 8:36:13 scanf and percentage D sorry percentage s and I'll say ch1 now I can print the string using printf once again and I can do something like this your name is percentage oops your name is percentage s and ch1 and then if I save this program and show you it is saying enter your name and see the magic it says your name is Harry so this is how this program works let's now move back to notes and read more on it scanf automatically scans uh adds the null character when the enter Keys press so the moment you press the Enter key scanf will automatically add the null character now there are two notes that I've written here the string should be short enough to fit into the error this is something that you need to make sure sometimes c will not throw any error but your program will not work as expected so do not wait for the error you might 8:37:14 not get the error you might have uh you know reached a memory location which you are not allowed to reach and maybe due to that you might get some error do not wait for that to happen rather make sure that your strings are able to hold the number of characters you intend to hold okay so scanf cannot be used to input multiword strings with spaces you cannot use scanf uh to input multi-word strings and I'll show you an example so if I run this program and let us say I try to say Harry by you can see that your name is Harry's sprinted and not Harry by so what I want to do is I want to also make sure that my name contains spaces I cannot do this with scanner because the moment scanf sees a space it will actually think that you are done entering the string and you'll not be able to uh print your string or store the string uh as is okay so what is the solution for that we'll see the solution for that and the solution for that is gets and puts so gets is a function 8:38:16 which can be used to receive a multi-word string so if you want to receive a multiword string do not use scanf rather use GS so let's create a program here I'll name it 04 uncore gets puts do c I'll put the boiler plate code here I'll close this and also what I'll do is I'll simply copy this program I'll I'll copy this program as is and I'll change this scan of call to get SC can I now add a multi-word string I'll say multi-word string yes it works so I can use gets in place of scanf if I know or if I suspect my string to contain uh one space or more than one space so if you think that your input string might have spaces in it you you might consider using gets okay coming back to the nodes gets is a function 8:39:17 which can be used to receive a multi-word string so this is a very basic function it can be used to receive multiword string um there is an example here the entered string is stored in St so St is a character array and gets will make sure that even if your input contains a space your string is stored in the appropriate Place multiple get calls will be needed for multiple strings so if you want to store multiple strings you need multiple get calls likewise puts can be used to Output a string uh you can use puts as well to Output a string prints the string places the cursor on the next line so puts will print the string first and then it will place the cursor on the next line I'll show you so there's a difference between printf and inputs so I'll show you this was using or let me say done printing string so if I'm using print F to print the string let's just print the string and not print the name is so I'll save 8:40:18 this program I hope that this will work I just place the semicolon I'll say hey hi so you can see hey hi done printing the string there's no new line here but if I use puts let me show you how puts works I'll simply say ch1 if I use puts the cursor will be placed on the next line so if I say hey hi oops you'll see that hey high and then done printing string is placed on the next line so this puts call will simply place the Curson on the next line so I hope that you understood what is the difference between puts and printf now declaring a string using pointers this is important so we can actually declare strings using pointers like this we can say cashar PTR is equal to Harry this tells the compiler to store the string in memory and assigned addresses stored in a character pointer so uh this will 8:41:18 actually uh find some space in the memory and it will give the address of the first character to PTR so PTR will hold the address of H okay I hope that you understood this syntax as well now once a string is defined using this it cannot be reinitialized to something else so if you do something like car St is equal to Harry you cannot change this string but a string defined using pointers can be reinitialized you can say PTR is equal to this or you might want to change it to something else so this is something that you need to keep in your mind now standard Library functions for Strings now C provides a set of standard Library functions for string manipulation you might write your own functions to manipulate your strings but sometimes that might get cumbersome so there are some standard libr functions for string manipulation we'll look into all those standard Library functions some of the most commonly used string functions are St Str Len which will simply give you the length of the string 8:42:19 you might want to implement your own s Str Len which will simply scan your character array and it will keep on Counting the number of characters until a back slash Z is encountered okay so you can do that but again there is a builtin function so this function is used to count the number of characters in The String excluding the null character so let me show you so I will simply create a new file I'll say 05 uncore string. C and what I'll do is I'll simply put down the boiler plate Cod code and also I will uh use this syntax to initialize my string now what I'll do is I'll say print F the length of this string is percentage D and I'll say SD Len sd1 so this should actually give me uh the length of this string so if I run this 8:43:21 program uh I am getting uh a warning it's telling me that please include this Library this header file so I'll include this header file which is string.h in order to use string Methods you need to include this header file which I've just included this is just a warning your program still works you can see the length of the string is five but again it's a good practice to add the header file when you are working with string functions so the program compiles without any errors and warnings you can see the length of this string is five what if I change change CH this to Harry with with a double y you will see the length of this string is six so in this case the length of the string is six I hope that you are able to uh understand how these things are working now let's talk about the other function which is St strcpy this function is used to copy the content of second string into the first string passed to it so if I have source is equal to Harry and I create a Target I can actually run St strcpy 8:44:22 Target source to make Target hold Harry let me show you so um I I I'll create another program I'll say s string sdln I'll create another program 06 uncore string St strcpy and I'm not going to type everything once again I'm going to copy this from here so I'll now say care Target and let me create a Target string with length 45 let me call this source and uh let me do something like this what I'll do is I will say St strcpy and I'll do Target and Source did you notice that when we didn't had the header file we were not getting the suggestion now if I write Str Str I'm getting all these functions uh in the suggestion so this is also one of the reasons to include the header file I'll save this program I'll say 8:45:25 the Target string is percentage s and I'll try to print the Target and you'll see that Target now contains Harry so if I run this program Target string is Harry why because this particular string was copied from source to the Target I hope that makes sense coming back to the notes again um St strcpy is something that we just saw Target string should have enough capacity to store The Source string obviously so if I create a string with um with four I'll start seeing weird behaviors you you might not want your program to misbehave so make sure that Target string has U sufficient character limit to hold the source string okay moving on to the next function which is St strcat this function is used to concatenate two strings so if I want to join two strings uh let us say I have S1 and I have S2 S1 contains hello S2 contains Harry and I want um to merge 8:46:28 these two strings so if I say s Str cat S1 S2 S1 will now contain hello Harry so let me show this to you s strr cat string concatenation 07 uncore oops SDR cat do c and let me copy this program I I'm starting from this program just to save some time uh so let us say Source contains 56 characters and Target contains this string let's say it contains is a nice boy and I say St cat Source Target so I want to copy everything to source so Source plus Target will be uh assigned to source so so if you want to you know get a rough idea of what is happening this is 8:47:29 something which is happening do not take this literally it's it's roughly which is happening so I hope that you understood the gist of it so if I now try to print the source string I'll say Source string is Source The Source string will be changed and it will now contain Harry is a nice boy if I add a space here Harry is a nice boy so Harry is a nice boy okay okay so these two strings are concatenated this is a program to concatenate the string and it will update sord string not the Target string this is something that you need to keep in your mind now the last function that we'll discuss in this chapter is s strcmp and this is last but not the least you might Google some other string functions there are a lot of string functions but these were some of the most important ones now let's see this function is used to compare two strings it returns zero if strings are equal so if you pass two strings and both of the strings are equal this function will return zero negative value of first strings 8:48:30 mismatching characters SK Sky value is not greater than second String's corresponding mismatching character so if far comes before joke in the dictionary we'll see a positive value but if that is not the case then we'll see a negative value let me show you so I'll use St strcmp let me show you st strcmp the new program so I'll say 08 _ St strcmp.c I'll start with this uh oops I'll start with this code St strcmp so let us say source and Target are two strings and let me say uh Akash also let me save this program let me remove this and let me say the SDS CMP returns percentage d this will return return an integer so STDs CMP will compare two strings let me 8:49:30 compare source and Target so tell me which of these words will come in the dictionary before Akash will be the one that you'll encounter if you turn the pages of dictionary you'll encounter this word before this so what will happen is if I run this program one is returned but in some of the compilers a positive values returns it might be one it might be some other positive value it might be a random positive value but if I change this to a a Harry this word will come in the dictionary before Akash so let me change this to small case let me run this for you this still returns fun because the S key value of uh capital letters is smaller than the SK value of small letters which is why this is happening if I change this to small letter you will encounter minus one and then I'll show you the case oops I selected something and run I need to you know I should not select something and run otherwise it will try to run run that particular snippet and this is the behavior of code Runner what it does is when I'm selecting something it will simply put that code inside temp code 8:50:32 Runner do C file and it will try to run it I don't want that so if I make this a a Harry Akash minus one is return because this particular word will now appear in the dictionary before this one okay so this is how this works but let's not see it from the dictionary point of view what we will see it from is we'll see it from the S key value point of view let us see say the Sky value of this character and this character is the same which is now it will check the Sky value of this and this so which Sky value is greater this Sky value is greater so it will return minus one so I hope that you UND understood how this is working what if I make both of these strings as the same what if I make it a a a a zero will be returned so let me show you so zero is returned and this is what I've written in the notes as well I hope that you like this chapter and uh you can now use strings in your program uh if this course was enjoyable make sure you leave a comment below and tell me that this course is something you are enjoying so that I can bring more 8:51:33 courses like this in the future but for now we'll move on to chapter 8 practice set and we'll solve some problems on strings let's move on to chapter 8 practice set now so now what we'll do is we'll start with the practice set and uh we'll start discussing the problems in the practice set so this is the practice set which I've created for you and the first question is an McQ it seems which of the following is used to appropriately read a multi-word string and the answer is pretty simple I want you to comment in the uh comment section put the Tim stamp please and tell me which is the correct answer so if you guessed it correctly in fact if you got it correctly the answer is a gets gets is the function which is used to appropriately read a multi-word string in C language so let's move on to the next question so the next question says that write a program to take string as an input from the user using percentage C and percentages confirm that the strings are equal so we need to write a 8:52:34 program firstly using percent C format specifier and then using percent s format specifier and we need to confirm that the strings are equal Let's uh try to attempt this problem so what I'll do is I'll create a program number 9 which says 09 uncore p02 doc because this is problem number two so I'll put in the boiler plate code and uh I'll simply say uh let me start with a print F statement what I'll do is I'll say enter the string you want to uh input okay so this is a string that you want to input uh so what I'll do is I'll simply use a fall Loop and I'll say inti is equal to zero and in I is less than five let's assume that the length of the string is five and I'll also add using perent C and uh I'll not write using 8:53:36 percent C using percent C because I don't want to print anything here so what we'll do is we'll keep on pressing the enter key and also I'll say enter the string of length five you want to input using person C now what I'll do is I'll simply use my for Loop let me use the all Loop and what I'll do is I'll say scan F person C so we in the first case what we are doing is we are taking the input character by character so I'll say m personent and let me say car I or St where St is a string so let me also say car st5 this will declare a string with name STD of length five um in fact I'll have to make it six because length of the string is five also I need to add a null character to it so I'll keep on doing this and at the end what I'll do is I'll simply say st5 is equal to sd5 is equal to uh null character back sl0 8:54:37 so let me run this program and the moment I run this program and try to enter h a r it it stops me what is happening here why is it not allowing me to enter my name my name is of length five the string can hold five characters what's wrong uh that I'm doing in this program let me explain you the moment I try to run this scanf and I try to store H inside STI which is st0 initially what will happen is I'll press an Enter key and that Enter key will actually be taken by the next scanner which means the next character will be the enter key so what is happening here is so when I say this a for Loop and a scan of inside for Loop listen to this very carefully because this this is very important concept and if I have Scan of inside a loop for Loop while loop or it can be any kind of loop do while loop this scanf will actually take this character whatever I'm typing using my keyboard 8:55:38 will be stored inside this so initially I will be zero so inside st0 this particular character whatever is entered by me through the keyboard will be stored but after I press that character I'll definitely press enter key that enter ke key will be catched by ST1 I don't want ST1 to catch that enter key so what I'll do is I'll write F flush STD so what this will do is this will flush that Enter key so the moment I press enter key to finalize the character that I want to store in st0 that Enter key will be removed from the STD ion which means that next time when this runs when this scanf runs for I is equal to 1 it will take the fresh character and not the enter key I hope I was able to explain you this so basically to flush the enter key that we have pressed I have to write this so now if I run this program now I can say h a r r y and yes I can store Harry inside 8:56:41 St now what I'll do is I'll simply say comparing this string with Harry gives percentage D and percentage D is nothing but the output of St strr CMP so I'll compare this St string with Harry now let me also include string. H because this is important we discussed this in the last um last chapter in in fact in the same chapter in chapter 8 now if I save this program and if I try to run this program and I enter Harry h e r r y so comparing this string with Harry give zero which means Harry and this string is the same so if I let us say type something else you can see that comparing this string with Harry gives minus one because the string was not the same so this was a bit you know vake question um 8:57:42 I hope that I was able to explain you the concepts maybe this statement uh might not lead you to the correct answer but I hope that I was able to explain you some of the concepts write a program to take a string from the the user using percent C and then using percent s so I should have taken it using percent s but that's fine you can we can even use these double codes uh this works as well you can refactor the program in in u the way you want now write your own version of stln function from string. this one is going to be interesting let me tell you so let's Implement Our Own St Str l so 10or pr03 so I'll say pr03 Doc so what I'll do is I'll start with this program so that I have a string and I'll say Care St is equal to Care St is equal to Harry also what I'll do is I'll write my version of St strn and what I'll do inside that St L is I'll return in St 8:58:42 Str Len and I'll remove this uh my St let me call it my St L and what I'll take is a character pointer and I'll keep on scanning this character pointer until I encounter a back slash so I'll say cast R PTR is equal to St and I'll keep on incrementing this pointer I'll say while this PTR is not equal to back sl0 so the moment this PTR uh you know turns out to be back sl0 what will happen is this V Loop will stop working so this pointer is currently not equal to or rather I should say starp the value of this pointer should not be equal to back sl0 it should not be null character I'll keep on incrementing this pointer and also I'll keep on uh incrementing length length Plus+ so initially the length will be zero so I'll say in length is equal to zero and 8:59:44 I'll return length the moment this uh y Loop exits I'll return uh sorry not zero I'll return length let's see if this works the length of this string is I'll say the length of this string is percentage D and um what I'll do here is I'll simply write the length of this string is perc D and it will be my STL Len and I'll give this particular pointer which is St and let's see if it is able to find the length so if I run this program I'm getting some error I think I forgot some brackets uh so this is the bracket for this okay so now the program should work and it says the length of the string is five let's see if it is able to return the correct answer the length of the string is six yes and if I even further increase my string you'll find the length of the string is 15 so this works 9:00:44 this my stln works pretty well I hope you understood how I implemented this let's move on to the next question which is question number four it says write a function slice to slice a string it should change the original string such that it is now the sliced string take M andn as a start and ending point for slice so this question simply says that if your string is this you might want to change this to say this a part of the string so in order to do this what you'll do is you'll keep on copying SDF dsfs into these initial characters and finally what you'll do is you'll terminate the string with back sl0 I want you to take this as a challenge and solve this problem because U we have already wrote a program on swapping um two numbers and reversing an array so this is exactly the same thing I want you to try this on your own and um also try to implement your own version of St strcpy because this is also very simple 9:01:46 this is also you know based on arrays we have already solved a lot of problems on array I don't want to repeat um a lot of Concepts that have been already discussed here the next question says that write a program to encrypt a string by adding one to the key values of its character and let me solve this for you uh so this is problem number six so 11 pr06 do c and what I want you to do is change a string such that it is encrypted uh so I'll simply say car name is equal to so so the car name is equal to Harry and I want to change this string so that it is encrypted so I'll use a for Loop I'll say I is equal to Zer I is less than six because um or rather let me make it five because there are five characters I don't want to touch the null character now what I'll do is I'll say name 9:02:46 i++ what this will do is this will increment one to the S key value of this character and if I try to print this string I'll say the string is percent s Harry oops name and that's about it if I run this program you'll see the string is IB ssz nobody can tell this is Harry but it's actually g h i a b comes after a then uh s comes after R and Z comes after y so we have kind of you know encrypted the string but this is not um you know SA form of encryption in my opinion uh but again you can use it as some sort of um encryption okay now write a program to encrypt a string by adding one to the skq value of its characters write a program to decrypt the string encrypted using uh this problem so all you have to do is decrypt the string by you know reducing one from the SQ value so this is exactly the same 9:03:47 thing all you need to do is you need to say name I minus minus and the problem is solved question number eight says that write a program to count the occurrence of a given character in a string what you need to do in this question is uh you need to count the occurrence of a given character which is um let us say I have a character you can iterate the array and keep on checking whether that character is equal to the uh character of that particular iteration in the string so you can count the number of characters in a given string it's a very basic question you should be able to solve it write a program to check whether a given character is present in a string or not this is even more simpler because all you need to do is iterate the array and once you find that particular character you are looking for you can break out of the loop so even if the length of the string is 17 and the third character in the string is the character that you are looking for you can find that character and break out of the loop you don't have to check further characters I hope that this practice set was pretty simple and 9:04:47 the concept of string was exactly the same as the concept of array but once you understand where to put the null character and how all these things work you'll be more comfortable in solving all these questions so I hope that uh this practice set was useful to you now we'll move on to the next chapter and we'll further explore the concepts of C programming language let's move on to chapter 9 now now that we have done so many things in C programming language let's look into structures and this is something that people think is also a complicated topic after pointers but let me tell you something something structure is very beautiful concept when it comes to C programming language if you start understanding what a structure is that will really you know build the foundation of programming Concepts that you are going to hold in the future so the concepts of object oriented programming and the concept of classes and objects that you are going to learn in maybe C C++ Java not in C C++ Java uh 9:05:48 JavaScript and so on those kind of Concepts will seem very easy to use so structure is something that was introduced decades ago but still it's going to be very important concept for you so let's try to understand what the structure is so let us say we have a array okay we have an array and we want to store uh two numbers inside that array we can do that right we can create an array and we can start storing all those numbers inside the array and uh that will work but let us say I want to create a collection and inside that collection I want to put a number and a string so if we talk about an array and if we talk about a string what happens inside array is they hold similar data either you can create an array of integers or you can create an array of strings or you can create an array of characters you can create an array of U any any kind of uh you know data type that exist in C programming language but all those data types needs to be same 9:06:51 but what if I want to group together together a collection that contains a dissimilar uh you know kind of data for example uh let us say I want to hold a number a name let us say an address a phone number all these kinds of data are not of the same type some of these data types are integers some of these data types are going to be strings some of these data types are going to be uh floating Point numbers as well so what we want to do is we want to group together dissimilar dat data and structure is something that helps us do that so structure is nothing but a way for us to hold the similar data in C programming language so this is something that we need to understand first now let's look into the Syntax for creating structures in C programming language so what I'll do is I'll simply start by writing a program and I'm going to write a very basic program so that you can understand how structures work in C programming language so let me create the very first program and I'll 9:07:51 name it structure or struct uh and what I'll do is I'll simply say boilerplate code so that I can put the boiler plate code inside my program now what I'll do is I'll use the syntax and start creating a structure so this a c structure can be created as follows as you can see I've written a struct keyword struct is a keyword that is used to create structures in C programming language then employee is a name chosen by me it is just like a variable in C programming language just like we create in a is equal to 7 we can create int employee equ = to 32 in a very similar fashion what we can do is we can say struct employee and we can start creating this structure so let me show you something what I can do is I can write something like this and I'll say struct employee now I can say in um salary let us say the salary is an integer and what I can also do is uh I can say float hike and these two are um together a different data types integer 9:08:53 and floats hike is a floating Point number salary is an integer let us say I also want to create a favorite character of that employee for some Fe reason I want to store uh the favorite character of this employee so all these three types of data types are different okay uh in this example what I've done is I've taken name his name as an array we can do that as well so let me refactor this and let me say that the employee will have some name and that name will be stored inside this variable and it can contain 10 characters at most okay now what I'll do is I'll save this program and you can see here that this declares a new user defined data type so employee is just like an integer now if that makes sense so employee is a data type in C programming language for this program so I can say something like this I can say employee E1 E2 just like I can say int A1 A2 so something which is really very important here is a semicolon I need to put a semicolon in 9:09:53 here and if I run this program let me show you something where is my code Runner so I need to enable cc++ extension um I have disabled it by default because uh you know anytime I open some window what happens is it it it it loads all these extensions and my computer starts working slow so what I do is I usually uh disable all these Extensions by default so I'm searching for code Runner I'll enable it for this particular window and I'll close it and I should get this button and once I get this button what will happen is so it is saying that cannot find the PATH uh what's wrong with this structure struct do c uh everything seems to be fine if I run this uh this is not working let me load the visual studio code window once again I don't know what the problem is so I renamed my program and it worked I think I accidentally added a back slash while I was naming the program but anyways it works so if I show you this program it runs without any errors and warnings which means that this syntax 9:10:55 works fine so what I've done here is I've created a custom data type now if I want to use this custom data type I'll have to write something like this we can use this user defined data type as follows I can say struct employee even1 now even is an employee so let me show you something I can I can do something like this I can say int a is equal to 45 and I can say struct employee employee Harry and Harry is now a variable of type employee so I'll have to write struct employee completely I cannot say employe Harry uh this is the rule so I can now do something like this I can say harry. name is equal to Harry and I can say Harry do hike is equal to 5.3 I can say let me write Harry here uh I can say Harry dot salary is equal to 10 so I can do something like this okay let 9:11:56 me let me add a couple of zeros here okay that sounds good okay so I can say Harry do name is equal to Harry I can do all these things what is it telling me uh it is saying that expression must be a modifiable l value so this is throwing me an error because it is just telling me that uh harry. name is simply the address of the first character of this array and you cannot assign it to Harry so what I'll have to do is I'll do something like this I will uh use St strcpy and I'll simply say name and I'll say Harry do name and then what I'll do is I'll say Harry and this should work and what I'll also do is I'll import string.h now let me run this program and let me show you that this works so I have not done anything so far but let me tell you that I can use all these uh variables from employee structure and I can print them out on the screen let me show you what I mean 9:12:57 by that I can do something like this I can say the name of the employee is let me say percentage s and I'll say harry. name then I can also say that the salary of employee is percentage D I can say Harry do salary then I can do percentage F I can say hike of the employee is Harry dohke because Harry's the name of the employee variable just look at this struct employee as a new data type just like you are looking at integers just you are looking at floats look at the struct employee in a very similar manner struct employee can now be used to declare structures or other structure variables of type employee okay so I so Harry is just like a variable the uh only difference between this variable and the variables that we have seen so far is that there are variables inside this variable so Harry has three variables I can use these three values 9:13:58 so it's a collection of three different things name salary and Hike okay I hope you understood that there is no other way in C programming language using arrays using string or using some other method to group all these things together so this is something that was brought into the C programming language to solve this problem okay now if I run this program for you uh you will see uh all these things printed on the screen let me also say back sln so that you you know you can see the new line after this is printed so the name of the employees are salary is uh I don't know how many zeros I added a hike of the employee is 5.3 and this is amazing okay because this solved a very big problem for us which was grouping dissimilar types so dissimilar types of data was not grouped by us so far now we can group the similar data types using structures Okay so semicolon is important I have written these notes really very beautifully for you make sure you download all these notes so a structure in C is a 9:15:00 collection of variables of different types under a single name so what is that single name single name is Harry Harry is a structure employee type structure which I have created so this is just like a data type this is just like a like in float or double for that matter and Harry is the name of the variable just like a now I haven't used this a but I just wanted to tell you just like we have a we have Harry okay so coming back to the notes let us see what this practice set is telling us write a program to store the details of threee employees from user defined data use the structure declared above so what we want to do is we want to use a bunch of scan fs and a bunch of um printf statements to take the user input and then display it on the screen so let's do this okay let's let's try to do this what we are going to do is we going to use the same program but I'm going to create a new program I'll say quick 9:16:02 uncore quiz do c I'll add the uh boiler plate code but I'll not add the boiler plate code I'll simply copy this program what I'll do is I'll simply say Harry then I'll say Kish then I'll say Rohan okay so what I want to do here is I want to take the information from the user so I'll say enter the details for Harry enter the details for Harry now I'll use back sln and I'll also put a semicolon now I'll simply say enter name so let me use U enter name for Harry then I'll say enter salary for Harry then I'll say enter hike for Harry and while I'm doing all this I'll use scan a function I'll simply say scanf and I'll simply say percentage s for the name and I'll say harry. name okay now 9:17:02 what I'll do is I'll simply copy this scanf I'll do the same for salary so salary is going to be an integer I know I'm doing a lot of typing but that's because I want you to get this concept very clearly so I'll say percentage F this will take the floating Point number entered by the user and store it into Harry dohke now let me get rid of all these things now I have to do the same thing I have to do the same thing let me get rid of this one because we'll need this one now let me do the same thing for other three employees so let me do control D now I'm copying a lot of code which violates the dry principle so we are not going to worry about that dry principle is nothing but do not repeat yourself dry principle says that do not repeat yourself something that you have already done should be kept by you and it should be reusable in a way that you don't have to reinvent the wheel again and again okay so we are going to do the same thing for 9:18:02 Kish I'll replace Harry with kiten and I'll also replace all these structure um variable names now I'll do the same thing for Rohan so let me do the same thing for Rohan I'll say Rohan do name Rohan do salary Rohan doik and we'll also replace all these values with ran okay let me use controls so we have taken user input so far now what we'll do is we'll print all these things so Harry Rohan and kishan okay so these are the three employees okay now what we'll do is we'll print the name salary and all these things for Harry we'll do the same thing for Rohan and we'll do the same thing for kishan so we'll say kishen I'll copy I'll paste I'll paste the salary of the employee of Kion is so I'll have to replace this with kishen I know that's a lot of typing but bear 9:19:04 with me I'll quickly do this and also I'll replace all these values with with rans okay so I've used multic cursor functionality of vs code which simply you know can be done by pressing ALT key and clicking wherever you want the cursor so you can press ALT key and you can keep clicking and you'll see the magic of Visual Studio code so let me run this program so it says enter details for Harry enter the name for Harry I want to enter Harry salary is 34 rupees and Hike is 4.2 now the program ended what went wrong let me check let me check what went wrong so enter the hike for Harry uh Harry dohke so I have made a major mistake here so what I'll do is I'll simply use M percent here I forgot to you know take the address of all these variables I'll do the same thing here and I'll not do this same thing for string because um sorry I don't want to put a am percent here I'll 9:20:07 simply put an AM percent here and here okay so let me also put the % for um kishan's salary and Harry's salary so let me do the same thing for Harry's salary and also for kishan's salary and Hike so I'll use am perent here and now this program should work so let me run this program U I accidentally selected the program so I'll say enter the name for Harry it's Harry let us say 45 hike can be 5.2 then again it asks the same thing for Rohan salary can be 34 for Rohan and the hike for Rohan is going to be zero I'm very sorry about that uh kishan and then salary 477 he's the richest guy in this company hike is 9.7 and you can see that all these values are printed on the screen so the name of are is are salary of are is 45 hike of are is 5.2 and all these values were stored in a very um compact way because we were using structure uh we could have 9:21:07 done the same thing by creating three different salary variables three different hike variables three different name character arrays but we do not want to do that because we want to create an organized pattern to store a data okay so this was a quick quiz now let's talk about why should we use structures why use structures now we can create the data types in the employee structur separately but when the number of properties in a structure increases it becomes difficult for us to create data variables without structures so what happens is let us say we have uh three employees in this case you might think that how about creating three different salary variables three different hike variables and three different name character it is saying name underscore Harry name underscore kishan name underscore Rohan and the same for hore for Harry kishan and Rohan but we didn't do that because in this case we had only three employees but you might encounter a situation wherein you will have say 70,000 employees 880,000 employees in that case it will not make sense for you to create 80,000 70,000 variables okay 9:22:10 this was one now the second reason why we are using structure is in this case there are only three data types but what if the number of data types are say 20 or 40 uh now you might be wondering why would there be 40 different types inside a structure but let me tell you something when the complexity of a program increases you will definitely encounter programs with this level of complexity so um I've written it here in a nutshell structures keep the data organized so structures help you keep the data organized structure make data management easy for the programmer just for the reasons I just told you okay now array of structures so we'll talk about array of structures just like we create integer arrays we can create array of structures so what we can do is we can say struct employee Facebook 100 and this structure can hold 100 employees okay just like int a00 can hold 100 integers so this Facebook variable is an 9:23:12 array of structures now this might sound really very um confusing to you if you are a beginner but bear with me let me write a program for you so that this this will become you know really easy for you so what I'll do is I'll say array of structs do c I'll put in the boiler plate code let me copy this structure because I'm not going to type this again and again I'll reuse it now what I'll do is I'll say struct employee struct I misses spelled struct and I don't want to use a snipet I want to say struct employee and I can say something like this I can say Facebook and I can say 10 now this array is actually capable of storing 10 different items uh or rather 10 different employees from Facebook company okay now it can be anything uh I just wanted to you know uh present the data in a very meaningful way so that you can understand that we are doing this for employees of Facebook 9:24:13 but this can be anything this can be a book this can be uh say uh an electronic equipment wherein you'll have the number of wires the number of circuit connections so it can be any complicated thing but the basics of C programming language and structures are going to be the same okay so what we can do now is we can access the data like this we can say Facebook 0. code Facebook 1. code in this case what I'll do is just like we have created our um our struct employee har what I'll do is I'll simply say uh Facebook z. name I can do something like this so what I'll do is I'll replace all these hares with Facebook zeros okay let me also copy this and let me paste this I'll save it now what I'll do is I will do the same thing that I did in the first program I'll copy these print of statements and before I return 9:25:14 I'm going to paste all these uh print of statement to print these values so what I'll do is I will change all these Harry variables to Facebook zero so I can now run this program and show you that Facebook zero was able to store name employee uh sorry name salary and Hike of this employee okay so this is how it works and we can use Facebook one Facebook 2 and facebook0 one and two will uh store different structures okay let me also create a program which will tell you you how we can refactor the program from Quick quiz let me call it quick quiz uh all. C what I want to what I want to do is I want to tell you that the program that we wrote can also be created by say creating an array of three different structures in the form 9:26:16 of an array so this particular thing will look like one particular structure but it is not not a single structure just like int a00 is not one integer it's 100 integers this represents three different employees okay so now what I can do is I can replace Harry with Facebook zero so let me do that so I'll say facebook0 so whatever I've written Harry dot I'll replace it with Facebook zero so let me do that for you so wherever I've written Harry dot I will replace it with Facebook zero so let me do replace all but before that I want to make sure that I'm replacing the correct things why am I not replacing Harry and Harry Dot and I'll replace it with Facebook dot uh I am replacing Harry dot because if I replace Harry these things will also get replaced I don't want that so I'm replacing Harry dot with facebook0 DOT replace all I'll do the same for Kish I'll replace it for U with Facebook one and then I'll do the same 9:27:16 thing for Rohan I'll say Facebook 2 and this should work okay but there is some error it seems no there is no error in this program let me run this program for you and I want to show you that this will work okay so let me quickly uh type few values and I will do the same thing for Rohan we'll remember that the Rohan salary is 20 rupees okay and hike for Rohan is 2.1 and for Kish name is Kish four and one okay so you can see that all these values are printed just like they were printed in program from or quick quiz okay so this is how you can use arrays uh arrays of structures okay now just like an array of integers an array of loads and an array of characters we can create array of structures just like I showed you and struct employee Facebook 100 will create an array of 100 employees and what is an employee employee is the structure so 100 such 9:28:18 entities can be created just using this simple declaration struct employee Facebook 100 okay now we can access the data using this and I have written this in the notes so that you can always come back to the notes and you can access all this syntax also what I'll do is I'll zip all these code files for you and I'll uh give it to you so that you can run any program of your choice anytime you want so we now move into the next topic and we'll talk about initializing structures so how do we initialize our structure so structures can be initialized as follows I've written a Syntax for you we have already made the program uh and I have already opened this particular folder in vs code I guess yes so what I'll do is I'll show you how to initialize structures using a program and it will make it easier for you to understand structure structure inate Doc and in this program I'm going to tell you how to initialize a structure okay so what I'll do is I'll 9:29:20 simply come to this particular page and I'll simply say struct employee har is equal to this and what this will do is this will initialize the structure I can directly initialize the structure I'll show you how so what I can do is I can you know copy this program and I'll paste it here and what I'll do now is I will simply get rid of all this all the code which has been written by me in the main but what I'll do is I'll simply say stru employee Harry is equal to I'll say salary is 34 and I'll say 5.2 is the hike and the name is Harry and I'll use a double codes okay now what I'll do is I'll save this particular program and if I run this program for you you'll see that we are getting no errors whatsoever but does this program actually initialize all these values is the question and I'm going to answer this question for you so here is the set of print of statements which were written by me in the previous program to print name salary and Hike of 9:30:22 Harry let's see if these values are still working with this particular synex so I'll run this program and you can see that all these uh you know names salary and Hike variables have been initialized properly which means that the syntax works you can also set all the elements to zero so all the elements will be set to zero if you use the syntax so in case you have a set of numbers or you want to initialize a couple of pointers to null what you can do is you can always use this synex and this will initialize all the elements to zero so this was another way to initialize structures we have already seen one of the ways to initialize structure here where we set name salary and Hike manually U using scanf and you know St strcpy now how does structure look in memory is something that you need to understand because if you understand how programs are running in memory you'll be able to easily decode what is happening in the program and it will make you a better programmer for sure so let's see 9:31:24 how structures look in memory structures are stored in contigous memory locations for the structure even of typ struct employee memory layout looks something like this so if the address of this particular block is 78 uh 78810 what will happen is this will take four bytes and the address for the next float will start from 78 814 and again it will take 4 bytes the starting address of this particular block which is St storing the string uh storing the name String rather is 78819 so this particular block will have 7888 as the starting address this will have this as a starting address and this integer will have this as a starting address so structures store the data in contiguous memory locations so this is something that you need to keep in your mind in an array of structures these employee instances are stored adjacent to each other so let us say we have a an array of 12 St structures or an array of three structures for that matter what will happen is three structures will be 9:32:28 stored in further continuous memory blocks so what will happen is let us say this is my memory let us say this particular row is my memory what will happen is the address will start from here for the first element of the structure so after the ending address of the string will be the starting address of this particular uh what is the salary of the next structure so I hope you got the point so in our continuous memory we'll have the first element of the structure here and the very next thing in the memory will be the second element of the structure array then third element of the structure array and so on so in an array of structures these employee instances are stored Adent to each other I hope you got my point so let's talk about pointers to structures a pointer to structure can be created as follows so you can say struct employee star PTR and PTR is equal to m% E so this index can be used to create a pointer of type struct employee remember I told you just like inte a data type 9:33:28 struct employe is also a data type and I can use this struct employee data type to create a pointer of this type so PTR is a pointer of type struct employee PDR is not a pointer of type in or type float or type care for that matter it's a pointer of type struct employee which is a data type a custom data type which is defined by me just like we have int float double all these data types available this is something that we made available for us by writing this structure I hope you got the point now we can print structure elements using print F person D and just like I was using this address of operator this estx operator I can use it with the pointer to structure as well I can say tell me which structure variable is this pointer pointing to and I can decode this address and get the structure and once I get the structure variable I can do do code do name do salary whatever is 9:34:28 available in the structure can be you know used by me in this case I don't have anything such code but I have okay I have this code I'm sorry I have not written the code in the program but in this example I have the code in my example I have salary hike and name so I can use do salary doik or do name to get all these things but in this example since we have code here so I'm using dot code so this is how you can you know create pointers to structures and then further dreference them to get the value at those pointers using the star operator now there is something called an arrow operator in C which will make your life a whole lot easier if you are using pointers to structures instead of writing star PTR do code what we can do is we can use Arrow operators to access structure properties as follows but before that let's write a program using pointers to structures okay so what I'll do is I'll simply create another program 06 uncore pointer to 9:35:30 structure do c and what I'll do is I'll copy this code inside this particular file and what I'll do is I'll simply create a pointer of type struct employee and what I'll do is I'll set this to the address of Harry Harry is what Harry is a structure of type employee so it's a struct employee variable so struct employee variable Harry now has a pointer PTR which is pointing to this structure so how do I make a pointer point to a variable I simply set it to the address of that variable and PTR will now point to Harry now instead of doing harry. name I can also do something like this let me show you so instead of writing Harry what I can do is I can write um I can write star and I can do uh sorry PTR do name so I can do something like this I can say star PTR do name star PTR do salary and star PTR 9:36:32 doik what this will do is this will dreference the pointer and finally after d referencing the pointer what I'll get is I'll get this particular structure so this is something which is very important I'll have to wrap it inside the brackets because this will not work otherwise uh so what we want to do is we want to get the structure first and then we want to do dot name on that structure so what I'll do is I'll replace all this with uh um these blackets so let me copy this paste here paste here and all these uh red underlines which were indicating the error have now vanished now if I run this program you can see the salary of har is 34 and this also prints the salary of are is 34 name is also printed same and the hike is also printed same the difference being this one is using the pointers and this one is using the direct structure so I can either use a pointer and D reference it or I can directly use the variable just like integers and floats we have already seen all these things okay now what we'll do 9:37:34 is we'll create another program and our next program is 07 uncore Arrow operator and arrow operator is really very important if you want to go into data structures and you know work with structures you'll definitely use all these things so what I've done here is I've copied the code from pointed to structure program this particular program and I've pasted it here and now I'll tell you how Arrow syntax works so Aros synex is really very easy all you have to do is instead of doing star PTR do name instead of dereferencing a pointer and then getting the value from that structure what you can do is you can directly use the pointer and use the arrow operator which will simply mean that what you want is you want to dreference this pointer and get name from from it so this syntax simply means that you want to dreference this pointer get the structure and then get the name from that structure okay so this syntax is really very simple so I'll use multiple cursors here to create 9:38:36 a arrow and I'll simply say PTR salary PTR all these things and this syntax will work let me show you if I run this program you'll see that the name and the salary and the hike have been printed this is the output of this particular thing where I've used directly the variable the structure variable and this is the output from using the pointers I've D referenced the pointer and then I've used a DOT operator dot operator can give me the name salary and Hike from the structure now this is a special syntax that we learned which is the arrow operator so here the C programming language or the C compilers is smart enough to understand that I want to dreference this pointer and get the name from it because I'm using this Aros syntax so this Aros syntax actually makes it really very convenient for users to uh you know dreference the pointer and get a value if you use a lot of pointers in your program uh it will be a big headache to you know dreference 9:39:36 those pointers and use dot operators this is a clean syntax and is easy to write easy to read as well as easy to maintain so this was the arrow operator instead of writing star pd. code we can use this Arrow operator to access structure proper properties as follows I can do star PTR do code or I can do something like this okay now here I think I have not written the bracket correctly this bracket should come outside so you need to make a note of this this is the correct syntax I accidentally wrote The Star outside the bracket this will create problems for you so I want you to remove this bracket and put it here so make sure that you do this this is a minor mistake in the notes but again Arrow operator is amazing this is this is why we use Arrow operators we don't want to get into all these things where to put the bracket where to put the star where to put the dot simply use the pointer and say Arrow code and this will give you the code from this structure okay so here this is known as the arrow operator you can use this operator to dreference a pointer and get get the value from the D 9:40:37 referenced pointer or the structure okay passing structure to a function can you pass a structure to a function and if yes how how does it look like so a structure can be passed to a function just like any other data type you can pass structure and this is the function prototype so there is a quick quiz I have for you complete this show function to display the content of employee so you need to complete this function and you need to tell me how to write this particular function so what we'll do is we'll simply write this program because this is a very important concept so what I'll do is I'll write a program 08 Qui quiz so I'll say 08 uncore quore quiz. C and inside this particular function what I'll do is um rather inside this program what I'll do is I will copy this structure first and what I'll do is I'll copy this show function so I'll say void show struct employeee so let me write 9:41:37 this function void show struct struct employeee now what will happen is if I pass a structure to this particular function that structure will be available inside this function but the question is will that structure be deeply copied or will that structure be passed as a reference so this is something that we'll try to understand in this program so what I'll do is I'll simply the name of employee is um I'll do something like this so I'll say the name of employee is and here also I'll write employee and instead of saying PTR I will say harry. name or e.name so I'll simply say right click change all occurrences I'll make it E do so I'll say e do name e do salary e dohke okay let me save this program and if I create a structure uh is there a program where 9:42:38 I've created a structure yes here is a program where I've created a structure I'm simply creating a hariry structure here now what I'll do is I'll simply pass this structure to show function and let's see if it is able to show all these variables and indeed it is able to show we know that but what if I change a particular variable let us say what if I change the hike what if I change the hike of e I'll say e do hike is equal to 45 or 4.55 what if I change the hike to this will this hike change be reflected here as well this is the question also what if I change the name will that change be reflected here so all these questions are important so this hike change will not be reflected here let me show you so what I'll do is instead of doing this I'll simply write harry. name Harry do salary and Harry do hike here and if I run this program you'll see that initially the hike was 5.2 inside the 9:43:40 function I tried to change it to 4.55 but it will not be reflected here because all these values are copied what if I try to change e do name what will happen happen there if I try to change e do name let us say I want to change the first character of e do name and let me try to make it P if I try to do something like this inside my function I'll get Parry but after this function returns this change will no longer be reflected in my main function which means that this function is making or receiving the copy of har structure it is not receiving it by reference I I can make it you know go by reference and in that case all these changes will be reflected let me create another show function and what I'll do is this time I'll name it show reference show ref and I'll receive a structure pointer and not a structure and I'll change all these things to Arrow operator because we are dealing with structure pointers so I'll 9:44:41 dreference this pointer and I'll get name salary and Hike from it and also here what I'll do is I'll simply say star e.name 0 is Al to P so I can do something like this and here as well I can do something like this okay so star e dohke is equal to this star e. name0 is equal to P all these things can be written like this okay now if I run this program you'll see that here the change is not reflected because I'm calling this show function what if I call show ref and I pass the address address of this structure in this case what will happen is if I try to run this program this change is actually reflected here also the hike change will be reflected so initially the hike change was 5.22 but after I changed it to 4.55 it will be reflected here so all these things are something you need to understand this is basic C logic I would say this is 9:45:41 something that you need to understand by now and I hope that all of you understood how these things are working by now now what we'll do is we'll talk about a a keyword which is typ Def keyword and this keyword is a syntactic sugar but really comes very handy now you you all must be thinking that when we have integer data type float data type character data type all these are single word data types I can use int a car b or I can say float C but when we are dealing with structures we have to write the struct we have to write struct complex but there is a way where instead of writing struct employee you can replace all your struct employees with a single word which is employee how how can we do that in this case you can use type def keyword we can use the type def keyword to create an alas name for data types in C typ def is more commonly used with structures type def is used with structure it simply renames the type it it will rename struct complex to complex 9:46:43 number so I can say type def struct complex complex number and in this case what I can do is I can say type def let let let me create another program for typ def so I'll say 09 typ def. C and I'll use a very simple program which is here to demonstrate what typ Dev is and how it works so I can say type Dev struct employee I can do all these things and instead of saying employee I can say EMP I can call this EMP now so instead of saying struct employee I can say EMP EMP here as well and no prices for guessing this will work this works so you'll see that we can use EMP to create a pointer as well as to create the structure we no longer need to type struct employee every time we want to create a structure of type employee okay so this comes really very handy when you are dealing with complex programs and uh you know you are frustrated writing Str employee strued employee again and again 9:47:44 you no longer need to type Stu employee again and again okay so this is something that you need to understand so in this case what done is I've created a diagram for you I've written the program for you so struct complex C1 C2 for defining complex numbers this is a structure for defining complex numbers now here we we are using type def keyword so instead of using struct complex C1 C2 I can write something like this complex number C1 C2 for defining the complex numbers so I hope that you understood structures well enough and I hope that these notes will help you even further so we'll now move on to chapter chter 9 practice set where we'll see all the questions pertaining to structures let's move on to chapter 9 practice set now so let's now talk about chapter 9 practice set let's look into the questions in this practice set so the question number one says create a two-dimensional Vector using structures in C so we just have to create a two dimensional Vector using structures in C so what I'll do is I'll simply create a program and let me minimize this one I 9:48:46 know I have made a lot of programs so what I'll do is I'll simply create a new file say 10or PR 01c and I'll put a boiler plate code here so now what I'll do is I'll simply write a structure I'll use the snippet and I'll say twwo dim vector and I'll say a stuct two dim vector and what I'll do is I'll say int X int Y and that's it this is a twood dimensional Vector now what I'll do is I'll simply create to dim Vector V1 and V2 now I can do something like this I can say V1 is equal to 1A 4 and I can say V is equal to 11 comma 54 let us say and also I can write a program to print these two vectors so just like I have these two integers in X and Y I can simply assign V1 and V2 to 11 54 and 14 9:49:48 now in order to initialize it I will have to write this in a single line and also the same thing for v2 so what I'll do is I'll simply cut this and I'll paste it here now I can save it and run this program and it will work fine but how do I know that it is going to work fine I can simply print I can do something like this print F uh percentage d i plus percentage DJ and what I'll do is I'll say V1 do X and V1 doy I okay and this should print V1 so if I run this program you can say 1 I + 4 J so this is printing a one dimensional two dimensional array if you don't know what a two dimensional array is simply uh consider two dimensional array to be an entity which contains two different integers one is the x- axis one is the y axis so let's move on to the next question now the next question says that write a function some Vector which Returns the sum of two vectors passed to it the vectors must be two dimensional so we have created a two 9:50:50 dimension array now we want to create a function which can add such two dimensional arrays if you don't know how to add two two dimensional vectors what we can do is we can add their X components and we can add their y components and the resulting Vector will be the sum of these two vectors so I'll say pr02 Doc also I'll copy this I'll copy this program and I'll paste it here also I'll close other programs I'll say close others I'll save this program so V1 and V2 are two vectors I'll do something I'll say V3 is equal to uh so we have to create a function sum I'll say uh struct to dim V3 is equal to sum of V2 and V V1 and vs2 now sum is a function which will take two vectors which are two dimensional vectors and it will return a structure so I'll say struct 9:51:51 two dim Vector sum and this is a function sum is a function and this function is going to do take two two dimensional vectors let us call them X and Y so this will take two two dimensional vectors so what I'll do is I'll create a third twood dimensional Vector here I'll say result and result will contain A and B now what is this A and B A and B are two integers int a a isal to x dox + y dox and B is equal x doy + y doy okay so what is this Vector to make it a bit less confusing what I'll do is I'll call this first and I'll call this one second and instead of writing X dox and Y doy I'll simply write first and second so I'll write first a and I'll write second a so a is the sum of X components from 9:52:53 first and the second vectors and B is the sum of Y components of first and second vectors so a is equal to this thing Y is equal to this B is equal to this thing and the resulting Vector is this and I will return result okay now let's see if I'm able to print this Vector just like I was able to print this here so instead of printing this for V1 I'll print it for V3 and I'll see if V3 is the sum of V1 and V2 so I should get 12 and 58 am I getting 12 and 58 yes I'm getting 12 and 58 which means that I'm able to sum these two vectors the sum of these two vectors is going to be 11 and 1 individually summed and 54 and four individually summed which is going to return us 12 I + 58 J okay I hope that makes sense let's now move on to the next question 20 integers are to be stored in memory what will you prefer 9:53:54 will you prefer an array of structure this is an important question and you need to think about it and tell me the answer in the comment sections so if you have 20 integers waiting to be stored in the memory what are you going to use are you going to create a structure with 20 different integers or you're going to create an array definitely you are going to create an array because we use structures when we have a collection of dissimilar data types and we use arrays when we have a collection of similar data types 20 integers will be all of type integers and they are a collection of similar data types which is why we will prefer arrays okay the write a program to illustrate the use of Arrow operator in C we have talked enough about it I want you to attempt this program on your own let's move on to question number five write a program with a structure representing a complex number and a complex number will have a real part and a complex part okay just like a two dimensional array we can use real part and complex part to store a comp complex number so let's solve this problem which problem is this this is problem number five so what I'll do is 9:54:56 I'll create a program 12core pr05 doc 05. C I'll put in a boilerplate code and what I'll do is I'll copy this structure and instead of making it in this time I'll make it float so I'll say complex I'll make it float real R for real and Float c c for complex so it will have a real part and a complex part and the rest of the things are going to be the same just like our 10th program so it's going to be the same except the fact that instead of taking integers we'll use floats so we'll say percentage F here and instead of saying R uh sorry instead of saying X we'll say R and C real and complex and instead of saying two dim Vector I'm going to use complex which is a name of our structure and this program should work instead of making it four I'm going to make it0 4 let us say 5.4 1.1 and 9:55:59 let's see what it returns so 1.1 I + 0.4 J which is true I'm able to print V1 in a very similar way I can print V2 as well so I can print V2 something like this okay so if I print V2 it is going to look something like this I should use back sln to better format it and you can see that I have this 1.1 I + 0.4 J 1.1 I + 5.4 JJ so I hope that you are able to understand how we solve this problem now let's move on to problem number six problem number six says create an array of five complex numbers created in problem five and display them with the help of a display function the values must be taken as an input from the user so you know how scanf works we have already seen uh how to add two numbers you can always create an array we have already seen a program on array of structure so I want you to use those learnings and solve this problem and in a very similar way you can refactor the program to use the typ def keyword we 9:57:00 have talked about type def keyword and how it works okay question number eight says that create a structure representing a bank account of a customer What fields did you use and why so in order to create a structure for storing bank account of a customer so I'll say PR it's problem number eight so I'll say PR 08 do c I'll put a boiler plate code here and I'll create a structure bank account so I'll say struct bank and what will I put inside the struct bank I'll say int account number also I'll create a name I'll say a name field for 20 characters I'll say float balance and also I'll say um let us add few more Fields what what else can I add so these are the three things that comes to my mind uh I can also say branch code I can say car 9:58:00 branch code and let's let's give it 15 characters and also you can add some other fields um this answer need not be unique you can come up with your Innovative bank which will have uh some of the other fields for the customers okay so this is what came to my mind so I've added these fields so question number eight is done let's solve question number nine now write a structure capable of storing date write a function to compare those dates so this is something that I want you to solve because we have already talked a lot about structures right a structure capable of storing dates what all Fields do you want to store inside a date maybe you want to store month maybe you want to store year also you might want to store a day okay so day month and year and when you're comparing these dates you can one by one compare these three things firstly you will compare the year then you'll compare the month if the year is same then you'll compare the dates so you can easily tell which of 9:59:01 the dates are occurring before uh let us say the other date so you can compare two dates and you can tell that which of these dates will appear first in the calendar okay so I want you to solve this problem now I'm aware of the fact that I've skipped most of the problems here and the reason being I want you to practice now because we have already completed nine chapters and we have learned a lot about C programming language so problem number 10 is very easy you have to refactor the program in problem number nine to type def keyword and I know you can do that so with this question we have finished chapter 9 practice set now what we are going to do is we are going to move into chapter number 10 and learn more about the C Concepts let's move on to chapter 10 now so now we'll move on to chapter number 10 and we'll start discussing file.io so the random access memory is volatile and its content is lost once the program terminates so if you are aware of how programs work in a particular computer or in any particular device what happens is the program is first loaded into the memory and that memory is nothing but 10:00:03 Ram which is a random access memory and all the content of that random access memory will get lost once the program terminates in order to Pur data forever VI use files so in order to persist the data in order to make the data available later even after the computer is turned off you need to store it in files inside your hard drives you cannot store data in your RAM and uh you know expect it to be available all the time in order to do that you'll have to store your data inside hard disk or some storage device and we store our data in the form of files a file is a data stored in a storage device a c program can talk to the file by reading content from it so a c program can talk to the file by reading content from it and writing content to it so so uh here is a visual diagram I hope you like this programmer C program can write to a file and it can read from a file so a c program can actually write data to a file inside a storage drive it can be a hard drive it 10:01:04 can be a USB drive it can be anything your program can talk to this file and perform read and write operations so let's now discuss what a file pointer is a file is a structure which needs to be created for opening the file okay so this is a pointer which needs to be created for opening the file the file pointer a file pointer is a pointer to the structure of the file so a file is a structure and file pointer is a pointer to the structure okay file pointer is needed for communication between the file and the program so if you need to talk to this file you'll have to create a file pointer a file pointer can be created as follows all you need to do is you need to type file St PTR and all you need to do is you need to use this F open function which is available let's create a program which can read a file so I'll say uh read file. C I'll put in the boilerplate code I'll also you know write a sample file.txt and I'll say this is a sample file I'll save this file so what I've done is inside my hard 10:02:06 disk at this location uh wherever this folder is open at this location what I've done is I've created this sample file.txt and I've generated some content inside it now what I'm doing is I'm trying to create a a file pointer so I need to keep it in caps I'm saying file star PTR now I'll say PTR is equal to F open the first argument is the name of the file which is sample file.txt and the second argument is the mode okay so all all I need to do is I need to give the mode here so this is the mode and mode is a string so I'll use double codes I'll give read mode now let me show you that this program works I'll save this program and I'll also have to enable C++ extension and also the code Runner so I'll enable these two things I'll also enable code Runner so what I'll do is I'll enable it and what I'll do is I'll simply you know start U with 10:03:08 this PTR is equal to fop sample file.txt now let me show you that this program actually works so I'll simply run this program and you can see that this program is not throwing any errors whatso ever which means that this particular program works now we'll see what are the file opening modes in see we have already seen read mode I just showed you read mode we can uh open our file in read mode now let's see what are the other modes available if you want to um if you want to open a file in C so C offers the programmers to select a mode for opening a file so you can either open in read mode either you can open in read binary mode you can also open in write mode you can also open in write binary mode also you can open in upend mode so let's see what are the modes available following modes are primarily used in C file IO so R is open for reading RB is open for reading in binary W is open for writing WB is open for writing in binary a is open for a pen now let me tell you something about read 10:04:09 if the file does not does not exist F open returns null so if the file does not exist you will get a null pointer and uh you know what a null pointer is null pointer means it is pointing to nothing so null is a constant in C and if if the pointer is null it means that it is pointing to nothing okay so you can actually check if PDR is equal to equal to null you can perform these kind of checks to find whether a pointer is null or not now let's talk about these open for writing and open for writing in binary if the file exists the content will be overwritten so this is something that you need to be very careful with if you are writing to a file and that file actually exists that file will be overridden so you should be very careful if you are dealing with files that you really care about let us say I write a program and I write a program to use write operation on Sample file.txt I might lose this content if I overwrite this file so this is something that I need to keep in my mind now let's talk about append append simply means U append characters at the end of the file 10:05:11 so let us say I'm appending three to this particular file I'll simply add three at the end this is how append works okay so append means add adding content to the end of the file if the file does not exist it will be created this is something that you need to keep in mind as well because if the file does not exist and you have open a file in aend mode the file will actually be created okay so I hope that this makes sense let's talk about types of files primarily there are two types of files text files and binary files text files are the ones which can be opened using notepad and the content of those files can be easily read by human now binary file is a bit tricky some examples of binary files are jpg datat now let's talk about reading a file how do we read a file a file can be opened for reading as follows you can create a pointer you can say PTR is equal to this thing and you can actually use f scanf which is a counterpart of scanf you can use it to 10:06:12 read a character or you can use it to read a um let us say a floating Point number for that that matter or an integer for that matter let's read one character from this sample file.txt so in order to read a character what I'll do is I'll create a character and I'll use f scanner function so what I'll do is I'll give this PDR now what I'll do is uh the first argument is the PTR the second argument is the format specifier and third argument is the address of the character so I'll say am person CH now what is the format specified it's percentage C because I'm dealing with characters now what I'll do is if I print this character CH I'll say the character CH is percentage C and if I try to print CH you'll see that first character has been read from the file so the first character was t let us make it h and let's see if the program is able to read H from this particular file yes this program is able to read one 10:07:13 character from this file I can actually Loop through this and read all the characters but we are going to talk talk about that later and now what I'll do is at this point I just want you to make sure that you understand how F scanf works and let us assume that harry. txt contains an integer you could have used int num and you could have read an integer as well but in this case my file contained a character so I read this character now I can also read an integer from here so let us say I have an integer 88 and I'm adding a space I can do something like this I can say in I and and instead of doing all this instead of doing all this what I'll do is I'll do inti and then I'll say F scanf PTR and instead of doing percentage C I'll do percentage D and M percent I and I'll say the value of I is I'll say the value of I is what is the value of I percentage d i so I'll say percentage D 10:08:16 and I'll say I and this will give me 88 so you can see that I can read an integer from a file as well using this syntax now I have made these notes very carefully so you can always download these notes and revise all the concepts that we are discussing in this chapter now this will read an integer from the file in Num variable in this case it will read it in the I variable okay now coming back to the noes quick quiz modify the program above to check whether the file exists or not before opening the file and how how how would you do that you will simply check whether the pointer is null or not because if the file does not exist F open returns null so you can always uh you know put a check whether uh this thing is null or not if uh your pointer is null this means that the file does not exist let's talk about closing the file it's very important to close the file after read or write this is achieved using App close as follows so whenever you are reading a file and 10:09:17 you're pointing a pointer to a file after you are done reading or writing from the file all you need to do is you need to run this app close because you are actually holding that file resource and it is not a good idea to hold a file resource into a pointer forever you need to free it as soon as possible and we use a close PTR to tell the operating system that I am done with this file I no longer need this file this file is no longer opened in this program and this will actually make the file available for other prr programs this will tell the compiler that we are done working with the file and the associated resource could now be freed okay now let's talk about writing to a file we can write to a file in a very similar manner like we read the file you can do something like file star fptr you can write to a file and in order to write to a file you can use f printf instead of F scanf and make sure that you use f close in this case also I'll use f close so 10:10:18 I'll say F CL those and then I'll give the PTR and this program will also work fine but let's try to write to a file so I'll say 02 uncore write file. C this program will actually write to a file so what I'll do is I'll create a new file and what I'll do is I'll open it in write mode so I'll say write file okay right file.txt is the name of my file and I'll say in I is equal to 89 and I'll say F print F and I'll say PTR and I'll simply add I to this to this file and let me get rid of this let me run this program and you can see that right file.txt has been created and 89 is now part of this file which means that we were able to successfully write to this file using fprintf so this is how you write to a file now let's let 10:11:19 talk about two more functions which are FG C and fput C these two functions are very similar to F scanf and F printf fgc and fput C are used to read and write a character from or to a file uh now if you do FG C PTR this can be used to read a character from file and if you do fput c a character in PTR used to write character C to the file let's see the demo of f get C and FP C so this is program number three and I'll say 0 3core FG C put C.C I'll put the boiler plate code also what I'll do is I'll simply paste all the content of uh this particular file to um to this file I'll open fgc FG cput c. dxt I'll open it in right mode and I'll demonstrate fput C so I'll 10:12:20 say fput C and I'll try to add this character to this file so I'll do something like this I'll get rid of this in I is equal to this thing I'll also put a semicolon here and save it so if I run this program you'll see that fput c. txt has been created and this character has been written to this file okay you can do the same thing for FG c as well so now if I want to read this file I can actually do something like this let me get rid of of this and let me replicate it instead of opening it in right mode I'll this time open it in read mode and I'll say F get cptr F get cptr and I'll do something like this and I can print this character I can say print F the value of CHS percentage C CH and this will if I if I run this the value of C is let me spell value 10:13:24 correctly the value of CH is C now what if I make this character y y DD L km this will show me why this will read one character now if I do this thing once again if I if I do something like this CH is equal to FG cpdr and I run this again what will happen is two characters will be R so the value of CH is y then again y if I keep on repeating this what will happen is it it will keep on reading further characters from the file so y first one was Y the second one was y third one was D as you can see in this file now it's always a good idea to put a back sln for better readability um if I run this program now it is a bit readable a bit more readable than the previous one so this is how FG C and fput C Works let's talk about end of file character now and gets the return CF when all the characters from a file have been read so we can write a 10:14:24 check like below to detect end of the file so what happens is when you are reading using FG C you'll keep on getting the character from the file until EO f is encountered and when eof is encountered it means that there is no further content in the file and you can stop your program there so what we can do is we can say V one and we can keep on reading from this file and we can put a check if CH is equal to eof then break okay so so I I can write a program like this so let me let me read fourth program 04 read all characters C and let me put some underscores here for better readability and let me copy this program so what I was doing here was I was repeatedly saying CH is equal to F get cpdr CH is to F get cbdr instead of doing this repeatedly what I'll do is um let me do something like this car CH and 10:15:27 so what I'll do is I'll create a loop for this one so instead of repeatedly doing this I'll create a while loop I'll say V while CH is not equal to eoff so while end of file character is not in counter do this and that's about it the moment end of file character is encountered what will happen is um I will stop reading from the file so let me run this program for you and you can see all the uh you know characters from the file have been read so if I show you this file the last character is M and this one is eoff character okay now this is printing this extra character because this extra character is eof so if you want to get rid of this eof all you all you need to do is you need to print only when the character is not eof so I can put a check here if this CH is not equal to eoff all you need to do is you need to print this okay so I I no longer need c 10:16:28 braces because this is just one line so this is something that you can remember if I run this program you can see it's printing starting from um starting from y all the way to M okay so all the characters from this file so this is how you can read all the characters from a given file in C language so when all the content of a file has been read break the loop so this is another implementation of the same thing you can either use this implementation or you can go for this one both of the things will get the job done for you so uh with this we will conclude file IO in C I hope that these notes will be helpful for you also let's move on to chapter 10 practice set to solve some more problems on file IO let's move on to chapter 10 practice set now so now that we have learned so much about file iio let's solve this practice set and let's see what this practice set has to offer for the question number one of this practice set says write a program to read three integers from a file so we have a file we have three integers and we have to read those three integers from the file 10:17:28 so what I'll do is I'll open my visual studio code and I'll create a new file I'll say 05 p01 since this is the first question of her practice set I'll create this file as well as I'll put in the boiler plate code I'll also use some of the other files which I've created here to read the integers so I can use f scanf percentage D and in order to use f scanf I'll make use of this read file program so I'll simply create a file pointer to read a string from a file so let's say q1 is the name of the file let me create this q1 txt so that we can have three integers inside this so let me say 12 24 and 36 so we'll remember what integers are there now 12 24 36 let me close this file and what I'll do is I'll keep on reading an integer using F scanner from this file how do we do that we have already seen that we can use f scanf like this and I can store this 10:18:31 value inside I so first what I'll do is I'll declare an integer variable which is I and then what I'll do is I'll simply print the first integer using this print F I can do this on repeat and let me show you that this works so if I run this program you can see that the value of I is 12 then 24 then 36 so I can keep on reading from this particular file let me write a back sln just so that we can get a new line character and her output is shown in a formatted way now let's move on to the next question which says write a program to generate multiplication table of a given number in text format now this is something that I want you to attempt because this is very simple you can attempt it using F printf simply use few F printf calls and you should be good to go so please go ahead and attempt this question all you need to do is uh you need to do the same thing that we have done in initial chapters but using F printf instead of 10:19:32 printf so I hope you can do this so let's move on to question number three now it says write a program to read a text file character by character and write its content twice in a separate file we have already seen this we have already seen how to read a text file character by character what you have to do is you have to print that particular thing twice so you can read twice or you can simply store it in a character array and you can write it it's your choice totally you can do it however you want all the methods will work let's move on to the next question which is question number four take name and salary of two employees as input from the user and write them to a text file in the following format so let me solve this problem for you so what I'll do is I'll simply create a new file I'll say uh 0 6core p4. C this is problem number four if I'm not wrong yes it's problem number four so what you have to do is you have to read name and salary of two employees so how do we do that I can simply create uh 10:20:34 character array and what I'll do is I'll use in to take the salary from the user so I'll use print if I'll say enter the value of salary and yes I'll put a back SL to format the output correctly now I'll use scanf I'll say percentage D so percentage D and M person salary this will give me the value of salary and I'll do the same thing for uh for my name so I'll use percentage s this time and I'll use M perent name uh so let me do it name because name is the um address of this the first element of this character array enter the value of name and I'll get name inside name and salary inside salary so if I want to print the name and salary I can do something like this I can say salary name and you'll see that name and salary 10:21:36 will get printed in the output so if I say 45 and I say Harry so 45 and Harry are printed on the screen which confirms that this particular variable now has salary and this particular variable has Harry or the the name entered by the user stored inside of it so what I have to do now is I have to create a file pointer and I have to use this I have to write to a file so what I'll do is I'll copy this program and I'll use this file pointer I'll declare the pointer at the top and um I'll get rid of this integer what I'll do is I'll say F printf and let me also call it q6 do dxt so that you can figure out which question this file is referring to I'll say salary and and and let me see in what fashion this uh particular thing demands to get printed so uh we have to do it for two employees first of all so name one comma salary so let me format it in this way so I'll say 10:22:38 percentage s and then I'll use a comma and then salary so I'll say name and salary let me see if this works for one employee and name let me run this program I'll say 34 Harry and let's see if 34 and Harry are written to this file yes Harry 34 are return returned to this file in this manner but we have to do the same thing for two employees not for one employee so instead of calling it name I'll call it name one and also I'll call it name two and salary 2 for the second employee and I'll say enter the value of salary 1 then salary two and we are going to do the same thing for uh sorry name one and then we going to do the same thing for uh name two and salary 2 so let me call this two two two once again to here to here as well and instead of printing the salary I'll directly write it to the file so I'll say name one salary one and also a back sln because I need to get a 10:23:39 new line character as well I'll say name two salary 2 let me save this program and let me run this program once I run this program it is asking me for the salary so if I enter this value and I say Harry and then once again the salary for employee 2 is this and name of the employee is Rohan and if I look at this file you'll see that Harry this Rohan this has been printed if you want to get rid of this new line you can remove this new line character from here you'll not get an additional new line at the end but this is how you can write to a file in a specified format okay in this case we wrote all the user input to q6 .txt in this format okay question number five says that write a program to modify a file containing an integer to double its value so in order to do this what you have to do is you have to read the integer from that file and then you'll have to write to the same file with integer multip by two let me show you how this is going to work let's write seventh program which is problem number 10:24:41 5. txt sorry not txt we are going to create a txt file later but for now I I want to create a C file and also in this case we have to read the file as well as write the file so these two programs are very handy sometimes you might forget the syntax so you can use these programs all you have to do is open the file in deed mode first so I'll open the file in deed mode and let me call this file Q Q5 txt just so that you know that this is a file from question number 5 and I'll read an integer from this so I'll say int I and in order to read an integer from this file I'll use this F scanf and I'll use the synex so whatever integers Q5 txt now contains has been St stored inside of I so I now contains the integer which was written inside Q5 txt now I have to open Q5 txt in right mode 10:25:41 and what I'll do is I will simply write this particular integer to this file so I'll say F printf instead of scanf I'll write F printf PTR percentage D and I but not I I'll I'll write I multiply by two to this file and also I I'll have close PTR I'll save this file let me create a file Q Q5 txt Q5 txt let's write 45 to this let's run this program if I run this program and if I check Q5 txt 90 is now there so if I run this program once again 180 should be present in the file if I run this program one again uh 360 720 then 1440 uh 1440 and so on okay so this will print double the value inside the file uh it will actually read the file first 10:26:41 and then it will write double the value to the same file so I think we have to create a new file in this case but you can always create a new file you can always replace this Q5 with Q5 uncore new and a new file will be created so I hope that you understood how files work with c and how you can read and write files in C programming language in the next chapter we are going to cover a very important topic which is dynamic memory allocation and this topic is going to help you a lot when it comes to data structures and algorithms so make sure that you understand this topic really very well we'll now move on to chapter number 11 and we'll look into dynamic memory allocation let's move on to chapter 11 now now this is going to be the last chapter from this C programming course I have opened this PDF and the name of the topic is dynamic memory allocation really a very important topic if you are going to or if you are planning to go into Data section and algorithms and uh for sure you'll sit for interviews and you are going to prepare for data talk algorithms uh in the near future so 10:27:41 let's prepare for that let's talk about dynamic memory allocation so C is a language with some fixed rule of programming for example changing the size of an array is not allowed there are lot of other rules you cannot use single codes for writing strings and so on so we have some fixed rules changing the size of an array is not allowed is one of the rules now why why why have I mentioned this changing the size of an array is not allowed as one of the fixed rules because at times you might want to have a variable sized array but what happens is memory allocation in a program happens before the runtime so so what happens is before the runtime your program reserves some space in the memory and your program operates on that memory but at time what happens is we need dynamic memory now let me explain you what dynamic memory is dynamic memory is the memory that you don't know that you will need but you might need while the program is executing for 10:28:42 example you might ask the user for the size of an array now after user tells you the size of an array you want to create an array of that size for the user now user might enter five user might enter 10 user might enter 25 you don't know so you will have to dynamically allocate the memory dynamic memory allocation means that memory allocation will happen during runtime so let's read about dynamic memory allocation from the notes now dynamic memory location is a way to allocate memory to a data structure during the run time we can use dynamic memory allocation functions available in C to allocate and free memory during runtime so what happens is if you are using traditional arrays or integers or character for that matter what happens is memory is allocated in stack now what is a stack whenever a function runs a stack for that function is maintained and all the variables are stored in that stack now you cannot dynamically change 10:29:43 the array which is stored inside the stack but what we can do is we can request some memory from the Heap which is another area in the memory and we can dynamically allocate memory from the Heap so what we tell our C compiler is hey C compiler I know we are between the program we are in mid of the program execution but please give me an array of size 20 25 30 a variable size it can be anything the moment you request C compiler for that memory C compiler will go to theat Heap and allocate the memory in the Heap and give you the pointer to that memory now this might s sound very strange to you but let me show this to you we'll write some programs and um we'll learn about some functions that C programming language offers to do the damic memory allocation in C now following functions are available in C to perform dynamic memory allocation the very first function which is available is melck then we have kellock then we 10:30:44 have free then we have real looc now if I tell you in very short about all these functions let me tell you maloc is for memory allocation Cog is for contiguous memory allocation we'll see how it is different from Moc U free is used to free the memory in the Heap because if you are uh requesting a memory from the Heap there is some memory which will be already in use because you have a pointer to that memory in the Heap now you want to free that memory in order to tell compiler that I no longer need this memory you are free to provide this memory for other tasks now realloc is used to reallocate memory in the Heap using a pointer now let me show you something what I'll do is I will show you memory layout of Inc I want to show you how this thing is working because if if you don't understand how this thing is working it'll be really very difficult for you to understand dynamic memory allocation so let me open an image here you can see that this is uh 10:31:44 your program in memory so this is your RAM now this is high address this is low address uh this is a nice diagram let me open this diagram in a new tab now what I'm going to do is I am going to explain how our program works so what happens is this is stack uh which starts filling from the top this is Heap which starts filling from the bottom and uh we have a text area where the code is stored uh in the memory for our program then there is initialized data uninitialized data now the stack will hold all the variables from different function functions in the program and Heap will contain all the memory which is dynamically allocated how will you allocate memory dynamically from Heap what you do is you allocate the memory in a heap and you request a pointer to that memory and you can now use it in your function from um you can store that pointer in stack and use memory which is available in Heap so I hope that you understood how stack and Heap are working initially we were dealing only with stack but now we 10:32:45 realized that we might need some memory for some time during our execution of the program we can request that memory from the Heap dynamically it can be any amount of memory as long as it is available your operating system is ready to give it to you your program will get it okay so we'll come back to the notes and we'll start with the Malo function Malo stands for memory allocation it takes number of bytes to be allocated as an input and returns a pointer of type void so what happens is you can use Malo and then you can use the amount of bytes you want and once you give it this input you'll get a pointer where will this memory be located inside your uh RAM it will be located in Heap make sure that it will be available in Heap you need to understand this concept uh so that you understand how dynamic memory is working okay so let's try to use dynamic memory in our C programs what I'll do is I'll close this one I'll open this particular folder in VSS code I know I'll have to 10:33:46 enable CC C++ extension because by default I have turned it off uh I'll enable it okay it's enabled for now which is good and we have code Runner as well let me see if it is enabled as well it is enabled as well which is good uh now what I'll do is I'll create my first program on Malo I'll say Malo Doc and I'll put in the boilerplate code and I'll show you how you can use Malo to allocate memory from Heap now you'll have to use this header file which is m.ed make sure you include this header file now what I'll do is I'll simply say PTR is equal to inst star we are typ casting the pointer what this in star in the brackets does is it will convert the type whichever is supplied here I'm going to use Moc function Moc will return a pointer of type void let me use 30 multiply by size of it 10:34:47 and I'll show you what this thing is doing now when I say in Star this is basically converting whatever is coming from this to an integer pointer type okay and MOG 30 multip by size of int will return the amount of bytes consumed by 30 integers now size of is a function which will take a data type and tell you how many bytes that data type is going to take now if you want to store 30 integers you'll have to multiply the amount of bytes taken by by one integer with 30 and you will get the amount of bytes taken by 30 integers maloc will take the amount of bytes and it will supply you a pointer of type void which can be typ casted using in Star if you use in Star MOG 30 into size of in you will get a pointer to the address in Heap which is holding your 30 integers in contigous memory locations if that makes sense so I can now simply say PTR 0 is equal to I can use it as an array I can say PDS 0 10:35:50 is equal to 45 and I can keep on storing values inside this array I can say 46 then 47 I am free to store anything I want ptr2 PTR 3 and I can go all the way till 9 sorry 29 because we have 30 integers now if I do print F percentage D and if I say PTR three or let me do PTR 2 I'll uh I'll use a semicolon you can see for 47 is printed because ptr2 contains 47 so you can use it as an array okay but the um new thing about this particular type of array is that it is not inside stack it is inside Heap it is requested dynamically so I can even say int n is equal to 5 and I can say n into size of int okay I can do something like this and I can create dynamic memory of size five integers inside my Heap now this n can even come from a 10:36:52 scanf so if I do scanf percentage D and if I do something like this m percent N I can even Supply 6 7 8 whatever I want from my keyboard and I can then use variable here you cannot do this with arrays you cannot do int I or uh let me show you what I mean by in I Y and you cannot do something like this this this is not valid okay you can see it's throwing uh throwing eror expression must have a constant value and is not a constant so it is saying that it's a variable you cannot use variable okay so you want to create an array dynamically this is how you do it okay so I have also written this inside the notes that we are casting void pointer to in this is space for 30 inss this is size taken by one int so if you want to create an array of characters you can use size of car now why do we use size of function why can't we simply hard code four here you can hardcode four here but it's not a good 10:37:53 idea to hardcode a value here because in different architectures integer might take different uh bytes so you don't want to hard Code 4 2 or8 for that matter now the expression returns a null pointer if the memory cannot be allocated so if you request a memory size which your computer fails to allocate you are going to get a null pointer so if PTR is null it means that your memory was not able to U Get allocated by the compiler so here is a quick quiz right a program to create a dynamic array of five floats using myoc so let's solve this quick quiz so what I'll do is I'll say 02 uncore quore quiz. C I'll use the boilerplate code and uh what I'll do is let me let me write uh write program to create a dynamic area of size uh five floats so let me use let me use the same program and I'm going to refactor 10:38:55 this what I will do is I will I will simply say 5 n is equal to 5 and I'll say myog 5 into size of float and I'll use float star Moc because uh this is PTR will be a pointer to float this time float and let me not use n let me simply say five here and I can use floating Point numbers here and since I'm using floating Point numbers I'll use percentage F so I can store five floats so I'll say 1 4.18 let me run this program for you 4.7 because ptr2 contains 4.7 so this is how you can use my lock to create a dynamic array of five floats okay let's not talk about Cog function what is Kellog kog stands for continuous allocation it initializes each memory block with a default value of zero so in case of Moc default 10:39:56 allocation was not done in this case default value of zero will be initialized to each memory block so you can simply use kellock in this way you can say PTR is equal to float star kellock 30 and comma size of float so this allocates continous space in memory for 30 blocks or for 30 floats okay if the space Cas is not sufficient memory location fails and a null pointer is returned just like in the case of Moc so if null pointer is returned it means that your memory allocation failed now another quick quiz says write a program to create an array of size n using kog where n is an integer entered by the user so n is an integer which is which is going to be entered by the user you'll use scanf and you can simply use this syntax to use kellock so kellock is really very simple let me show this to you let me write this program for you I'll say 03 uncore kog Doc and I'm going to use the same program that I used for malog 10:40:57 except the fact that this time I'm going to use scog so I'll say print F enter the size of your array and I'll simply say ERS um I'll say nothing because I've already used this um scanf in the new line so what will happen is this this is an integer pointer this is uh an integer now print F enter the size of your array will be shown on the screen then user will enter the size and then what I'll do is I'll use kellock and kellock takes two arguments first is the uh the number of integers Floats or whatever you want and then the size of one block so it will allocate these many continuous blocks of this size so I want n blocks of size of in which is four in this architecture okay so I can use this to create an array let me come back to the question so size n using KOC where n is an integer so I can enter as large n as 10:42:00 possible and this will do the job for me so if I save this program and run this program uh and if I say four 47 is printed so I can allocate an array of size 4 by using this synex as well but by default what will happen is all the blocks will be initialized to zero so if I don't do this I will get zero as the output so if I say four Zer is the output in case of Melo you might or might not get zero as the output but in kellock you will always get zero as the output because all the blocks are initialized with the initial value zero so I have already written it here uh where is it written it's written here okay now let's talk about free function we can use free function to deallocate the memory so if a pointer is holding some memory you can always is use free and that pointer to free that memory in the Heap the memory allocated using kellock Moc is not deallocated automatically so the moment you allocate memory and use it and your program is still running kellock or meloc will keep 10:43:02 on holding that memory in Heap now your compiler might do some sort of optimizations from its end but we are talking about C program here and kellock maloc will not deallocate the memory automatically so in that case you'll have to use free function which will release the memory which PTR was holding so if you were holding say 5 in those memory blocks will be freed so here's a quick quiz write a program to demonstrate the usage of free with bck so you are free to write the program it's really very simple simply use the free function and it will deallocate the memory okay now let me let me show you this uh one thing quickly if I allocate the memory uh let me come back to this quick quiz let us say I use this free PTR here so here itself I freed all the memory if I run this program you'll see that nothing is printed so this thing is not printing anything because I've already freed the memory which I was holding okay so this was one weird usage 10:44:03 of free you might not want to use free in this way but again you can always use free to release the memory which was held by uh PTR okay now the last function is realloc and it is also really very important because sometimes the dynamically allocated memory is insufficient or more than required so you might want to reallocate uh using the reoc function reoc is used to allocate memory of new size using the previous pointer and size so let us say you were having PTR um and it was holding say four integers and now you want PTR and you want 30 integers you can always use real looc and real looc will give you new memory of your desired size okay so PTR is equal to reoc PTR 3 into size of int will give me a new pointer which is capable of holding three integers so PD now points to this new block of memory capable of storing three integers so this is how real looc works I've written all the syntax in the nodes I hope that uh you like this dynamic memory allocation concept now 10:45:05 you might want to dive deeper into dynamic memory allocation by writing programs on your own so practice as much as possible as hard as possible and you'll definitely get this concept sometimes it's very very hard to grasp for beginners the concept of Stack concept of Heap and how a memory layout in of a program in C looks like so I want all of you to search on the internet about the memory layout of C programs how memory layout of C program looks like and then from there you can definitely understand how dynamic memory Works in order to add to our understanding we will solve practice set from chapter 11 let's now move on to chapter 11 practice set and solve some problems so I'll open this chapter 11 practice set for you and inside this chapter 11 we are going to solve few questions so let me open this folder in vs code quickly so I'll open this folder in code and also what I'll do is I'll create program for the very first question because I'm going to solve it no matter what okay so we are going to 10:46:05 solve this problem I don't even know what the problem is but I'm going to solve it no matter what it is so the problem says write a program to dynamically create an array of size six capable of storing six integers I think I've already done this program I'll simply use this particular syntax yeah I am a big fan of copy paste by the way and you should be as well it saves a lot of time do not copy from others do not copy with negative intentions but again if your intent is right and the code is yours and the code you coping from is yours you are free to you are in fact more than free to copy the code so I'll simply write six here I'll uh simply create this particular program and run it and this program is capable of storing uh six integers I'll include my log. H and this time if I run it it's not going to run with any errors I'm not going to get any errors and this is the official solution to this problem okay question number two what does it say it says use the ARR in problem number one to store six integers entered by the 10:47:07 user let's do it let's do it uh so what I'll do is I'll simply write 05 uncore 05 uncore p 0 2.c and let me close this PDF file I don't know why I opened it at the first place I'll copy this particular code and I'll operate on this this question says that use the array to store six integers so how how will I do that I will simply say PTR 0 is equal to say 1 and then um I'll simply use 1 2 3 4 5 6 7 8 so I'll say 1 2 3 4 by the way I can go only till five because I have to store six integers and let me give some random numbers here and I'll give it8 this time now let me simply print all these numbers the value at index percentage D is percentage D okay so let's say PTR Z and 10:48:09 this is going to be at index Zer okay so 0 PD 0 and I'll do the same for 1 2 three four and so on so let me do it for one let me do it for two then for three then for four then for five this is how you quickly edit uh anything you want in vs code okay oh my God I forgot to put these back SLS and the output is horrible so let me put a back sln for all the printf statements and you can see that all the integers I stored are now visible so at index Zer we have 1 then 4 11 5 88 21 these are the same numbers which we initially assigned or ARR with okay so I hope that you understood how this thing works question number three says solve problem one using kok and yes you can use Kellog so kok is very similar to Melo and you you need to you need to solve it yourself okay this is very simple you just have to change the Melo called to kellock and just look at the syntax of kellock from the uh chapter 11 PDF it's pretty 10:49:12 straightforward question number four says that create an array that dynamically capable of storing five integers now use realog so that it can store 10 integers we are going to solve this problem this problem is interesting this is question number four so I'll simply say p4. C and let me start from this program itself so initially this program uh was capable of storing six integers but my question says that create one which is capable of storing five integers let me remove the last element from the AR so this is a program which is capable of storing um with an array with a dynamic array capable of storing five integers now I need to change it to be um you know able to hold 10 integers so how will I do that I'll simply say PTR is equal to I'll use reoc and I'll simply say reoc and then I'll say PTR and new size what is a new size new size is 10 multip by size of end now 10:50:14 this array is capable of storing 10 integers so if I run this program you can see that all these integers are visible but now I can write PTR 5 as well now I'll say PTR 5 is equal to and I'll simply punch in some number and I can print PTR 5 here and if I run this code you'll see that index 5 um is 5432 so I'm able to store one more integer and I can store four more yeah this is an array of size 10 so I can store four more okay coming back to the practice set the next question says that create an array of multiplication table of 7 up to 10 so you need to start from one and you need to create a multiplication table of seven from 7 multip by 1 all the way to 7 multip by 10 and then you need to use real loock to make it store 15 numbers so this is a very simple problem of using an array a dynamic array of size 10 and then changing it size to 15 we have already done that so I want you to show some of your 10:51:15 creativity and uh start writing this code this is very simple and then once again attempt problem four using Kellog very simple I just need to change this call to kellock and uh when I say kellock I'll simply give the number of bytes sorry the number of blocks and size per block okay so this will give me continuous memory allocation uh I've already told you all this in chapter 11 so in case you think that all these things are something that you're not able to remember all the synex is present uh inside chapter 11 notes so feel free to use chapter 11 notes so this ends this c tutorial I hope you enjoyed this video if you did share it with your friends as well so that they can enjoy this video as well and I hope that uh this will add some value to your lives uh to your programming career make sure you take the copy of these notes I'll give all the links in the description all the programs all the source codes anything that we have done here is completely free available free 10:52:15 in the description you can get the link and download it it is absolutely free no condition supply also if you think I should create a new course just let me know I'll be happy to create that course for you I have already uploaded a python course for you so in case you haven't watched my python in one video make sure you watch it so I will simply open my channel and I'll show you where that video is so it says Python tutorial for beginners with noes and this is 11 hours 38 minutes video I hope that you will enjoy this one as well if you enjoy this one also I'm planning for an HTML tutorial with notes so watch out for that as well uh thank you so much guys for watching this video and I will see you next time [Music]