 Hello everyone, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we are having day 20 of C and C++ coding bootcamp and today's topic is structure. So in this session I will be explaining you the brief of structure and will be implementing couple of programs. So moving on to the next slide. So if you have joined this bootcamp for the first time and if you want to know about me, so this slide will let you know everything about me. So moving forward, be consistent if you are learning this C and C++ programming languages because these are very much important. So consistency is the key and do regular practice. So there is an exercise sheet available in the session tracker of this bootcamp and links are available in the description of the video. So you can have it from there. Okay, so do regular practice so that you can understand the concepts. Now moving forward, so this is a self help group that you can join and learn coding and ask questions as well. And follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram, Telegram and if you follow this, so you will be receiving timely notification and all the important links are available in the video's description. Okay, so let's start with structuring C programming. So first of all, we need to understand the concept what structure is and how we can implement it. So we already discussed about array. So if you talk about array, so arrays basically collection of similar type of elements, right? Now if you talk about structure, so structure is basically a user defined data type. And if you want to store different type of elements, then we basically create structure, right? So in a single variable, if you want to store different type of values, for that purpose, we basically create structure. And if you talk about object oriented programming languages, so if we modify structure and other than variables, other than data, if we add some methods as well, so that is basically treated as class. So in object oriented programming, we use class as a data type. So it is just extension of the structure, right? So in C programming, we have structure only in C plus plus Java apex, we have the concept of class. So here, in third statement, it is written like we can create variables as well as area of structure data type, right? So as you already know, structure, if you're creating, so it will be considered as a data type, right? And through that data type, we can create a single variable or we can create its area as well. Okay, so moving on to the next slide. So next, you can see the declaration. So if you want to create a structure in C programming, so here you can see we have a syntax or you can say the declaration. So here we have a keyword which is known as struct, right? Struct is a keyword that we need to use. And after struct, we have a space and then we use structure name, right? So struct keyword will be always same and structure name, you can have anything. And and inside this curly basis, you will be having all the data type and variables, right? So we can have any number of data type. And as per the requirement, you can create variables. So we have variable one of one data type variable two of maybe another data type, then variable and of any other data type, right? So this way we created, then moving forward, you have an example. So struct is a keyword, student is a name of the structure. Then we have three variables, roll number, name and marks. And you can see all the variables are having different data type. So this is just a structure declaration. Before using structure, you need to declare it. Now after declaration of the structure, we will be creating its variables. Okay. So that variable will be able to store roll number, name and marks. And here you can see all are of different data type, right? So this is the way like how we can create structure variable. So we'll be using struct keyword, the name of the structure and then the variable name. Okay. So in previous slide, like this structure we created with struct keyword and structure name as student. So this whole thing we need to use here, struct, then structure name, and then variable, right? So the struct student will be treated as data type and this S will be created as a variable. Okay. So I hope now you are able to read it clearly. So here structs, student is a data type and S is variable of structure. So this way it is basically designed. So then we have memory locations. So first we saw like how to declare the structure, then we saw how to create variable for that structure. Now we are seeing the variable which we create for that structure, how its memory is allocated. So we have three members, roll number, name and marks. So roll number will be of two bytes name will be of 10 bytes and marks will be of four bytes. And this whole memory location will be identified as S, right? So through S, you will be able to access roll number through as you will be able to access name through as you will be able to access marks, right? So this way, like through single variable, we will be able to access different type of values. So this is the main advantage of using structure. Now if we move forward, so this is the way like how we can access the members of structure. So in previous slide, you can see we created a variable named as S. Now how you can access. So you need to write the structure variable and then the structure member. So structure variable is S and its member is roll number. So we need to write S dot roll number. So S will be the structure variable and after dot we have the member named as roll number, then S dot name, then S dot marks, right? So this way all the members are available and this is the common variable which we created for structure that is S, right? So this is the way we can access the members, okay? So moving forward now it's time to create some real examples practically. So I will be implementing this in front of you so that you understand how actually we can create programs for structure. So here we need to read the name, number of pages and price of a book and we need to display them, right? So I will be implementing this solution with the help of structure and structure you need to create before main. So here I'm creating structure with name book and here we need three members name, pages and price. So name I'm creating like care data type. So care name of 10. Then for number of pages we will be using integer pages, sorry int not integer. And for price I'm using float. So this way three variables I created name, pages and price data types you can see different data types are available. And here you need to use semicolon. So whenever you create any structure so at the end you will be using the semicolon, okay? Now in main we will be creating its variable. So I need to write struct book and then B. So here this struct book will be the data type and this B will be the variable of the structure. So this B will be having memory equals to name, page and price, okay? So B is capable enough to store the values. So here I'm writing printf enter book name then I'm using getters B dot name, right? So through B I will be fetching these numbers. So next time I'm writing printf then backslash and enter number of pages, right? Then scanf percent d m percent s dot pages, sorry B dot pages not s. So this line number 14 and 15 will be reading the number of pages. Now we need to read number we need to read price. So enter price and scanf percent f m percent d dot price, okay? So name then number of pages and then price and everything we are processing with the help of B variable which is related to structure. So after reading all these information now we need to display it on output. So name equals to percent s then B dot name then pages B dot pages then again printf price. So here we need to use percent f then B dot price, right? So this way you can see everywhere name pages and price. I'm referencing them through B variable. So struct book is a data type and variable B we created and through B we are processing name pages and price, okay? So this way I think you are able to understand how we are reading and displaying the values, okay? Now I'm going to execute this code. So I'm just clicking on run. So it is asking for book name. So I am entering sales force maybe SFDC because size is 10. So I just want to enter accordingly. Number of pages 1000 price is 500 and then you can see the results. So book name SFDC pages 1000 price 500. So the values which I entered are displayed here, okay? So you might be thinking like we can implement this program using name, page and price. Like we can declare these variables directly in the main function. Why to use structure? So that is absolutely right because if we are creating name, page and price above that doesn't make any sense. So this I demonstrated you so that you can understand how we can create structure, okay? The real advantage of using structure is like if you want to create more than one variables like you want to store information of more than one books. So you can create more than one variables of structure type or you can create its array. So in that case that structure will be beneficial. So this structure we need to declare once and any number of variables we can create like if you want to create one more book. So we just need to write B comma B1. Now through this B1 I can access B1.name, B1.pages, B1.price. So name, pages and price I don't need to write again and again. These are declared once in the structure and then through structure variable we can have any number of instances of that structure so that we can process name, pages and price, okay? So I hope this explanation helps. Now we have one more example, write a program to read name, roll number, marks of a student and display. So it is kind of same name, roll number and marks. So here like if I want to change this example, so here I am using struct student then int, roll number, care name will be same and float marks, right? So this way three different variables for student then here struct student then s. Now here I am going to enter and like student name. So here I need to write s.name then enter roll number. So this I am going to read and roll number variable then enter marks. So this I am going to read in marks variable, right? So this way modified the input statements. Now I need to modify the output statements. So here s.name then roll number. So s.roll number then we have marks. So here we need to write s.marks, okay? So this way we can modify this program, okay? So both the programs are in same nature like we are creating one one instance of the structure variable, right? So in today's session I will be explaining this much only. Now I am going to run this code so that you can understand it as well. So I am clicking on run. So I am entering student name as my name then roll number 101 and then marks 500. Now you will see the result. So this showing name, roll number and marks, right? So all the data which we entered is available in output in the format which we provided here as output, okay? So this way if you want to create a variable which can process different type of values. So for that purpose actually we create structure, right? So I hope now you will be able to implement solution for these two problems. So implement them and in tomorrow's session I will let you know how we can create arrays of these structure and then you will realize the real benefit of using structure. And if you want to learn object-oriented programming, so understanding of structure is very much important for that, okay? So thank you for joining this session and if you are watching the recording, so thanks to you as well, okay? And please share a review or feedback about the bootcamp which I am running. So total 20 sessions I have already conducted and I think in next 10 sessions C programming related stuff will be completed and then I will be explaining the C++ concepts which are related to object-oriented programming, okay? So this is it for today's session. Thank you. Bye everyone.