TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
C PROGRAMMING p-2.pdf
1. BASICS OF “C” PROGRAMMING
By
R.Sivagami, Assistant Professor
Department of Computer Science and Applications
D.K.M College for women(Autonomous), Vellore-1.
2. TOPICS COVERED
Structure of C program
Compiling A C program
What goes inside the compilation process?
3. STRUCTURE OF C PROGRAM:
A C Program is divided into six sections
1) Documentation
2) Link
3) Definition
4 Global Declaration
5) Main()
6) Sub programs
4. DOCUMENTATION
* Consists of description of the program such as the name of the
program, creation date and time of the program.
* It is specified at the start of the program.
* Any thing written as comments will be treated as documentation
of the program and this will not interfere with the given code.
* In a C Program, Single-line comments can be written using two
forward slashes i.e., //
* Multi-line comments can be represented using /* */
Example
/* description, name of the program, programmer name, date, time
etc. */
5. LINK SECTION
All header files are included in this section
A header file is a file that consists of C declarations that can be
used between different files.
It helps us in using other’s code in our files.
A copy of these header files is inserted into the code before
compilation
Example
#include<stdio.h>
#include<math.h>
6. DEFINITION
* A Preprocessor directive in C is any statement that begins with the “#” symbol.
* The #define is a preprocessor compiler directive used to create constants.
* They basically allows the macro definition, which allows the use of constants in
our code
* Whenever this name is encountered by the compiler, it is replaced by the actual
piece of defined code.
* #define statements does not ends with a semicolon
Example
#define long long ll
7. GLOBAL DECLARATION
This section include all global variables, function
declarations, and static variables
Variables and functions which are declared in this scope can
be used anywhere in the program.
Example
int num = 18;
8. MAIN() FUNCTION
For every C Program, the execution starts from the main() function.
It is mandatory to include a main() function in every C Program
Operations like declaration and execution are performed inside the curly braces
of the main program.
The return type of the main() function can be int as well as void too.
Void() main tells the compiler that the program will not return any value.
The int main() tells the compiler that the program will return an integer value.
Example
void main()
Or
int main()
9. SUB PROGRAMS
Includes all user-defined functions(functions the user defined).
They can contain the in built functions and the function
definitions declared in the Global Declaration section.
The control of the program is shifted to the called function
whenever they are called from the main or outside the main()
function.
These are specified as per the requirements of the programmer.
Example
int sum(int x, int y)
{
return x+y;
}
10. //*****
*PROGRAM TO FIND SUM OF THE GIVEN NUMBERS.
*/ //Documentation
#include<stdio.h> //Link
#define X 20 // Definition
int sum(int y); // Global declaration
int main(void) // Main function
{
int y=55;
printf(“sum: %d”,sum(y));
return 0;
}
int sum(int y) //Sub program
{
return y+x;
}
O/P
Sum:75
11. STEPS INVOLVED IN THE COMPILATION AND
EXECUTION OF A C PROGRAM
Program Creation
Compilation of the program
Execution of the program
The output of the program
12. COMPILING A C PROGRAM:
The C program goes through the following phases during
compilation:
13. HOW DO WE COMPILE AND RUN A C PROGRAM?
Step 1: Creating a C Source File
Step 2: Compiling
Step 3: Executing the program
14. WHAT GOES INSIDE THE COMPILATION PROCESS?
A compiler converts a C program into an executable. There are
four phases for a C program to become an executable:
Pre-processing
Compilation
Assembly
Linking
16. 1. PRE-PROCESSING
This is the first phase through which source code is passed.
This phase includes:
Removal of Comments
Expansion of Macros
Expansion of the included files.
Conditional compilation
17. 2. COMPILING
The next step is to compile filename.i and produce an; intermediate compiled
output file filename.s. This file is in assembly-level instructions.
18. 3. ASSEMBLING
In this phase the filename.s is taken as input and turned
into filename.o by the assembler. This file contains machine-level
instructions.
19. 4. LINKING
This is the final phase in which all the linking of function calls with
their definitions is done.
Linker knows where all these functions are implemented. Linker does
some extra work also, it adds some extra code to our program which is
required when the program starts and ends.
For example, there is a code that is required for setting up the
environment like passing command line arguments.
This task can be easily verified by using $size filename.o and $size
filename. Through these commands, we know how the output file
increases from an object file to an executable file.
This is because of the extra code that Linker adds to our program.