Unit 5: Programming Concepts and Logics (C Programming Basics)
This note is made for NEB Grade 11 Computer Science. This post contains the complete note of Unit 5 in simple and easy understanding way, based on the syllabus of CDC, along with sample board exam questions based on the official 2077 Specification Grid.
Why Our Note
Most notes for this unit stop at C program examples and skip the theory that examiners actually test. Syntax/semantic/runtime errors, low-level vs high-level vs 4GL languages, pseudocode, and character encoding (absolute binary, BCD, ASCII, Unicode) all appear directly in the CDC curriculum, but get left out of a lot of popular notes online. This one covers every sub-topic from the official syllabus, and backs each C concept with a full, runnable program and its actual output, not just a definition.
Table of Content
- 5.1 Programming Concept
- 5.1.1 Introduction to Programming Languages
- 5.1.2 Low Level, High Level and 4GL Languages
- 5.1.3 Compiler, Interpreter and Assembler
- 5.1.4 Syntax, Semantic and Runtime Errors
- 5.1.5 Control Structures
- 5.1.6 Program Design Tools
- 5.1.7 Absolute Binary, BCD, ASCII and Unicode
- 5.2 C Programming Language
- 5.2.1 Introduction and Features of C
- 5.2.2 Structure of a C Program
- 5.2.3 C Preprocessor and Header Files
- 5.2.4 Character Set Used in C
- 5.2.5 Use of Comments
- 5.2.6 Identifiers, Keywords and Tokens
- 5.2.7 Basic Data Types in C
- 5.2.8 Constants and Variables
- 5.2.9 Type Specifiers
- 5.2.10 Simple and Compound Statements
- 5.2.11 Operators and Expressions
- 5.2.12 Input/Output Functions
- 5.2.13 Selection Control Statements
- 5.2.14 Iteration Control Statements
- 5.2.15 Arrays
- 5.2.16 Strings
- Solved Program Examples
- Activities and Practice
- Common Mistakes to Avoid
- Sample Board Exam Questions
- Important Questions
- Frequently Asked Questions
5.1 Programming Concept
5.1.1 Introduction to Programming Languages
A computer only understands electrical signals in the form of 0s and 1s. A programming language gives humans a way to write instructions in a form they can actually read and reason about, which is then converted into a form the machine can run.
| Term | Meaning |
|---|---|
| Program | A set of instructions given to a computer to perform a specific task |
| Programming Language | A formal language with its own rules, used to write programs (e.g. C, C++, Java, Python) |
| Programmer | The person who writes computer programs |
| Source Code | The program as written by the programmer, in a high-level language, before translation |
| Object Code / Machine Code | The translated version of the source code, in binary, that the processor can actually run |
Writing a program generally follows the same rough sequence, no matter which language is used: understand the problem, plan the logic (using an algorithm, flowchart, or pseudocode), write the source code, translate it into machine code, and then test and fix it. Skipping the planning step is one of the most common reasons beginner programs end up buggy or hard to follow.
5.1.2 Low Level, High Level and 4GL Programming Languages
Programming languages are grouped by how close they sit to the hardware versus how close they sit to human language.
- Low Level Language: written close to the hardware, using machine code (pure binary) or assembly language (short mnemonics like MOV, ADD). Fast to run but hard to write and read.
- High Level Language: written closer to human language, using English-like keywords and readable syntax. Easier to write, debug, and maintain, though it needs to be translated before the machine can run it. Examples: C, C++, Java, Python.
- Fourth Generation Language (4GL): designed to be even more accessible than a typical high-level language, often used for a specific purpose such as database queries or report generation, with less code required to get a result. SQL is a common example.
5.1.3 Compiler, Interpreter and Assembler
Source code written by a programmer has to be translated into a form the processor can execute. Different languages use different translators to do this.
| Translator | How It Works | Example |
|---|---|---|
| Compiler | Translates the entire source code into machine code in one go, before the program runs. Errors are reported all together after compilation. | C, C++ |
| Interpreter | Translates and executes the source code line by line, stopping as soon as it hits an error. | Python, early BASIC |
| Assembler | Translates assembly language (mnemonics) directly into machine code. | Assembly language programs |
C is a compiled language: the whole program is translated by the compiler into an executable file first, and only then is it run.
5.1.4 Syntax, Semantic and Runtime Errors
- Syntax Error: a mistake in the grammar of the language, such as a missing semicolon or an unmatched bracket. The compiler catches this before the program can even run.
- Semantic Error: the code follows the rules of the language correctly but does not do what the programmer actually intended, such as using the wrong variable in a formula.
- Runtime Error: an error that only appears while the program is running, such as dividing a number by zero or trying to access an array index that doesn't exist.
5.1.5 Control Structures: Sequence, Selection and Iteration
A control structure decides the order in which the statements of a program are executed.
| Control Structure | What It Does |
|---|---|
| Sequence | Statements run one after another, in the exact order they are written |
| Selection | The program picks a path to follow based on whether a condition is true or false (if, if-else, switch) |
| Iteration | A block of statements repeats until a condition is met (while, do-while, for) |
5.1.6 Program Design Tools: Algorithm, Flowchart and Pseudocode
Before writing actual code, a programmer usually plans out the logic of a program using one of these tools.
- Algorithm: a step-by-step written procedure for solving a problem, described in plain language rather than any particular programming language. A good algorithm is finite (it must end), definite (every step is precise and unambiguous), and effective (each step is simple enough to actually carry out).
- Flowchart: a diagram that represents the steps of a program visually, using standard symbols.
- Pseudocode: a structured, code-like description of a program's logic, written in plain English but following a programming-style layout, without worrying about the exact syntax of any real language.
Solved Example: Design Tools for "Find the Largest of Two Numbers"
Algorithm:
- Start
- Read two numbers, A and B
- If A is greater than B, display A as the largest
- Otherwise, display B as the largest
- Stop
Pseudocode:
Flowchart:
5.1.7 Absolute Binary, BCD, ASCII and Unicode
A computer stores every type of data, whether it is a number, a letter, or a symbol, in binary. These four terms describe different ways that binary is used to represent information.
- Absolute Binary: data represented directly in raw 0s and 1s, the actual form in which a computer's hardware stores and processes everything.
- BCD (Binary Coded Decimal): a way of encoding numbers where each individual decimal digit is stored as its own separate 4-bit binary value, rather than converting the whole number to binary at once. For example, the decimal number 25 is stored as 0010 0101 (2 = 0010, 5 = 0101), not as the binary equivalent of 25 as a whole.
- ASCII (American Standard Code for Information Interchange): a character encoding standard that assigns a unique numeric code (originally 7-bit, later extended to 8-bit) to English letters, digits, punctuation, and control characters. For example, the capital letter 'A' is represented as 65 in ASCII.
- Unicode: a character encoding standard built to represent text from virtually every writing system in the world, not just English, using more bits per character than ASCII to fit the much larger number of symbols. This is what allows a computer to correctly display Devanagari script alongside English text.
5.2 C Programming Language
5.2.1 Introduction and Features of C
C shows up everywhere from operating systems to compilers to everyday applications, mostly because it's efficient and gives a programmer close control over how a program runs.
| Feature | What It Means |
|---|---|
| Structured Language | A program is organized into functions, and logic is broken into clear, ordered steps |
| Middle-level Language | Reads like a high-level language, but still allows fairly direct control over hardware and memory, like a low-level language |
| Portability | C code can run on different types of computers with little or no change |
| Rich Library Functions | Comes with a large set of built-in functions (in header files) for common tasks |
| Memory Management | Allows direct memory access using pointers, giving fine-grained control |
| Fast Execution Speed | Compiled C programs are generally very fast, since they run as native machine code |
5.2.2 Structure of a C Program
Every C program follows roughly the same basic layout:
- Preprocessor directives start with
#and are processed before compilation.#include <stdio.h>tells the compiler to bring in the standard input/output header file. - Global variables are declared outside every function and can be accessed from anywhere in the program.
- main() is the most important part of any C program. Execution always begins and ends there, and every program must have exactly one.
- Local variables are declared inside a function and can only be accessed within it.
return 0;inside main() signals that the program ran successfully; a non-zero value usually signals an error.
5.2.3 C Preprocessor and Header Files
A preprocessor directive is an instruction that starts with a # symbol and is handled before the actual compilation begins. The most common ones are #include, which brings in a header file, and #define, which creates a macro.
A header file (with a .h extension) contains the declarations of functions and macros that belong to the C standard library. For example, stdio.h must be included before a program can use functions like printf() and scanf(), and string.h must be included before using functions like strlen() or strcpy().
5.2.4 Character Set Used in C
The character set is the collection of characters that C recognizes as valid when writing a program. It includes:
- Alphabets: uppercase A-Z and lowercase a-z
- Digits: 0-9
- Special characters: such as
+ - * / % ; , . ( ) { } [ ] # & - White space characters: blank space, tab, and newline
5.2.5 Use of Comments
A comment is text in a program that the compiler ignores completely. It exists purely to help a human reader understand the code.
- Single-line comment: starts with
//and runs to the end of the line - Multi-line comment: starts with
/*and ends with*/, and can span several lines
5.2.6 Identifiers, Keywords and Tokens
- Identifiers: the names a programmer gives to variables, functions, and other elements in a program, such as
totalorcalculateArea. - Keywords: reserved words that already have a fixed meaning in C, such as
int,if,while, andreturn. A keyword can never be used as an identifier. - Tokens: the smallest individual units a C compiler recognizes while reading a program, which includes keywords, identifiers, constants, operators, and punctuation.
Naming rules for an identifier:
- Must begin with an alphabet (A-Z, a-z) or an underscore (
_) - Can contain alphabets, digits (0-9), and underscores after the first character
- Cannot contain spaces or special symbols other than an underscore
- Cannot be a reserved keyword
- Case-sensitive —
ageandAgeare treated as two different identifiers
5.2.7 Basic Data Types in C
| Data Type | Description | Typical Size | Format Specifier |
|---|---|---|---|
| int | Whole numbers, positive or negative | 2 or 4 bytes | %d or %i |
| float | Single-precision decimal numbers | 4 bytes | %f |
| double | Double-precision decimal numbers, more accurate than float | 8 bytes | %lf |
| char | A single character | 1 byte | %c |
| void | Represents the absence of a value or type | - | - |
5.2.8 Constants and Variables
- Variable: a named location in memory that holds a value which can change during program execution, such as
int score = 0;. It must be declared with a data type before use. - Constant: a fixed value that does not change while the program is running.
Types of constants: integer (10, -5), floating-point (3.14, 2.5e-3), character ('A', enclosed in single quotes), and string literals ("Nepal", enclosed in double quotes).
A constant can be defined either with the const keyword (const float PI = 3.14159;) or with the #define preprocessor directive (#define PI 3.14159).
5.2.9 Type Specifiers
A type specifier modifies a basic data type to adjust its size or the range of values it can store. C provides four: short, long, signed, and unsigned. For example, unsigned int can only store non-negative numbers, but in exchange it can hold a larger positive range than a plain signed int.
5.2.10 Simple and Compound Statements
- Simple statement: a single instruction ending with a semicolon, such as
a = a + 1; - Compound statement (block): a group of statements enclosed within curly braces
{ }, treated by the compiler as a single unit, commonly used inside loops and conditions.
5.2.11 Operators and Expressions
An operator is a symbol that performs an operation on operands, and an expression is a combination of operators, operands, and function calls that evaluates to a single value.
| Type | Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / % | Performs mathematical calculations |
| Relational | == != < > <= >= | Compares two values, returns true or false |
| Logical | && || ! | Combines or negates conditions |
| Assignment | = += -= *= /= %= | Assigns or updates a value in a variable |
| Increment/Decrement | ++ -- | Increases or decreases a variable's value by 1 (pre or post) |
| Conditional (ternary) | ? : | A shorthand for a simple if-else, e.g. (a > b) ? a : b |
Note: pre-increment (++a) changes the value before it is used in an expression, while post-increment (a++) changes it after. The same applies to decrement.
5.2.12 Input/Output Functions
C uses two standard library functions for basic input and output, both defined in stdio.h.
Common format specifiers: %d/%i for integers, %f for floats (%.2f for 2 decimal places), %c for characters, %s for strings, \n for a newline, \t for a tab space.
The & (address-of) operator is essential with scanf(), since it tells the function the memory address to store the input at. For reading a string with scanf("%s", name), & is not needed because an array name already acts as a pointer to its own base address. Note that scanf() with %s stops reading at the first space, so fgets() is used instead when a string may contain spaces.
5.2.13 Selection Control Statement: Decisions
Selection statements let a program choose which block of code to run, based on a condition.
if statement
if-else statement
Nested if-else / if-else-if ladder
switch statement
The break statement exits the switch block once a match is found. If it's left out, execution "falls through" to the next case. The default case runs only if nothing else matched.
| Feature | if-else | switch |
|---|---|---|
| Condition type | Any boolean expression, using relational/logical operators | A single variable compared against fixed integer/character values |
| Flexibility | Can handle ranges and complex, multi-part conditions | Best suited to a fixed, known set of values |
| Execution | Runs the first block whose condition is true | Runs the block matching the case value; needs break to avoid fall-through |
5.2.14 Iteration Control Statement: Looping
Iteration statements repeat a block of code as long as a condition holds true.
for loop
while loop
do-while loop
| Loop | Condition Checked | Minimum Executions |
|---|---|---|
| while | Before the body runs | 0 (may never run) |
| do-while | After the body runs | 1 (always runs once) |
| for | Before the body runs, with init/update built in | 0 (may never run) |
A nested loop is a loop placed inside another loop, most often used to work through a 2D array or matrix row by row.
5.2.15 Arrays: 1D and 2D
1D (One-Dimensional) Array
A 1D array is a simple, linear list of values, declared as dataType arrayName[size];.
2D (Two-Dimensional) Array
A 2D array is an array of arrays, visualized as a table of rows and columns, declared as dataType arrayName[rows][columns];. Each element needs two indices, one for the row and one for the column.
5.2.16 Strings and String Functions
\0), which marks where the string stops. The string.h header file provides functions to work with strings.
| Function | What It Does | Example |
|---|---|---|
| strlen() | Returns the length of a string, not counting the null character | strlen("Nepal") returns 5 |
| strcpy() | Copies one string into another | strcpy(dest, "Hello") |
| strcat() | Joins (concatenates) one string onto the end of another | strcat(s1, " World") |
| strcmp() | Compares two strings; returns 0 if equal | strcmp("apple","banana") returns negative |
| strrev() | Reverses a string | strrev("C Prog") gives "gorP C" |
| strupr() | Converts a string to uppercase | strupr("hello") gives "HELLO" |
| strlwr() | Converts a string to lowercase | strlwr("HELLO") gives "hello" |
Note: strrev(), strupr(), and strlwr() are not part of the official ANSI C standard, but they are available as common extensions in compilers such as Turbo C and GCC. For strict portability, the same result can be achieved manually using toupper()/tolower() from ctype.h inside a loop.
Solved Program Examples
A definition is easier to remember once it has been used in an actual program. The programs below cover the most commonly asked coding questions from this unit, each with its expected output.
1. Sum of Two Numbers
2. Check Whether a Number is Even or Odd
3. Factorial of a Number Using a Loop
4. Sum and Average of 5 Numbers Using a 1D Array
5. Input and Display a 2x3 Matrix
6. Reverse a String
Activities and Practice
Write an algorithm to check if a given number is even or odd.
An algorithm is a step-by-step procedure to solve a problem:
What are the key differences between a while loop and a do-while loop?
A while loop checks its condition before executing the body, so it may not run at all if the condition starts out false. A do-while loop executes the body once first, then checks the condition, so it always runs at least once. For example, while (i < 0) never runs if i starts at 5, but the equivalent do { ... } while (i < 0) still runs once before stopping.
Why isn't the & operator always needed with scanf()?
The & (address-of) operator gives scanf() the memory address of a variable, so it knows exactly where to store the value typed in. Arrays are the exception: when reading a string with %s, the array's name already refers to its base memory address on its own, so adding & in front of it is unnecessary.
Common Mistakes to Avoid
Watch out for these while writing C programs
- Forgetting the semicolon
;at the end of a statement, which causes a syntax error - Using
=(assignment) instead of==(comparison) inside an if condition - Forgetting to include the correct header file, such as
string.hfor string functions - Mixing up format specifiers, such as using
%dfor afloatvalue instead of%f - Leaving out the
&(address-of) operator before a variable inscanf() - Forgetting
breakinside a switch statement, causing unwanted fall-through to the next case - Confusing array indexing — a C array of size
nis indexed from0ton-1, not1ton
Sample Board Exam Questions
Based on the official NEB Computer Science Specification Grid 2077, Unit 5 (Programming Concepts and Logics) carries 5 marks out of the theory paper, drawn mainly from the Remembering and Understanding levels. Given the size of this unit, MCQs are the most common format, but SAQ and LAQ patterns from past board papers are included below too, since programming questions can appear this way in practice.
Group A: Multiple Choice Questions 1 × 5 = 5
- Which type of translator converts the entire source code into machine code before execution begins?
- a) Interpreter
- b) Compiler
- c) Assembler
- d) Linker
- An error that occurs while a program is running, such as division by zero, is known as a:
- a) Syntax error
- b) Semantic error
- c) Runtime error
- d) Logical error
- Which program design tool uses standard symbols such as ovals, rectangles, and diamonds?
- a) Algorithm
- b) Pseudocode
- c) Flowchart
- d) Source code
- Which header file must be included in a C program to use printf() and scanf()?
- a) math.h
- b) stdio.h
- c) string.h
- d) conio.h
- Which loop in C is guaranteed to execute its body at least once?
- a) for loop
- b) while loop
- c) do-while loop
- d) nested loop
Group B: Short Answer Question 5 marks
Write a C program to input five numbers from the user and calculate their sum and average using a one-dimensional array.
Explanation: the program declares an array of 5 integers, reads each value with a for loop while adding it to a running total, then divides that total by 5 (cast to float, so the division isn't truncated) to get the average.
Group C: Long Answer Question 8 marks
Define an array. Explain 1D and 2D arrays with their declaration and initialization, and write a C program to input and display the elements of a 2x3 matrix.
See Section 5.2.15 for the array definitions, and the fifth solved program above for the full working code with output.
Important Questions
- What is a programming language? Differentiate between low level and high level languages.
- Differentiate between compiler and interpreter with examples.
- Define syntax error, semantic error, and runtime error with one example of each.
- What are control structures? Explain sequence, selection, and iteration with examples.
- What is an algorithm? Write an algorithm to find the largest of three numbers.
- Draw a flowchart to check whether a given number is even or odd.
- Differentiate between ASCII and Unicode.
- What is BCD? How is it different from absolute binary?
- List the features of the C programming language.
- What is a header file? Explain the use of the C preprocessor with an example.
- Differentiate between a keyword and an identifier with examples.
- What are the basic data types available in C?
- Differentiate between a constant and a variable.
- Write a C program to find the sum of two numbers entered by the user.
- Differentiate between if-else and switch statements
- Differentiate between while and do-while loops with an example of each.
- Write a C program to print numbers from 1 to 10 using a for loop.
- Differentiate between a 1D array and a 2D array with examples.
- Write a C program to add two 3x3 matrices.
- Define string. List any four string handling functions used in C with their purpose.
- Write a C program to find the sum and average of 5 numbers using an array.
Frequently Asked Questions
Is C programming hard for a Grade 11 student who has never coded before?
Not if the basics are practiced regularly. Most of Unit 5 is about recognizing patterns (a loop, a condition, a data type) rather than memorizing theory alone. Typing out and running the solved programs above a few times, then tweaking the numbers, tends to build confidence faster than reading definitions alone.
What is the difference between an algorithm and a flowchart?
Both describe the same logic. An algorithm writes the steps out in plain sentences, one after another, while a flowchart shows the same steps as a diagram using standard symbols. A flowchart is often easier to follow at a glance, while an algorithm is quicker to write down.
Why does the do-while loop always run at least once?
Because it checks its condition after running the body, not before. A while loop or for loop can skip the body entirely if the condition is false right from the start, but a do-while loop has already executed the body once by the time it checks anything.
Are strrev(), strupr(), and strlwr() part of standard C?
No. These three are common compiler extensions (available in Turbo C and GCC, among others) rather than part of the official ANSI C standard. They're still fair game in NEB programs since the prescribed compiler supports them, but it's worth knowing they aren't guaranteed to exist on every C compiler.
Is this unit mostly theory or mostly programming in the board exam?
Based on the 2077 Specification Grid, this unit carries 5 marks, mainly at the Remembering and Understanding levels. That usually means it is tested through Multiple Choice Questions on terminology (compiler vs interpreter, error types, data types, and so on) rather than long coding questions, though past papers have also included short and long answer programming questions, which is why worked examples for both are included above.



