You are on page 1of 5
Name= Hemant: Kumar Regstraton number= USB24 Rall number=RD2IGADY question umber! ©). Gxgplain the structure of ¢ programming Language in detal with sample program Answer troduction te Structure of ¢ Program ALL huernen Benge have a definte structure key head, neck, and Four Limbs comected te a torso: Almest everything has a definite structure. Lkenisay the case of programming Languages oll of them have a definite structure. These structures have to be Followed whie writng the code. The structure of a ¢ program can be manly avided nts sic parts, each having its purposes it makes the pragram easy to reach easy to modfiy easy te coctimenty and makes it consistent in formate Basie Structure of c Fragran Qorcumentation= consists of the deserption of the pragram, pragrammers name, and creation ante. These are gererally written im the Focm of camments. QLink=AL header fles are neluded in this Section which contams afferent Functions From the libraries. A copy of these header Flas & inserted nto yur code before complation GQpcfnitionrtrcludes preprovesser directives which contane symbole constants Cay? foefine allows US to Use constants in aur code. it replaces all the constants with its value n the cade. Welobal Declarationclides declaration of global variables, function declarations, static gfobal. variables, and Functions. (S)ranO Fanctionfar everye pragrany the execistion starts Prom the -main() flanction eo eas acetate rh) (oon ts everfe pees B)suxprogramssnelides all usercefines Functions Functions the user provides). They can contain the insult (Functions, and the Function definitions declared in the clobal Declaration section These are called mn the mainG) Function Let's Look at an example to understand the structure of a ¢ program! Example: Write @ program te calculate aur ages by the Fallewing example, welll calculate age concerning a years Algorithm Youve to subtract the current year with your birth year, and you get your ages Let's implement the and check code! I Hi0ociumentation "Fle age * author? you * description: pragrams to Find cuir ages "" include [Link Haefine BORN 2000 I/DeFrition int ageGnt current); //qlobal Declaration int mainCvord) //MainQ) Function 2 int cuerrent = 202k ponthC Age: fal’, ageCoucrent)) return O 3 int agent current) { //subproarams return current - BORN 3 output Age: 21 Let's explore the coder Offerent sections of the above code. Doctamentation fac program, singledine comments can be written Using tive forward slashes Lex //, and we can create multiline comments using /* “/ Here, weve used multiline comments. re * blet agee * eucthort yes * description: program to Find our ages " Lake Al heaser Fles are incliided in this section A header file is a Fle that consists of c declarations that can be Used between afferent Fles. It helps us im wang others! code in aur Files. A copy of these header files ® nserted nto ypAr code before complation Frclucde Definition A preprocessor directive nc @ any statement that begns with the "#" symbol. The #aefine iS a preprocessor compler directive Used to create constants. Ih simple terms, faefine basically allews the macro sefriition which allows the we of constants in cur codes FfeteFine BORN 2.000 Weve created a constant BOtN which is assigned a valine of 2000. Ceneralliy uppercase letters are preferred for defining the constants. The above constant SRN will be replaced bby 2000 throughout our code wherever Used Haefine is tupeally used to make a Source program easy to modfy and comple in afferent execttion enviroments. define statement does not ends with a semicalon. Global. declaration This section includes all. global variables, fixnction declarations, and static variables. The variables declared in this section can be wsed anywhere in the program. Theyre accessible to all the Functions of the program. Hence, they are called dlobal variables. int ageGnt current), Welve declared aur age Function which takes one integer argument and retins an integer. Maing Function fh the structure of a ¢ program, this section contains the main flnction of the code. The compler starts exectition fram the marG) fiinction It can Use global variables, static variables, nbult Functions, and userdefined Functions. The retiscn type of the mainQ) Function can be veid alse not necessarily int. int mainbvord) ‘ int current = 202k print Cage: la’ ageCeucrent)), return 0 : 5 Suibpregrams This includes the user-defined Functions called in the main) Function. User-defined Functions are ggnerallyy written after the main) function respective of their orders When the user-defined Function is calles From the maint) Function the contral of the Program shfts to the called Runction anc when it encounters a ceturn Statement, it returns to the man) flunction ih this case, wee defined the age) flunction which takes one parameter nem the current year. int ageGnt current) { retum current - Boers 5 This Flanetion is called in the main Function It retums an integer to the main Functions Conclusion To coneluae, the structure of a ¢ program can be divided into six sections; namely Documentation Link, Definition Global Declaration, MainG) Flinction Subprograms: The mainQ) function is compulsory te include in every c program, whereas the rest are optionale A well-structured ¢ program makes debiigging easier and nereases the readablity and modularity of the code.

You might also like