C Programming · Unit 1 · 2 hrs
Problem Solving with Computer
Exam-focused notes for Problem Solving with Computer (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Problem Solving with Computer in short
This chapter traces how a problem becomes a working C program: analysing what is being asked, designing an algorithm and flowchart, writing and compiling the source code, and then debugging, testing and documenting it, before looking at where the C language itself came from and how a C program is structured.
What this unit covers
- Problem analysis
- Algorithms and Flowchart
- Coding
- Compilation and Execution
- History of C
- Structure of C program
- Debugging, Testing and Documentation
Structure of C program
2081 · 5 marks
Explain the basic structure of C Programming. [5]
2074 · 10 marks
Discuss structure of a C program with suitable example.[10]
Show solved answerHide answer
Structure of a C Program
Introduction
Every C program follows a standard, well-defined structure. Understanding this structure is essential because the compiler processes each section in a specific order. A well-structured C program is easier to read, debug, and maintain.
Sections of a C Program
A C program is typically divided into the following sections:
1. Documentation Section
- Contains comments that describe the purpose of the program, author name, date, etc.
- Comments are ignored by the compiler.
- Single-line comment:
// comment - Multi-line comment:
/* comment */
/* Program to add two numbers
Author: Ram
Date: 2081 */
2. Preprocessor Directives
- Lines that begin with
#are processed before compilation. #includeis used to include header files (libraries).#defineis used to define constants or macros.- These directives make library functions like
printf()andscanf()available to the program.
#include <stdio.h>
#define PI 3.14
3. Global Declarations
- Variables or functions declared outside all functions are called global.
- They are accessible throughout the entire program.
int count = 0; /* global variable */
4. main() Function
- The
main()function is the entry point of every C program. - Execution always begins from
main(). - It returns an integer value to the operating system.
return 0signals that the program ended successfully.- Curly braces
{ }define the body of the function. - Every statement inside must end with a semicolon (
;).
int main()
{
/* statements */
return 0;
}
5. User-Defined Functions
- Functions written by the programmer to perform specific tasks.
- They are defined outside
main()but can be called frommain()or other functions. - They help in making the program modular and reusable.
int add(int x, int y)
{
return x + y;
}
Complete Example
The following example demonstrates all sections of a C program:
/* Program to add two numbers and display the result */ /* 1. Documentation */
#include <stdio.h> /* 2. Preprocessor directive */
int result; /* 3. Global declaration */
int add(int x, int y); /* Function prototype */
int main() /* 4. main() function */
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
result = add(a, b);
printf("Sum = %d\n", result);
return 0; /* Signals successful end */
}
int add(int x, int y) /* 5. User-defined function */
{
return x + y;
}
Sample Output
Enter two numbers: 5 3
Sum = 8
Explanation of Key Elements
| Element | Purpose |
|---|---|
#include <stdio.h> | Includes standard I/O library for printf() and scanf() |
int main() | Starting point of the program; returns integer to OS |
{ } | Curly braces define the body of a function |
return 0 | Signals the program ended successfully |
; | Every statement must end with a semicolon |
scanf() | Reads input from the user |
printf() | Displays output to the screen |
How a C Program Runs
Once the source code is written following the above structure, it goes through the following stages before producing output:
source.c --> Compiler --> source.obj --> Linker --> source.exe --> Output
- Compilation: The compiler translates source code into object code and catches syntax errors.
- Linking: The linker combines object code with library functions (like
printf,scanf) to produce an executable file. - Loading: The loader places the executable into memory.
- Execution: The CPU runs the program and produces output.
Summary
A well-structured C program contains five main sections: documentation, preprocessor directives, global declarations, the main() function, and user-defined functions. Following this structure makes programs organized, readable, and easy to maintain. The main() function is mandatory in every C program as it serves as the entry point for execution.
History of C
2078 · 5 marks
Write short notes on: History of C, Bitwise operator. [5]
Algorithms and Flowchart
2077 · 5 marks
Explain flowchart with example. What are the benefits of using flowchart? What are the benefits of using flowchart? [3+2]
2074 · 5 marks
What is algorithm? How is it different from flow chart? [2+3]
Show solved answerHide answer
Algorithm and Flowchart
What is an Algorithm? [2 marks]
An algorithm is a finite, step-by-step set of instructions to solve a problem. It must be clear, unambiguous, and always terminate.
Properties of a Good Algorithm:
| Property | Description |
|---|---|
| Finiteness | Must end after a finite number of steps |
| Definiteness | Each step must be clear and unambiguous |
| Input | Has zero or more inputs |
| Output | Produces at least one output |
| Effectiveness | Each step must be simple enough to be carried out |
Example: Algorithm to find the largest of two numbers
Step 1: Start.
Step 2: Read a and b.
Step 3: If a > b, print a. Else print b.
Step 4: Stop.
Difference Between Algorithm and Flowchart [3 marks]
A flowchart is a diagram that represents an algorithm using standard symbols connected by arrows. It shows the flow of control from start to end.
| Basis | Algorithm | Flowchart |
|---|---|---|
| Nature | Written in plain text or pseudocode as numbered steps | Represented as a diagram using standard symbols and arrows |
| Form | Textual / verbal description | Visual / graphical representation |
| Symbols | No special symbols required | Uses standard symbols (oval, rectangle, diamond, parallelogram, etc.) |
| Ease of Understanding | Easier to write but may be harder to follow for complex logic | Easier to understand visually, especially for branching and loops |
| Modification | Easy to modify by editing text | Harder to modify as the diagram must be redrawn |
| Purpose | Describes the logic of a solution in steps | Depicts the flow of control from start to end graphically |
Key Relationship
An algorithm and a flowchart both serve the same purpose: to plan and represent the solution to a problem before coding. The algorithm is the textual form, while the flowchart is its graphical equivalent. A flowchart is essentially a visual translation of an algorithm.
Compilation and Execution
2077 · 5 marks
Write short notes on: Compilation and execution, Operator precedence and associativity. [5]
Make Unit 1 stick
Practice CSC115 with flashcards & quizzes