Crash Preparation Bootcamp
Crash Preparation Bootcamp
(based on Annexure-B6) designed for a crash preparation bootcamp! This will be structured as
a series of "lectures" covering the key topics, focusing on essential concepts and syntax for
quick review.
Introduction:
Welcome to the Computer Science XII Crash Prep Bootcamp! This cheat sheet tutorial is
designed to rapidly review the core concepts of C programming as outlined in your syllabus
(Annexure-B6). We'll break down the topics lecture-wise to make it digestible and
exam-focused. Let's get started!
● What is C?
○ A powerful, general-purpose programming language.
○ Known for its efficiency, flexibility, and portability.
○ Forms the foundation for many other languages.
● History (Key Milestones - Very Brief)
○ Developed at Bell Labs in the early 1970s by Dennis Ritchie.
○ Evolved from languages like B and BCPL.
○ Standardized by ANSI (ANSI C or C89) and later ISO (C99, C11, etc.).
● Why C is Important:
○ System programming (operating systems, embedded systems).
○ Application development.
○ Foundation for understanding computer architecture and programming principles.
Anatomy of a C Program:
// Preprocessor Directives (Lecture 2.1)
#include <stdio.h> // Standard input/output library
Declaration: Must be declared before use, specifying data type and name.
int age; // Declare an integer variable named 'age'
float price; // Declare a float variable named 'price'
char initial; // Declare a character variable named 'initial'
○
○
●
Size Modifiers:
○ short: Reduces the size of int (e.g., short int).
○ long: Increases the size of int or double (e.g., long int, long double).
○ signed: Allows both positive and negative values (default for int and char).
○ unsigned: Allows only non-negative values (e.g., unsigned int).
● Categories of Operators:
○ Lecture 5.1: Arithmetic Operators:
■ + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulo -
remainder of integer division)
Example:
int sum = 10 + 5;
int diff = 10 - 5;
int prod = 10 * 5;
float div = 10.0 / 5.0;
int rem = 10 % 3; // rem will be 1
■
Example:
int a = 10, b = 5;
int isEqual = (a == b); // isEqual will be 0 (false)
int isGreater = (a > b); // isGreater will be 1 (true)
■
Example:
int x = 5, y = 2;
int resultAND = (x > 0) && (y < 5); // true AND true = true (1)
int resultOR = (x < 0) || (y > 1); // false OR true = true (1)
int resultNOT = !(x == 0); // NOT false = true (1)
■
Example:
int num = 10; // Simple assignment
num += 5; // num = num + 5; (num becomes 15)
num -= 2; // num = num - 2; (num becomes 13)
■
Example:
int count = 5;
int prefixInc = ++count; // count is now 6, prefixInc is 6
int postfixInc = count++; // postfixInc is 6, count is now 7
■
Example:
int age = 20;
char *status = (age >= 18) ? "Adult" : "Minor"; // status will be "Adult"
■
○ Lecture 5.7: Bitwise Operators (Less Common in Basic XII Syllabus, but
mentioned for completeness):
■ & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), <<
(Left shift), >> (Right shift)
■ Operate on individual bits of integers.
○ Lecture 5.8: Special Operators:
■ sizeof(): Returns the size (in bytes) of a variable or data type.
■ & (Address-of operator): Returns the memory address of a variable (used
with pointers).
■ * (Dereference operator): Accesses the value at a memory address (used
with pointers).
■ , (Comma operator): Evaluates expressions from left to right, returns the
value of the rightmost expression.
■ . (Member access operator): Accesses members of structures and
unions.
■ -> (Pointer member access operator): Accesses members of structures
and unions through pointers.
○
if-else statement: Executes one block of code if a condition is true, and another block if false.
if (condition) {
// Code if true
} else {
// Code if false
}
○
○
switch-case statement: Efficiently handles multiple choices based on the value of a variable
(typically integer or char).
switch (expression) {
case value1:
// Code for value1
break; // Important: Exit switch after case
case value2:
// Code for value2
break;
// ... more cases
default: // Optional default case
// Code if expression doesn't match any case
break;
}
○
for loop: Used when you know the number of iterations in advance or need precise control over
initialization, condition, and increment/decrement.
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 something in the loop to eventually make condition false!
}
○
■
○
do-while loop: Similar to while, but 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 at the end
} while (condition);
○
■
○
● Lecture 6.3: Unconditional Control Statements
Lecture 7: Functions
Function Declaration (Prototype): Tells the compiler about the function's name, return type,
and parameters before it's used. (Usually placed before main() or in a header file).
return_type function_name(parameter_list); // Declaration ends with semicolon ;
○
1. return_type: Data type of the value the function returns (e.g., int, float,
void if it returns nothing).
2. function_name: Identifier for the function.
3. parameter_list: Comma-separated list of parameter declarations (data
type and parameter name). Can be empty () if no parameters.
○
Function Calling: To execute a function, you need to "call" it from another part of the program
(e.g., from main() or another function).
function_name(argument_list); // Calling the function
○
○
○
○
○
○
○
String Initialization:
char greeting[] = "Hello"; // Compiler automatically adds null terminator
char name[20] = {'J', 'o', 'h', 'n', '\0'}; // Explicit null termination
○
○ Input/Output of Strings:
■ scanf("%s", string_variable); - Reads a string until whitespace (not safe
for buffer overflow if input is longer than array size).
■ gets(string_variable); - Reads a whole line of input (until newline
character). Deprecated and unsafe due to buffer overflow risks. Avoid
using gets().
■ fgets(string_variable, size, stdin); - Safer alternative to gets(). Reads up
to size-1 characters or until newline.
■ printf("%s", string_variable); - Prints a string.
■ puts(string_variable); - Prints a string followed by a newline.
○ String Functions (from string.h header file):
■ #include <string.h> (Must include this header file)
■ strlen(string): Returns the length of a string (excluding null terminator).
■ strcpy(destination_string, source_string): Copies source_string to
destination_string. Make sure destination_string is large enough!
■ strcmp(string1, string2): Compares string1 and string2. Returns 0 if equal,
negative if string1 < string2, positive if string1 > string2.
■ strcat(destination_string, string_to_append): Appends string_to_append
to the end of destination_string. Again, ensure destination_string is
large enough!
Lecture 9: Pointers
●
○ data_type: Data type of the variable the pointer will point to.
○ *: Asterisk indicates it's a pointer variable.
○ pointer_name: Identifier for the pointer.
● Lecture 9.3: Pointer Initialization:
Initialization with Address-of Operator (&): Get the address of an existing variable.
int num = 25;
int *ptr = # // ptr now stores the memory address of num
○
Initialization to NULL: Indicate that the pointer doesn't point to any valid memory location
initially.
int *ptr = NULL; // ptr is a null pointer
○
Used to access the value stored at the memory address pointed to by a pointer.
int value = *ptr; // value will now be 25 (the value of num)
*ptr = 30; // Modifies the value of num through the pointer (num is now 30)
○
int main() {
int number = 100;
int *ptrNumber; // Declare a pointer to an integer
printf("Value of number after changing through pointer: %d\n", number); // Output: 200
return 0;
}
●
Conclusion:
This cheat sheet tutorial provides a rapid overview of the essential C programming concepts for
your Computer Science XII exam. Remember to practice coding examples and review past
papers. Focus on understanding the syntax and purpose of each topic. Good luck with your
bootcamp and exams!
Key Takeaways for Exam Preparation:
● Syntax is crucial: Pay close attention to the correct syntax for declarations, operators,
control structures, functions, arrays, and pointers.
● Understand the purpose of each concept: Don't just memorize syntax; understand
why and when to use each feature.
● Practice, practice, practice: Write small programs to solidify your understanding of
each lecture's topics.
● Review past papers: Get familiar with the types of questions asked in exams.
This cheat sheet should be a valuable tool for your crash preparation. All the best!