Transcript for:
Understanding the Structure of a C Program

in the series of learning programming in c in this video i am going to talk about structure of a c program what is basic structure of a c program how the program looks like how to write a c program we will also see this with a simple example we'll take a simple example and we'll write a program here in the previous videos i have already discussed some basics like what is c introduction to programming why do you write programs low level languages high level languages language translators features of c language now let us discuss the basic structure of a c program what sections are there in a c program how you can write a c program what you have to include and see i am going to discuss all the sections but some sections are optional and some sections are essential to include in your program it is not necessary that you have to include all the sections in your program when you are writing a program so very first section is your documentation section or you can say it is comment section here what you include comments means what information you include in this section like the author of that program who has developed that program date the date and time of development of that program and a brief description about that program like suppose let us take one example i want to write a program for addition of two numbers so what you can include in this documentation section this you can include author is jayanti khatri you can include date as well as time and you can also write down a brief description like program for addition of two numbers and now these are comments you have to include this line in comment section like you you can say if you write this one two forward slash this is single line comment in c language now what does this mean this line would be ignored by the compiler compiler will not execute this line right it compiler is not going to convert this line into object code completely compiler will ignore this line and suppose this line also it is a comment and if you want to comment multiple lines then there is also a multiple line comment here you can write forward slash as trick these two lines right and here is trig and this forward slash this is single line command this is multi this is multiple line commands right so whatever command you want to include you can include either a single line comment or multiple line command so now what is the purpose of including this thing if compiler ignore this thing compiler will not execute this thing it is just for understanding for future use like suppose if anyone wants to use your program in future and he wants to understand what this program is all about then rather than looking at code c addition of two number it is simple having maybe four or five lines simple as that you can look that program and you can you can easily figure out this program is about addition of two numbers but actual programs when they develop software the programs are having maybe hundreds of lines line of four thousand lines of code so by looking at that code we cannot easily understand what this program is all about but if you look at this documentation section which is above that program you can easily understand that who has developed this program the date and the time and what this program what this code is all about just by looking at documentation section right so this section is just for the understanding and a reading of the program to enhance the reading of the program to understand the program what the program is all about it is optional generally when you write program when we write program we do not include this documentation section right but it's better to include it should be in your practice but because in future when you will write those programs then obviously you have to include documentation section so it's better you start including this section from today only right when you write program but this is completely optional second section is link section here we include header files uh header files means like we include hash include stdio.hash is what it is preprocessor right now now stdio means the full form is standard input output don't touch extension why because it is header file now there are some predefined functions in c library built-in functions and we use those functions that i have already discussed when we're discussing the features of c language that c is having a many built-in functions many predefined function you can directly use those functions now this header file is for what standard input output means for which function for printf and scanf sprintf is what for output and scanf is what to take input right now see these are predefined functions and where these functions are defined in this library in this header file dot h so if you want to include this in your program then you have to include this header file why so because see compiler doesn't understand what does that mean printf we understand printf is what to give output to print something on screen but compiler is not having its own brain he'll not understand what printf means so you have to provide the meaning the code right and when compiler will look at that code then only compiler is able to understand that printf means he has to print something and where that definition of this function is where that code about this function is written in this header file in this library stdio dot h so see same suppose if you want to add two numbers then simply if you write sum compiler is not able to understand sum is what but if you write down the logic the code sum is equal to suppose a variable a plus b yeah then compiler will do sum of two variables a plus b numbers and will add will store that number into in some variable right same for printf also you have to define you have to tell the compiler what printf means so that meaning is in this header file studio dot h it's already defined you don't have to write down the code in your program for printf function you can directly use this function but you have to include the setter file to tell the compiler the meaning of this thing same as scanf the meaning of this thing is also in this header file if you include hash include corner dot h corneo means console input output the full form some sometimes they can ask in viva this is for what if you use a function gate ch it is basically to hold the screen output screen when when we will discuss a complete program then we will discuss what does that mean so the function or this function is already defined in this header file right so when this you will write this in your compi in your code compiler will not able to understand if you will not include this file but to tell the compiler the meaning of this gets yes then why why you are including this file you have to you want to hold the output screen so that meaning that code of this function is already written in this library right that is why you are including this new rotate now suppose if you want to use some math function like square root sqrt or power those functions are defined in math dotage at that time you will include here hash include math dot if you want to uh use some string functions like uh strlen you want to find out length of a string str cmp string compare string concatenation then compiler is not aware about this thing let string length means he has to find out length of the string no where this definition is written the code of this function is written the meaning of this function is written in the library string dot h so there you will include hash include string dot h then compiler will come to know yeah string length str alien means he has to he will have to find out the length of that string right that is why we include this header file and this is what link section because the linker is going to link this thing the code the c library code means the predefined function code into your program how the linking and loading is done that also will discuss when we will discuss the compilation process so this is what link section when you include the header files third section is what definition section this section defines all the symbolic constants now what does that mean suppose i want to write in a program i want to use pi you know the pi means 3.14 right so i'm using this pi in a program suppose 10 times right so rather than writing 3.14 10 times in a program what you can do you can write down here hash define by 3.14 so if you use this hash defined this is what macro definition so whenever in the program this is written pi suppose 10 times pi is written in a program so everywhere that will replace that will be replaced with this value right let us suppose one more more example i am writing here hash define suppose any max value or mean value max is 100 and max i'm using in a program in suppose 10 times or 15 times so 20 times so rather than writing 10 times 100 100 100 you can write down this thing one more advantage is what if you want to suppose change value of max suppose i want to change it from 100 to 200 so if you will not write this thing if in a program you have written that value 100 10 times then at 10 places you have to update that value 200 but if you use this thing then in a program rather than this value you can simply write down the name max so when you are going to update then you just have to update here only 100 to 200 that's it and that will automatically be replaced by 200 every time you write max in a program so that is you can say advantage of using this these macro or definitions or here you can also write down a small function definition like next comes to global declaration section see in a program we use two types of variables and functions one is local another is global what are those global suppose local variables they can be used within a within that function only where you define those variables like if i am writing a function ah here void sum and if suppose i am declaring a variable a here so a i can use within this function only this a variable i cannot use in main function or maybe another function like maybe any function is therefore subtraction there i cannot use this a but suppose here in a program we have five functions and in all the five functions we are using same variable that is a so now you can globally declare that variable if you globally declare that variable you can use that variable throughout the program in every function of a program in main also and in every user defined functions but if you will not globally declare then if you have five user defined functions like sum or subtraction multiplication division or any function for every function you have to declare this variable like int a two times and five times if five functions are there then you have to declare this variable five times because this is what what local declaration but if you will declare here outside these functions and outside of main function then you can use that like int a then you can use this variable in every function throughout the program in every function of the program right so the variables that are used in more than one functions in a program those are global variables and you can declare those variables globally outside of those functions as well as here you will declare what all the user defined functions will declare here suppose a function is to write to find out some of a program so here you can declare that some word sum and that's it again a function is suppose subtraction void subtraction and that's it this is what global declaration of a variable global declaration of functions now next is main main section this section is compulsory it is not optional this section is optional this section is also optional this you have to include this section is also optional but this is you have to include this is compulsory to include and in a program there can be only one main function this question can also be asked many times by how many main function can be there in a program only one main function always control will go when you compile that program and you run that program the control will go to main function first of all right so one main function can be there now how you will write main function like main i am writing here void main this is body of main function here you will write some statements now here also two parts are there one is declaration part and second one is executable part in declaration part you declare some variables if you want to declare like in a into b in some or any variable and in executable part it it is what the logic like if you want to find out sum then executable part means sum is equal to a plus b you are writing down the logic printf uh you can say enter value of a and b scan f then sum is equal to a plus b then you can print the sum print uh sum and this thing that will discuss the syntax that is executable part the logic and the declaration of variables that is what in declaration part so two parts are there in main function and this is compulsory to include right always control will go ah to main function the first line of main function when you compiling that program right so next is we have sub program section in this section you will include all the user defined functions like after main you can write down so here we can have many user defined functions 1 2 3 4 up to n and generally all the user defined functions comes immediately after main function but that is not compulsory they may appear in any order generally we write just after main function and you call these user defined functions into main function and these are also optional it's not like that you have to compulsory it's compulsory to include user defined function in your program no but yeah main function is main section to include main section that is compulsory and in main also two parts i have told you declaration and executable part so in declaration part we declare those variables that are used in executable parts right and it is completely optional to include declaration part in some program it is not we do not include the declaration part that also will discuss but there should be at least one statement in executable part at least one that is compulsory and both the declaration and executable parts that comes within curly braces word main then curly braces then two parts and then closing of curly braces right now let us see one example so generally your first program is to print hello world now how you will print include header files then i'll write down main function this is compulsory right and within main you have to print so you can write down printf just hello world and that's it right here to hold the output screen you can use get ch fine so see we haven't include documentation section in this suppose i am not writing this thing we have include link section only definition section is not there global declaration section is not there because we are not using these thing in this program this is a simple program in main also i have only executable part there is no declaration part that is fine and after that that's it sub program section there is no sub program section in this program right so it is not compulsive that you have to include all these sections but yeah this is basic structure every program should follow this structure if you are including every part like you have user defined functions also global declaration everything in a program this is small program that is why some sections are missing suppose you want to find out sum of two numbers or suppose you want to include all these sections so let us write down a program for that thing suppose this is a program so here and i have written now this also like program is just to print something it's not addition of two numbers so this is documentation section this is link section this is what definition section i have defined one macro that is mix and max value is handed although i am not using this here and there is no use of this thing but just to make you understand how you write a program when you include all the sections that's it i have used this thing this is what a is equal to 50 this declaration is what see in this data type a is name of the variable and this is value everything will discuss in detail later but this is just to make you understand the structure this is global declaration it is outside of all the functions one function is display function this is also what global global declaration section this is this is for a variable and this is for function right now comes to main function in main i have no declaration part only executable part printf hello this is syntax to write printf printf then these brackets and then your quotation mark and within that you can print something whatever you will write that would be displayed on the screen and here what you are doing i am calling this function this is what calling right calling of a function in calling you not need to specify the data type of any function just name of the function that is display right after this main we have sub program section i have only one user defined function that is display word display this is a definition of this user defined function this is calling of this user defined function and this is what declaration of this user defined function three uh things are there three things are associated with the function this also will discuss in detail so in definition we are just printing printf jenny's lectures so whenever this program is going to be executed control will go to main function so first of all what would be printed hello then you are calling display function then control will go to this function now within this function what is written printf jenny's lecture legend is lectures printf means compiler can understand like printf means he has to give some output because you have included this file and the code of this printf is here so compiler will understand compiler can easily understand printf means he has to print jenny's lectures then first of all hello would be printed and then jenny's lectures would be printed then control will go back to main function and after that you can write down what in main function you can write down get ch to hold that output screen right that's it that this would be about hello and jenny's lectures right so this is what structure of a c program in next video we are going to discuss compilation process how the program is to be converted into object code and then executable code right so now i'll see in the next video till then bye bye take care