Basic Structure of a C Program

Brief Summary

The video outlines the necessary structure of a C program, which includes specific sections essential for the program's functionality.

  • The program must have a documentation section, a header file section, a definition section, a global variable declaration section, a main function, and user-defined functions.
  • Each section's purpose is explained, from providing comments for clarity to linking libraries and defining variables.

Structure of a C Program

The video begins with an introduction to the fundamental structure of a software engineering (SE) program, specifically focusing on C language. It establishes that every C program must adhere to a defined structure consisting of various sections.

Documentation or Comment Section

The first section discussed is the documentation or comment section, which is essential for understanding the program's purpose. Comments explain statements in the code, providing clarity; for example, a statement like C = A + B can be elucidated with a comment indicating that it adds A and B and stores the result in C. Comments are non-executable and begin with /* and end with */, aiding programmers without affecting compilation.

Header File Section or Preprocessor Directives

The second section is the header file section, also referred to as the preprocessor directive section. Header files contain collections of library functions. For instance, #include <stdio.h> includes standard I/O functions. The preprocessor directives are processed before compilation to ensure that functions used in the code, such as printf() or scanf(), are recognized by the compiler. Proper linking is necessary to allow the main program to access these functions.

Definition Section

Next, the definition section is crucial for defining macros, which serve as symbolic constants. An example provided is defining PI as 3.14. This constant cannot be altered throughout the program, ensuring stability. The syntax used to define a macro is typically #define MACRONAME value, and these definitions help maintain consistent values across the program.

Global Variable Declaration Section

The video then addresses the global variable declaration section, which includes local and global variables. Local variables exist within a specific function, while global variables can be accessed throughout the entire program. Declaring a global variable above the main function allows it to be used across various functions, enhancing its accessibility.

Main Function Definition

Every C program must have at least one function, which is the main function. The execution of a C program starts from this function. The main function includes a declaration part for variables and an execution part that contains the operational logic of the program. This section highlights how statements are processed within the function, detailing input and output operations.

User Defined Functions

Finally, user-defined functions are discussed. These functions are created by the user and begin with a similar syntax to the main function but do not require a semicolon at the end of the function declaration. Users can define multiple functions based on their program's needs, allowing flexibility in coding for operations such as arithmetic calculations.

The video concludes by reinforcing the necessity of following this structure in any C program, summarising each of the functions and sections discussed.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the App Store
Get it on Google Play

Transcript

YouTube Transcript Generator

0:00 In this class we are going to discuss about structure of SE program, any SE program must follow this structure, so any SE program must contain these sections, the first section is documentation or comment section, the second section is header file section or preprocessor next our section is definition section fourth one global variable declaration section next one main function definition last one user defined function definition so any C program must follow this structure let's see the first section documentation or comment section

0:51 so here documentation section can also be called answer comment So here documentation section can also be called as comment section. Comments are mainly useful for understanding the program. Such as what is the meaning of each statement. Let the statement be like this. C is equal to A plus B. So what is the meaning of this statement? Add A and B and store the result in C. Add A and B P and store result in C store result in C so likewise we can write the comment so by using comments mainly we can understand the program so such as what is the meaning of each

1:38 statement so that statement purpose is provided in comments we can place comments anywhere in the program and any comment starts with far more and any comment starts with forward slash yastrik so yastrik is nothing but star symbol and ends with yastrik forward slash so this is nothing but forward slash not backward slash this is nothing but backward slash this symbol is nothing but forward slash so forward slash means division symbol so for comments we use forward slash so any comment begins with forward slash yastrik and ends with yastrik forward slash let us place name of the program let name of the program is yandrc here comments are not compiled and

2:26 not executed okay so if you place any statement any message in the comments then the corresponding statements will not be compiled and will not be executed so that is about comment section comment section can also be called as documentation section. be called yes So for what purpose we are using comments? We are using comments mainly for understanding the program. And the next one is header file section or pre -processor directive section or linking section. Any header file is a collection of library functions. Any header file is a collection of library functions so we have several library functions like of library functions. the first important library function is hash include

