Class 12 Computer Science Programming II Notes and Important Questions
Learn
Loading notes…
Notes for Class 12 Programming II (Computer Science) are shown above.
Class 12 Programming II Notes (Computer Science)
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.1 Review of C Programming Concepts C is a general-purpose, procedural, middle-level programming language developed by Dennis Ritchie at Bell Labs (1972).
Key points
- NEB Class 12 | Computer Science | Unit 4 Programming in C Unit 4 Programming in C Functions
- Structures & Unions
- Pointers
- Files NEB Grade XII
- Computer Science Syllabus Topics Covered 4.1 Review of C Programming Concepts 4.2 Functions 4.2.1 Library & User-defined functions 4.2.2 Definition, Prototype, Call & Return 4.2.3 Passing values to functions 4.2.4 Storage classes (auto & external) 4.2.5 Recursion (Factorial & Fibonacci) 4.3 Structures and Unions 4.4 Pointers 4.5 Working with Files These notes follow the official NEB Class 12 Computer Science syllabus. Practice every code example on your computer for better understanding. — 1 — Study Notes
- Handwritten Style NEB Grade XII
- NEB Class 12 | Computer Science | Unit 4 Programming in C Unit 4 Programming in C Functions
- Structures & Unions
- Pointers
- Files NEB Grade XII
- Computer Science Syllabus Topics Covered 4.1 Review of C Programming Concepts 4.2 Functions 4.2.1 Library & User-defined functions 4.2.2 Definition, Prototype, Call & Return 4.2.3 Passing values to functions 4.2.4 Storage classes (auto & external) 4.2.5 Recursion (Factorial & Fibonacci) 4.3 Structures and Unions 4.4 Pointers 4.5 Working with Files These notes follow the official NEB Class 12 Computer Science syllabus. Practice every code example on your computer for better understanding. — 1 — Study Notes
- Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.1 Review of C Programming Concepts C is a general-purpose, procedural, middle-level programming language developed by Dennis Ritchie at Bell Labs (1972). It is the foundation of many modern languages. Key Features of C Simple, efficient and fast Rich set of operators and data types Supports structured programming Low-level memory access via pointers Portable (write once, compile anywhere) Extensible with functions and libraries Basic Structure of a C Program #include <stdio.h> // Preprocessor directive int main() { // main function – entry point // Variable declarations int a = 10; float b = 3.14; char c = 'A'; // Statements / Logic printf("Value of a = %d\n", a); printf("Value of b = %.2f\n", b); return 0; // Exit status } Data Types (Quick Review) Type Size (typical) Example int 2 or 4 bytes int x = 25; float 4 bytes float p = 3.14; double 8 bytes double d = 2.718; char 1 byte char ch = 'A'; void 0 bytes No value / empty Remember: C is case-sensitive. Every statement ends with a semicolon (;). Comments: // or /* */ — 2 — Study Notes • Handwritten Style NEB Grade XII
- NEB Class 12 | Computer Science | Unit 4 Programming in C 4.2 Functions A function is a self-contained block of code that performs a specific task. It helps in modular programming, code reusability and easier debugging. 4.2.1 Library vs User-defined Functions
- Library (Built-in) Functions Pre-written functions available in C standard libraries. We only need to include the header file and call them. stdio.h printf(), scanf(), getchar(), putchar() math.h sqrt(), pow(), sin(), cos(), abs() string.h strlen(), strcpy(), strcmp(), strcat() stdlib.h malloc(), free(), exit(), atoi()
- User-defined Functions Functions created by the programmer according to the need of the program. Advantages of Functions Code reusability – write once, use many times Modularity – break large program into small parts Easier debugging and testing Better readability and maintenance Avoids code duplication — 3 — Study Notes
- Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.2.2 Function Definition, Prototype, Call & Return 1. Function Prototype (Declaration) Tells the compiler about the function name, return type and parameters before it is used. return_type function_name(parameter_list); // Examples: int add(int a, int b); void display(void); float area(float radius); 2. Function Definition Contains the actual body of the function. return_type function_name(parameter_list) { // local variables // statements return value; // optional if void } 3. Function Call function_name(arguments); // Example: int result = add(5, 3); display(); 4. return Statement Sends a value back to the calling function and terminates the function. return expression; // returns a value return; // for void functions Complete Example #include <stdio.h> int add(int a, int b); // Prototype int main() { int x = 10, y = 20, sum; sum = add(x, y); // Function call printf("Sum = %d", sum); return 0; } int add(int a, int b) { // Definition return a + b; // Return statement } — 4 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.2.3 Accessing a Function by Passing Values Data can be passed to a function in two main ways: 1. Call by Value (Pass by Value) A copy of the actual argument is passed. Changes made inside the function do NOT affect the original variables. #include <stdio.h> void change(int x) { x = x + 10; printf("Inside function: %d\n", x); } int main() { int a = 5; change(a); printf("In main: %d\n", a); // still 5 return 0; } // Output: // Inside function: 15 // In main: 5 2. Call by Reference (using Pointers) Address of the variable is passed. Changes inside the function DO affect the original variable. #include <stdio.h> void change(int *x) { *x = *x + 10; printf("Inside function: %d\n", *x); } int main() { int a = 5; change(&a); // pass address printf("In main: %d\n", a); // now 15 return 0; } // Output: // Inside function: 15 // In main: 15 Call by value is safer (original data protected). Call by reference is useful when you need to modify the original data or return multiple values. — 5 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.2.4 Concept of Storage: Automatic & External Storage class determines the lifetime, scope and default initial value of a variable. 1. Automatic (auto) Storage Class Default storage class for local variables Created when function is called, destroyed when function ends Scope: local to the block/function Default value: garbage (undefined) void demo() { auto int x = 10; // or simply: int x = 10; printf("%d", x); } // x is destroyed here 2. External (extern) Storage Class Used for global variables shared across multiple files Lifetime: entire program Default value: 0 Declared with keyword extern // file1.c int count = 0; // definition // file2.c extern int count; // declaration void increment() { count++; } Other Storage Classes (for completeness) Class Keyword Lifetime Scope Automatic auto Function Local External extern Program Global Static static Program Local/Global Register register Function Local — 6 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.2.5 Concept of Recursion Recursion is a process in which a function calls itself. Every recursive function must have a base case (termination condition) to avoid infinite calls. Factorial using Recursion n! = n × (n-1)! and 0! = 1 #include <stdio.h> long factorial(int n) { if (n == 0 || n == 1) // Base case return 1; else return n * factorial(n - 1); // Recursive call } int main() { int num = 5; printf("Factorial of %d = %ld", num, factorial(num)); return 0; } // Output: Factorial of 5 = 120 Fibonacci Series using Recursion Fib(0)=0, Fib(1)=1, Fib(n)=Fib(n-1)+Fib(n-2) #include <stdio.h> int fibonacci(int n) { if (n == 0) return 0; // Base case if (n == 1) return 1; // Base case return fibonacci(n-1) + fibonacci(n-2); } int main() { int i, terms = 8; printf("Fibonacci Series: "); for (i = 0; i < terms; i++) printf("%d ", fibonacci(i)); return 0; } // Output: 0 1 1 2 3 5 8 13 ! Recursion uses stack memory. Too deep recursion can cause stack overflow. For large n, iterative solutions are often more efficient. — 7 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.3 Structures and Unions 4.3.1 Structure: Definition, Declaration, Initialization A structure is a user-defined data type that groups related variables of different data types under a single name. // Definition / Template struct Student { int roll; char name[30]; float marks; }; // Declaration of variables struct Student s1, s2; // Initialization struct Student s3 = {101, "Ram", 85.5}; // Another way struct Student s4; s4.roll = 102; strcpy(s4.name, "Sita"); s4.marks = 90.0; Size of Structure sizeof operator gives the total memory occupied by a structure (sum of sizes of members + possible padding). printf("Size = %lu bytes", sizeof(struct Student)); // Typical: 4 (int) + 30 (char) + 4 (float) + padding ≈ 40 bytes 4.3.2 Accessing Members of Structure Use the dot (.) operator for structure variables and arrow (->) operator for pointers to structures. struct Student s1 = {101, "Hari", 78.5}; printf("Roll : %d\n", s1.roll); printf("Name : %s\n", s1.name); printf("Marks : %.1f\n", s1.marks); // Using pointer struct Student *ptr = &s1; printf("Name via ptr: %s", ptr->name); — 8 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.3.3 Array of Structure We can create an array where each element is a structure. Useful for storing records of many students, employees, etc. #include <stdio.h> struct Student { int roll; char name[20]; float marks; }; int main() { struct Student s[3]; int i; // Input for (i = 0; i < 3; i++) { printf("Enter roll, name, marks: "); scanf("%d %s %f", &s[i].roll, s[i].name, &s[i].marks); } // Display printf("\n--- Student Records ---\n"); for (i = 0; i < 3; i++) { printf("%d %s %.1f\n", s[i].roll, s[i].name, s[i].marks); } return 0; } 4.3.4 Union: Definition & Declaration A union is similar to a structure but all members share the same memory location. Only one member can hold a value at a time. union Data { int i; float f; char str[20]; }; union Data d1; d1.i = 10; printf("%d", d1.i); // 10 d1.f = 3.14; printf("%f", d1.f); // 3.14 (i is overwritten) 4.3.5 Difference between Structure and Union Feature Structure Union Keyword struct union Memory Sum of all members Size of largest member Access All members at once Only one at a time Value All members retain Only last assigned Use case Different data together Same memory, different types — 9 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.4 Pointers 4.4.1 Definition of Pointer A pointer is a variable that stores the memory address of another variable. It “points to” the location of data rather than holding the data itself. int x = 25; int *ptr; // declaration of pointer to int ptr = &x; // ptr now holds address of x printf("Value of x = %d\n", x); printf("Address of x = %p\n", &x); printf("Value of ptr = %p\n", ptr); printf("Value pointed= %d\n", *ptr); // 25 4.4.2 Address (&) and Indirection (*) Operators & (Address-of operator) – returns the memory address of a variable * (Indirection / Dereference operator) – accesses the value at the address stored in the pointer int a = 10; int *p = &a; // p stores address of a printf("%d", a); // 10 (value of a) printf("%p", &a); // address of a printf("%p", p); // same address printf("%d", *p); // 10 (value at address p) *p = 50; // changes value of a to 50 printf("%d", a); // 50 4.4.3 Pointer Expression and Assignment int x = 5, y = 10; int *p1, *p2; p1 = &x; // p1 points to x p2 = &y; // p2 points to y p1 = p2; // p1 now also points to y *p1 = 100; // y becomes 100 // Pointer arithmetic int arr[5] = {10, 20, 30, 40, 50}; int *p = arr; // points to first element printf("%d", *p); // 10 printf("%d", *(p+2)); // 30 p++; // move to next element printf("%d", *p); // 20 — 10 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.4.4 Call by Value and Call by Reference This topic is closely related to pointers (already introduced in 4.2.3). Here is a focused summary with pointers. Call by Value Only the value is copied. The function works on a local copy. void swap(int a, int b) { int temp = a; a = b; b = temp; } // original variables remain unchanged Call by Reference (using Pointers) Addresses are passed so the function can modify the original variables. #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; printf("Before: x=%d y=%d\n", x, y); swap(&x, &y); printf("After : x=%d y=%d\n", x, y); return 0; } // Output: // Before: x=10 y=20 // After : x=20 y=10 When you need a function to modify more than one variable or return multiple results, use call by reference with pointers. — 11 — Study Notes • Handwritten Style NEB Grade XII
- NEB Class 12 | Computer Science | Unit 4 Programming in C 4.5 Working with Files 4.5.1 Concept of Data File A data file is a collection of data stored on secondary storage (hard disk, USB, etc.). Files allow programs to store data permanently so that it remains available even after the program ends. Text files – store data as readable characters (e.g. .txt) Binary files – store data in binary format (more compact) 4.5.2 Sequential and Random Files
- Sequential File Access Data is read/written in order from beginning to end To reach the n-th record you must pass through the first n-1 records Simple to implement, suitable for continuous processing
- Random (Direct) File Access Any record can be accessed directly using its position/address Uses functions like fseek(), ftell(), rewind() Faster for large files when only specific records are needed 4.5.3 File Manipulation Functions Function Purpose fopen() Open a file fclose() Close a file putc() / fputc() Write a character getc() / fgetc() Read a character putw() Write an integer getw() Read an integer fprintf() Write formatted data fscanf() Read formatted data fputs() / fgets() Write/Read a string fread() / fwrite() Binary read/write fseek() Move file pointer ftell() Current position rewind() Go to beginning — 12 — Study Notes
- Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C 4.5.4 Opening, Reading, Writing & Appending Opening a File – fopen() FILE *fp; fp = fopen("data.txt", "mode"); // Common modes: // "r" - read (file must exist) // "w" - write (creates new / overwrites) // "a" - append (adds at end) // "r+" - read + write // "w+" - write + read (overwrites) // "a+" - append + read // Add "b" for binary: "rb", "wb", "ab" Writing to a File #include <stdio.h> int main() { FILE *fp; fp = fopen("student.txt", "w"); if (fp == NULL) { printf("Error opening file!"); return 1; } fprintf(fp, "Name: Ram\nRoll: 101\nMarks: 85\n"); putc('A', fp); putw(100, fp); fclose(fp); printf("Data written successfully"); return 0; } Reading from a File #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("student.txt", "r"); if (fp == NULL) { printf("File not found!"); return 1; } while ((ch = fgetc(fp)) != EOF) putchar(ch); fclose(fp); return 0; } — 13 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C Appending Data to a File #include <stdio.h> int main() { FILE *fp; fp = fopen("student.txt", "a"); // append mode if (fp == NULL) { printf("Error!"); return 1; } fprintf(fp, "Name: Sita\nRoll: 102\nMarks: 92\n"); fclose(fp); printf("Data appended"); return 0; } Using fscanf and fprintf together #include <stdio.h> int main() { FILE *fp; int roll; char name[30]; float marks; // Write fp = fopen("marks.dat", "w"); fprintf(fp, "%d %s %.1f\n", 101, "Hari", 88.5); fclose(fp); // Read fp = fopen("marks.dat", "r"); fscanf(fp, "%d %s %f", &roll, name, &marks); printf("Roll: %d Name: %s Marks: %.1f", roll, name, marks); fclose(fp); return 0; } getw() and putw() Example — 14 — Study Notes • Handwritten Style NEB Grade XII
NEB Class 12 | Computer Science | Unit 4 Programming in C #include <stdio.h> int main() { FILE *fp; int nums[] = {10, 20, 30, 40, 50}; int i, n; fp = fopen("numbers.dat", "wb"); for (i = 0; i < 5; i++) putw(nums[i], fp); fclose(fp); fp = fopen("numbers.dat", "rb"); while ((n = getw(fp)) != EOF) printf("%d ", n); fclose(fp); return 0; } — 15 — Study Notes • Handwritten Style NEB Grade XII
- NEB Class 12 | Computer Science | Unit 4 Programming in C Quick Revision Checklist Functions Prototype Definition Call Return Library functions vs User-defined functions Call by value (copy) vs Call by reference (address) auto = local, temporary | extern = global, shared Recursion needs a base case (Factorial, Fibonacci) Structures & Unions struct groups different types; each member has own memory union shares memory; only one member active at a time Access with . (dot) or -> (arrow for pointers) Array of structures stores multiple records Pointers & address of variable * value at address (dereference) Pointer arithmetic: p++, p+1, *(p+i) Essential for call by reference and dynamic memory Files fopen() / fclose() – always check for NULL Modes: r, w, a, r+, w+, a+ (+b for binary) putc/getc, putw/getw, fprintf/fscanf Sequential = one after another | Random = direct access Practice Task: Write a C program that uses a structure for Student, stores 5 records in a file using fprintf, then reads and displays them. Add a function to search by roll number using call by reference. End of Unit 4 Notes – Keep Practising! NEB Class 12
- Computer Science
- Unit 4 — 16 — Study Notes
- Handwritten Style NEB Grade XII
Related chapters in Computer Science: Class 12 Web Technology - II (CSS, JavaScript, PHP) notes, Class 12 OOP Concept notes, Class 12 DBMS Concept notes.
Practice
Important Questions
What will be the output of the program code?
#include
#include
int main()
{
char str1[20] = "Hello";
char str2[] = "world";
strcat(str1, str2);
puts(str1);
return 0;
} Hello
World
Helloworld
WorldHello
What is the correct syntax to declare a structure in C?
struct { }
define struct { }
struct [ ]
struct
In C, which operator is used to get the address of a variable?
*
&
->
.
The statement int number (int a); is a ...
function call
function definition
function declaration
function execution
Write a C program to read account number, name and address of ten customers and display them using array and structure.
Write a C program to input a number and check whether it is positive, negative or zero using a user-defined function.
Write a C program to generate the Fibonacci series of the first two numbers, 7 and 14, up to 10 terms using a user-defined function.
10 more questions locked
Upgrade to a paid plan to view all important questions
Exam
Past Question Analysis
Historical exam-pattern data from past NEB question papers — not a prediction of future questions.
11
Question Items
18
Total Marks
5
Papers Appeared In
| Year | Question Items | Marks |
|---|---|---|
| 2083 | 2 | 9 |
| 2082 | 2 | — |
| 2081 | 3 | — |
| 2080-supp | 2 | — |
| 2079 | 2 | 9 |
Also appears in 1 model question (not counted above — model sets aren't actual NEB exam history).
Programming II develops practical programming skills using the C programming language. Students study functions, recursion, pointers, structures and file handling, along with different methods of passing arguments. The chapter emphasizes writing programs to solve problems and working with structured and stored data.
This page covers Programming II, chapter 4 of 7 in the Class 12 Computer Science syllabus set by the National Examination Board (NEB). 14 important questions for this chapter are available, each with a full solution.
For numerical and derivation-based chapters like this one, working through past NEB questions is usually more useful than re-reading notes alone — try solving each important question above before checking the solution, then compare your working step by step.