C and C++ Programming Cheat Sheet
C and C++ Programming Cheat Sheet
Description: A quick reference for C and C++ covering basics to advanced topics like
functions, pointers, OOP, and STL.
1. Basics
Data Types & Variables
Control Structures
if (x > 0) {
cout << "Positive";
} else {
cout << "Negative";
}
Loops (C/C++)
Input/Output
C Example
#include <stdio.h>
int x;
printf("Enter a number: ");
scanf("%d", &x);
C++ Example
#include <iostream>
int x;
cin >> x;
cout << "Value: " << x;
2. Intermediate
Functions
C Function
C++ Function
Arrays
Pointers
int x = 10;
int *ptr = &x; // Pointer to x
printf("%d", *ptr); // Dereference pointer
FILE *fptr;
fptr = fopen("file.txt", "r");
if (fptr == NULL) {
printf("Error opening file!");
}
3. Advanced
class Car {
public:
string brand;
int year;
};
Car myCar;
myCar.brand = "Tesla";
Inheritance (C++)
class Vehicle {
public:
int speed;
};
class Car : public Vehicle {
public:
string brand;
};
C++ Example:
try {
int x = 10 / 0;
} catch (...) {
cout << "Exception caught!";
}
Vector Example
#include <vector>
vector<int> myVec = {1, 2, 3};