3:17 stdivo .h stdivo stands for standard input output and we have several library several header files like hash include so likewise we have several header files so any header file is a collection of library functions so library function means the corresponding functions are already defined in the header file stdlib .h contains library functions like printer function, scanner functions get cat, gex, put cat, puts so all these functions are defined in putcap ,puts so

4:02 all these functions are defined in stdio .h so if you want to make use of those functions then we must include stdio header file likewise string .h contains several library functions like strlen function strcpy function so likewise it contains several library functions so thats why it is called as header file so header means the corresponding statements The corresponding statement is placed as the beginning statement, so header is nothing the corresponding statement is placed but the beginning, so this is about what is a header file and the next one is preprocessor directive section, so header file section can also be called as preprocessor directive section, why because the name itself specifies the mean, pre means

4:51 first, processor means some processing, the statements which are processed. So processing, the statements which are processed before compilation are known as preprocessor directives. So preprocessor directives are processed, executed before compilation. Why because let us assume that in our program we use strln function. Our compiler doesn't know what is strln function because strln function definition is available in string .h. So strln In string .h, so sdr alien function definition should be processed before compilation. So then only our compiler understands sdr alien function code. So that's why these are also known as pre -processor directives. So pre -processor means those statements

5:41 are processed before compilation. So all these library functions are processed before the compilation. Processing is nothing but execution so that during compilation our compiler understands what is the meaning of those library files, those library functions because those functions are already executed or this section is also known as linking section why because here linking section means it links the object code of several library functions to the main program so let us assume that in our program we use this printer program so let us assume that in our program we use print of and scanner functions so our compiler doesn't understands what is print of and scanner functions so what will happen

6:26 now is print of and scanner functions definition is available in stdio .h so whenever the compilation is over then the compiler generates object file the extension for the object file is .obz so the compiler here what will happen is the linker links the object code of these library functions to the main program so we use a printup and scanner functions so these two functions definitions are available in stdivo .h so what the linker will do here any program is developed in various pages like coding page compilation page linking page loading page execution page during linking page During linking phase, the linker links object code of several library functions to the main program. If we

7:17 use a strln function and strcpy function, then these two functions definitions are available in string .h. So what the linker will do? The linker links the object code of these two library functions to the main program. So that our compiler understands this function code as well as this function code. So that's why header files section is also So that's why... known as preprocessor directive section or linking section. So header file means it is a collection of library functions. Any header file, most of the header files ends with the extension .h. .h. H stands for header file. And this is also called as preprocessor directive section. Why because

8:02 preprocessor means those statements are processed before compilation. processor means those statements are processed before compilation. So these library functions are processed before compilation. Then only compiler understands what is the purpose of those functions. And this section is also Nonia's linking section. Why? Because the linker links object goal of these library functions to the main program. So why these are Nonia's library functions? Why? Because these functions definitions are already defined in the corresponding header file and the thought section is definition section it is mainly useful in order to define macro so macro means some symbolic constants so in order to define macro we use this definition section the syntax for define the macro

8:52 is let's see an example has defined space generally macroname is given in uppercase letters it is our choice we can also use lowercase letters also but it is preferable to write macroname in uppercase letters what we know what is the value of the pi the value of the pi is 3 .14 so throughout the program the value of the pi is 3 .14 only if we try to change pi value then the compiler generates program the value of the pi is 3 .14 error message like if we want to change pi value like this pi is equal to 4 then the compiler

9:37 generates error message why because pi is defined as a macro here this macro is also known as symbolic constant symbolic constant symbolic constant means its value never changes during program execution so throughout the program the corresponding variable value is 3 .14 only if we try to change the corresponding variable value then the compiler produces error messages so this is about definition section so definition section is mainly useful in order to define symbolic constants or macro so here the syntax for symbolic has defined space macroname space constant example has defined space

