0% found this document useful (0 votes)
3 views

C and C++ Programming Cheat Sheet

This cheat sheet provides a quick reference for C and C++ programming, covering basics such as data types, control structures, and input/output, as well as intermediate topics like functions, arrays, and pointers. It also includes advanced concepts like object-oriented programming, dynamic memory allocation, and exception handling. Additionally, it offers tips and warnings for best practices in both languages.

Uploaded by

hacker.88
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C and C++ Programming Cheat Sheet

This cheat sheet provides a quick reference for C and C++ programming, covering basics such as data types, control structures, and input/output, as well as intermediate topics like functions, arrays, and pointers. It also includes advanced concepts like object-oriented programming, dynamic memory allocation, and exception handling. Additionally, it offers tips and warnings for best practices in both languages.

Uploaded by

hacker.88
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

Data Type Size (C/C++) Example

int 4 bytes int x = 10;

float 4 bytes float y = 3.14;

char 1 byte char c = 'A';

bool (C++) 1 byte bool flag = true;

Control Structures

If-Else Statement (C/C++)

if (x > 0) {
cout << "Positive";
} else {
cout << "Negative";
}

Loops (C/C++)

for (int i = 0; i < 5; i++) {


cout << i << " ";
}

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

int add(int a, int b) {


return a + b;
}

C++ Function

int multiply(int x, int y) {


return x * y;
}

Arrays and Pointers

Arrays

int arr[5] = {1, 2, 3, 4, 5};

Pointers

int x = 10;
int *ptr = &x; // Pointer to x
printf("%d", *ptr); // Dereference pointer

File Handling (C)

FILE *fptr;
fptr = fopen("file.txt", "r");
if (fptr == NULL) {
printf("Error opening file!");
}

3. Advanced

Object-Oriented Programming (C++)

Class and Object

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;
};

Dynamic Memory Allocation


C Example:

int *ptr = (int*)malloc(sizeof(int) * 5);

C++ Example:

int *ptr = new int[5];

Exception Handling (C++)

try {
int x = 10 / 0;
} catch (...) {
cout << "Exception caught!";
}

STL (Standard Template Library)

Vector Example

#include <vector>
vector<int> myVec = {1, 2, 3};

4. Tips and Warnings


Avoid gets() in C: Use fgets() instead to prevent buffer overflow.
Use std::vector in C++ for dynamic arrays.
Always free allocated memory using free() (C) or delete (C++).

You might also like