Crash Prepatory CS XII
Crash Prepatory CS XII
Here's a crash course preparation material for Computer Science XII, based on the
reduced syllabus you provided. This material is designed to be concise and cover the most
important points for exam preparation.
Basic Structure:
#include <stdio.h> // Preprocessor directive (e.g., for input/output)
● Source Code:
○ C program files have a .c extension (e.g., myprogram.c).
○ Compilation Process: Source code (.c) is compiled into machine code
(executable).
6. Control Structures
● Selection (Conditional) Control Structures:
if-else statement: Executes one block of code if the condition is true, and another block if false.
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
for loop: Used when you know in advance how many times you need to repeat a block of code.
for (initialization; condition; increment/decrement) {
// Code to be repeated
}
while loop: Repeats a block of code as long as a condition is true. Condition is checked before
each iteration.
while (condition) {
// Code to be repeated as long as condition is true
// Make sure to update variables in the condition to avoid infinite loop
}
do-while loop: Similar to while loop, but the condition is checked after each iteration.
Guarantees at least one execution of the loop body.
do {
// Code to be repeated at least once
// Condition is checked after the first execution and then after each iteration
} while (condition);
○ return statement: Exits the current function and returns control to the calling
function. Can also return a value from a function.
○ break statement:
■ Used to exit a loop ( for, while, do-while) prematurely.
■ Used to exit a switch statement after a case is matched.
○ continue statement: Skips the rest of the current iteration of a loop and
proceeds to the next iteration.
7. Functions
● System-defined/Predefined Functions (Library Functions):
1. Functions provided by the C standard library.
2. Perform common tasks (input/output, math operations, string manipulation, etc.).
3. Examples: printf() (formatted output), scanf() (formatted input), sqrt() (square
root), pow() (power), strlen() (string length).
4. Header Files: Function declarations for library functions are found in header files
(e.g., stdio.h, math.h, string.h). You must #include the appropriate header file to
use library functions.
● User-defined Functions:
1. Functions created by the programmer to perform specific tasks in their program.
2. Modular Programming: Functions promote code reusability, organization, and
readability.
3. Function Components:
Function Declaration (Prototype): Tells the compiler about the function's return type, name,
and parameters before it is used. Usually placed at the beginning of the program or in header
files.
return_type function_name(parameter_list); // e.g., int add(int num1, int num2);
Function Definition: Contains the actual code (statements) that the function executes.
return_type function_name(parameter_list) {
// Function body - code to perform the task
// ...
return return_value; // If return_type is not void
}
No Arguments, No Return Value: Function performs a task but doesn't receive input or send
back a result.
void print_message() {
printf("Hello!\n");
}
No Arguments, Return Value: Function performs a task and returns a value, but doesn't
receive input arguments.
int get_random_number() {
return rand(); // Example (requires srand() for proper use)
}
Arguments, No Return Value: Function receives input arguments to perform a task but doesn't
return a value.
void print_sum(int a, int b) {
printf("Sum: %d\n", a + b);
}
Arguments, Return Value: Function receives input arguments, performs a task, and returns a
result.
int add(int num1, int num2) {
return num1 + num2;
}
8. Arrays and Strings
● Arrays:
○ Definition: **Collection of elements of the same data type stored in contiguous
memory locations.
Declaration: Specifies the array name, data type, and size (number of elements).
data_type array_name[size]; // e.g., int numbers[5];
Element-wise:
int numbers[5];
numbers[0] = 10;
numbers[1] = 20;
// ... and so on
Accessing Elements: Using index (subscript) - 0-based indexing (first element is at index 0).
int first_element = numbers[0]; // Accessing the first element
Initialization:
int matrix[2][2] = { {1, 2}, {3, 4} };
9. Pointers
● Definition: A pointer is a variable that stores the memory address of another
variable.
● Purpose:
○ Direct Memory Access: Pointers allow you to directly manipulate memory
locations.
○ Dynamic Memory Allocation: Used for allocating memory during program
execution (not covered in this syllabus, but important concept related to pointers).
○ Function Arguments (Pass by Reference): Pointers enable functions to modify
the original variables passed as arguments.
○ Data Structures: Essential for building complex data structures like linked lists,
trees, etc.
Declaration:
data_type *pointer_name; // e.g., int *ptr; - ptr is a pointer to an integer
○ data_type: Specifies the data type of the variable that the pointer will point to.
○ * (asterisk): Indicates that pointer_name is a pointer variable.
● Initialization:
Assigning the address of a variable to a pointer using the address-of operator &:
int num = 25;
int *ptr;
ptr = # // ptr now stores the address of num
Accessing the value stored at the memory address pointed to by a pointer using the
dereference operator *:
int value = *ptr; // value now holds the value of num (which is 25)
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
return 0;
}