10:24 pi in uppercase letters we can use any variable here has defined let name of the macro is a let the value is 10 here macroname is nothing but we have to follow the rules of identifier ok so here a is nothing but a valid identifier a valid variable space what is the value of the a 10 so throughout the program a value should be 10 only if we try to change a value then the compiler generates error message so this is about definition section and the next section is the global variable declaration section mainly there are two types of variables are available mainly local variable global variable and we have two more variables static variables and register variables so mainly we have local variables and global

11:12 variables local variable means those variables are used only inside a function in which they are declared so if you declare a variable inside a main function then we can use that variable only in main function only if you declare a variable in a function called fun1 then we can use the variable only in this function only so that is nothing but local variable so local variables are used inside a function in which they are declared whereas global variable means we can use the global variable throughout the program that the program contains 100 functions so if we want to make use of a variable in all the 100 functions then it is better to declare that variable

12:00 as a global variable so that is the advantage of a global variable so what is the advantage of a global variable we can use global variable throughout the program so where we have to declare global variable above main function so if we declare a variable above main function then the corresponding variable is known as global variable so the advantage of global variable is we can make use of global variable anywhere in the program and the next section is main function any C program is a collection of functions And any C program must contain at least one function that is main function. Without main an NEC program must contain at least function it is not possible to write a C program. So every C program must

12:45 contain at least one function that is main function. And the execution of any C program starts from main function. So initially the control will be the statement. Next to control will be transferred to the left with a case. Next declaration part. Next execution part. Likewise all the statements are executed so we can say that the execution of a C program must always starts from main function any main function mainly contains two parts the first part is declaration part and the second part is execution part so during declaration part we declared the variables so where we declared the variables after this left we can replace we have to declare the variables and the variables okay and the next one is execution part execution part

13:33 contains the statements next one is which performs the given task let us assume that we want to add two numbers we want to add two numbers so how many variables are needed here two variables are needed here so int a comma v so this is nothing but declaration part so during declaration part we have to declare the variables execution part means what is the task we have to perform here, we need to add two numbers. So what is the operation we have to perform? So printel %d, a plus b. So this is nothing but our program logic. So that logic is nothing but execution part. So during declaration part, we have to declare the variables which are used in execution part. is nothing but program logic.

14:19 It contains input statements, output statements and the logics. and the last section is user defined function the name itself specifies the meaning the functions which are defined by the user are known as user defined functions already we have seen the library functions so what is the library function the functions which are already defined in the corresponding header file printer function is already defined in stdio scanner function is already defined in stdio str aen function defined in stdio, str len function is already defined in string .h, str cpy function is already defined in string .h, ok, where as user defined function means these functions are defined by the user, so that means we have to define these functions, so here the syntax for defining an user

15:08 defined function is, here the syntax is similar to the main function, so what is the syntax for main function, so main, left to parenthesis, right parenthesis, here this is function definition so there is no need of any semicolon at the end of the main statement next within the curly braces left curly brace side curly brace we have to write declaration part and execution part even main function contains one statement the corresponding statement should be enclosed within curly braces okay likewise here also we are following the same syntax you shall define function so left curly brace, right curly brace Why because this is nothing but function definition never ends with semicolon Within the curly braces function body So what is usually defined function? These functions are defined by the

15:57 usual So within the curly braces we can place the function body So function body means the corresponding function logic We can define any number of usually defined functions According to our choice, according to the problem if you need if you want to perform arithmetic operations we have If you need, if you want to perform arithmetic five arithmetic operations like plus minus star division module so we need five functions so depends upon the problem we can define any number of user defined functions okay so this is nothing but the structure of a c program so any c program must follow this structure so what are the section what are the sections which we are using documentation section or comment section header section or pre -processed directive or linking section, definition section, global variable declaration section, main function and user defined function.

Share

YouTube Video Summaries