Programming In CUnit 110 min read
Programming Basics: Languages, Algorithms, and C Fundamentals
Unit 1 of Programming In C covers the foundational concepts of programming—from high-level vs. low-level languages and software types to algorithm design, flowcharting, and C’s role as a structured language. Learn how to write clear, efficient programs and understand the tools that power modern applications.
TAKEAWAYS:
- Programming languages are classified by level (high/low) and purpose (general/specialized), with C as a mid-level language bridging hardware and abstraction.
- Algorithms must be finite, unambiguous, input/output-defined, and effective to solve problems efficiently.
- Flowcharts and pseudocode are essential tools for designing algorithms before coding.
- C’s syntax (keywords, case sensitivity, and structure) enforces discipline in writing maintainable programs.
- Real-world applications (e.g., eSewa’s transaction validation or Khalti’s payment routing) rely on structured algorithms and efficient language choices.
- Debugging and testing are critical steps in translating algorithms into working programs.
1. Programming Languages: High-Level vs. Low-Level
Programming languages are tools that allow humans to communicate with computers. They are categorized based on their abstraction level and purpose.
Classification by Level
| Type | Description | Examples | Pros | Cons |
|---|---|---|---|---|
| Low-Level | Directly interacts with hardware (machine code or assembly). | Machine Code, Assembly | Fast execution, hardware control | Hard to write/debug, not portable |
| High-Level | Abstracts hardware details; closer to human language. | C, Java, Python | Easy to write, portable, maintainable | Slower execution, needs compilation |
| Mid-Level | Balances abstraction and hardware access (e.g., C combines low-level control with high-level features). | C, C++ | Flexibility, performance, portability | Requires understanding of memory/hardware |
Highlighting how low-level languages interact directly with registers and memory. (Image: JonRoma (talk) (Uploads), Public domain, via Wikimedia Commons)
Why C?
C is a mid-level language because:
- It provides low-level access (pointers, memory management) for performance-critical tasks.
- It offers high-level abstractions (loops, functions, data types) for readability.
- Used in systems programming (operating systems, embedded systems) and application development (e.g., databases, compilers).
Example:
#include <stdio.h>
int main() {
printf("Hello, World!"); // High-level abstraction
return 0; // Low-level control (exit status)
}
Trace:
| Step | Action | Output/State |
|---|---|---|
| 1 | #include <stdio.h> |
Preprocessor includes library |
| 2 | int main() |
Entry point function called |
| 3 | printf("Hello, World!") |
Outputs text to console |
| 4 | return 0; |
Program exits successfully |
2. Types of Computer Software
Software is classified based on its function and purpose:
Classification Table
| Type | Description | Examples | Role in Programming |
|---|---|---|---|
| System Software | Manages hardware/software resources. | OS (Windows, Linux), Compilers | Provides platform for applications to run. |
| Application Software | Performs specific tasks for users. | Browsers, eSewa, Photoshop | Directly used by end-users. |
| Programming Software | Tools for writing/debugging programs. | IDEs (VS Code), Debuggers (GDB) | Helps developers write and test code. |
| Utility Software | Enhances system performance or adds functionality. | Antivirus, Disk Cleanup | Optimizes or maintains system health. |
In the Real World:
- eSewa uses system software (Linux servers) and application software (backend APIs in C/Java) to process payments.
- Khalti relies on programming software (Python/C++ for fraud detection algorithms) and utility software (firewalls for security).
- NTC’s billing system uses structured algorithms (written in C/Java) to calculate electricity bills based on consumption tiers.
3. Algorithms: The Heart of Programming
An algorithm is a step-by-step procedure to solve a problem. It must satisfy:
- Finiteness: Must terminate after finite steps.
- Definiteness: Each step must be unambiguous.
- Input: Takes zero or more inputs.
- Output: Produces at least one result.
- Effectiveness: Steps must be executable by a machine.
Example: Algorithm to Find the Sum of Two Numbers
Pseudocode:
START
Read num1, num2
sum = num1 + num2
Display sum
STOP
Flowchart:
Trace Table:
| Step | Operation | Input (num1, num2) | Output (sum) |
|---|---|---|---|
| 1 | Read num1, num2 | (5, 7) | - |
| 2 | sum = 5 + 7 | - | 12 |
| 3 | Display 12 | - | 12 |
In the Real World:
- Pathao’s fare calculation uses an algorithm to compute distance + time + base fare.
- Daraz’s order processing follows an algorithm to check stock → validate payment → dispatch.
- NEPSE’s stock price updates rely on algorithms to aggregate and display real-time data.
4. Flowcharts: Visualizing Algorithms
Flowcharts use standard symbols to represent steps:
- Oval: Start/Stop
- Rectangle: Process
- Diamond: Decision (Yes/No)
- Parallelogram: Input/Output
- Arrow: Flow direction
Example: Flowchart for Checking Even/Odd
flowchart TD
A["START"] --> B["Read number"]
B --> C["Is number % 2 == 0?"]
C -->|"Yes"| D["Even"]
C -->|"No"| E["Odd"]
D --> F["STOP"]
E --> FTrace:
| Step | Action | Input (num) | Output |
|---|---|---|---|
| 1 | Read 4 | 4 | - |
| 2 | 4 % 2 == 0? (Yes) | - | Even |
| 3 | STOP | - | Even |
5. Introduction to C Language
C is a procedural, structured language developed by Dennis Ritchie (1972). Key features:
- Case-sensitive:
int≠INT. - Compiled: Source code → Object code → Executable.
- Portable: Write once, compile for any platform.
- Memory-efficient: Direct hardware access.
Structure of a C Program
#include <stdio.h> // Preprocessor directive (includes library)
int main() { // Main function (entry point)
// Code block
return 0; // Exit status
}
Trace of Compilation Process:
flowchart LR
A["Source Code (.c)"] --> B["Preprocessor"]
B --> C["Compiler"]
C --> D["Assembler"]
D --> E["Linker"]
E --> F["Executable (.exe)"]In the Real World:
- Ncell’s billing system uses C for low-latency processing of SMS/voice traffic.
- Bank ATMs run on C/C++ for real-time transaction validation.
- Google’s early systems (e.g., MapReduce) were prototyped in C for performance.
6. Writing Your First C Program: "Hello, World!"
Code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Step-by-Step Execution:
| Step | Action | Output/State |
|---|---|---|
| 1 | #include <stdio.h> |
Links standard I/O library |
| 2 | int main() |
Program starts |
| 3 | printf("Hello, World!\n") |
Prints text + newline |
| 4 | return 0; |
Exits with success code |
Common Errors and Fixes:
| Error | Cause | Fix |
|---|---|---|
error: 'printf' undeclared |
Missing #include <stdio.h> |
Add #include <stdio.h> |
error: expected ';' |
Missing semicolon | Add ; after statements |
warning: return type default |
Missing return in main() |
Add return 0; |
Exam Tip
For definitions:
- Always state properties (e.g., "An algorithm must be finite, unambiguous...").
- Compare high-level vs. low-level languages in a table (as above).
For algorithms:
- Write pseudocode first, then draw the flowchart.
- Show a trace table with inputs/outputs/steps.
- Example: If asked to find the sum of two numbers, include:
- Pseudocode
- Flowchart
- Trace table (with sample inputs)
For C programs:
- Start with
#includeandmain(). - Use meaningful variable names (e.g.,
suminstead ofs). - Include comments explaining key steps.
- Start with
Real-world connections:
- Link algorithms to eSewa’s payment flow or Khalti’s routing logic.
- For C, mention systems programming (e.g., "C is used in Linux kernels because...").
Avoid:
- Writing raw code without explanation.
- Forgetting to initialize variables before use.
- Using magic numbers (e.g.,
if (x == 5)without context).
Practice Question: Design an algorithm to check if a number is prime, then:
- Draw the flowchart.
- Write the pseudocode.
- Trace it for input
7and10. - Write the equivalent C program.
Based on the PU BE Computer (PU) syllabus for Programming In C, unit 1.
Discussion
Loading…