RELIANCE DEGREE COLLEGE TALIKOTI
BCA 1st Semester
Course Title: Computer Concepts and C Programming(Notes by : Sajjad)
Unit - 2
Flowcharts and Algorithms Covered.
Introduction to C Programming: Features of C Covered.
Structure of a C Program
A C program is written in a structured format to make it easy to read, understand, and execute. The general
structure contains the following sections:
1. Documentation Section
Used for writing comments about the program (author name, purpose, date, etc.).
Not executed by the compiler.
The // symbol is used for single-line comments. Everything after // on the same line is ignored by the compiler.
The /* ... */ symbols are used for multi-line comments. Any text between /* and */ is ignored by the compiler, and
this comment can span multiple lines.
Eg1 : /* Program to calculate area of circle */
Eg2 : int g = 10; // Global variable
2. Link Section
The link section provides instructions for the compiler to include files which contain the predefined functions
which can be directly used in programs.
eg: To use scanf and printf functions it is necessary to include stdio.h header file as #include <stdio.h>.
3. Definition Section
Used to define symbolic constants or macros, using #define directive.
eg: #define PI 3.14
4. Global Declaration Section
Variables and functions declared here can be used throughout the program.
A global variable in C is declared outside all function, typically at the top before the main() function, so all
functions can use it.
eg:
#include <stdio.h>
int g = 10; // Global variable
int main()
{
int a = g * 2; // Access global variable
}
5. main() Function Section
Every C program must have one main() function.
Execution of the program starts here.
Contains declarations and executable statements.
eg: int main()
{
6. Subprograms (User-defined Functions)
Functions created by the user for modular programming.
eg: float area(float r)
{
return PI * r * r;
}
=> Example Program: Structure in Action
/* Program to calculate area of circle */
#include <stdio.h> // Link section
#define PI 3.14 // Definition section
// Global declaration
float area(float r);
int main() // main() section
{
float r, a;
printf("Enter radius: ");
scanf("%f", &r);
a = area(r); // Function call
printf("Area = %.2f\n", a);
return 0;
}
// Subprogram
float area(float r)
{
return PI * r * r;
}
Creating and Executing a C Program
To run a C program, the following steps are followed:
1) Creating/Writing the Program
Write the program using a text editor or IDE.
Save it with the extension .c (e.g., prog.c).
2) Compilation
Use a compiler (like GCC or Turbo C) to check the program for syntax errors.
If errors exist, correct them and recompile.
3) Linking
The compiler links the program with necessary library files (e.g., functions like printf).
4) Execution
After successful compilation and linking, an executable file (.exe) is generated.
Run this file to get the output.
Compilation Process in C
When we write a program in C, it cannot be executed directly by the computer. The source code (.c file) must pass
through several stages before becoming an executable file.
The steps are:
-> Preprocessing
The preprocessor handles directives beginning with # such as #include and #define.
It expands macros, includes header files, and removes comments.
The output is an expanded source code.
-> Compilation
The compiler translates the preprocessed code into assembly language.
At this stage, errors like syntax errors are detected.
-> Assembly
The assembler converts the assembly code into object code (.obj or .o file).
Object code is machine-readable but incomplete for execution.
-> Linking
The linker combines the object code with required library files (e.g., stdio.h functions).
It resolves external references (like printf, scanf).
Produces a complete executable file (.exe).
-> Execution
Finally, the operating system loads the executable into memory and runs it.
The program then produces the desired output.