CS501 · Reference Guide
A fully customizable, printer-friendly reference covering types, loops, arrays, structures, pointers, file handling, and vectors.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}| Type | Bytes | Description |
|---|---|---|
| bool | 1 | true or false (0 or 1) |
| char | 1 | Single ASCII character ('a') |
| int | 4 | Integers (-2B to 2B) |
| float | 4 | Single-precision float (7 decimals) |
| double | 8 | Double-precision float (15 decimals) |
+ - * / %< <= > >= == !=&& || !i++ ++i i-- --i+= -= *= /= %=if (score >= 90) {
cout << "A";
} else if (score >= 60) {
cout << "Pass";
} else {
cout << "Fail";
}switch (op) {
case '+': cout << a + b; break;
case '-': cout << a - b; break;
default: cout << "Error";
}// For loop
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
// While loop
int i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
// Do-while loop
int i = 0;
do {
cout << i << " ";
i++;
} while (i < 5);// Prototype/Declaration
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b;
}
// Call
int sum = add(5, 3);// 1D Array
int marks[5] = {90, 85, 77, 92, 80};
int first = marks[0];
// 2D Array
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int val = matrix[0][1]; // 2// Char Array (C-string) char msg[6] = "Hello"; // includes \0 // std::string (C++) #include <string> string name = "Ali"; name += " Ahmed"; // concatenation
struct Point {
int x;
int y;
};
Point p1 = {10, 20};
p1.x = 15; // dot access
Point* ptr = &p1;
ptr->y = 25; // pointer accessint num = 10; int* ptr = # // Pointer (stores address of num) int& ref = num; // Reference (alias for num) *ptr = 20; // Changes num to 20 ref = 30; // Changes num to 30
// Recursive function
int factorial(int n) {
if (n <= 1) {
return 1; // Base case
}
return n * factorial(n - 1); // Recursive call
}#include <fstream>
using namespace std;
// Writing to file
ofstream out("data.txt");
out << "Line 1" << endl;
out.close();
// Reading from file
ifstream in("data.txt");
string line;
if (in.is_open()) {
while (getline(in, line)) {
cout << line << endl;
}
in.close();
}#include <vector>
using namespace std;
vector<int> vec = {10, 20, 30};
vec.push_back(40); // Adds 40 to the end
int len = vec.size(); // len is 4
// Traversal
for (int x : vec) {
cout << x << " ";
}#include <cmath> // pow(x,y), sqrt(x), abs(x) #include <iomanip> // setw(w), setprecision(p) #include <algorithm> // sort(), max(), min(), swap() cout << fixed << setprecision(2) << 3.14159; // prints 3.14 sort(arr, arr + 5); // Sorts array ascending
| Seq | Description |
|---|---|
| \n | Newline (line break) |
| \t | Tab (indentation spacing) |
| \\ | Backslash character |
| \' | Single quote character |
| \" | Double quote character |
| \0 | Null character (ends C-strings) |