All C library headers with .h remove the .h and prepend c:
C | C++ |
---|---|
#include <stdio.h> |
#include <cstdio> |
#include <stdlib.h> |
#include <cstdlib> |
#include <string.h> |
#include <cstring> |
#include <ctype.h> |
#include <cctype> |
#include <stddef.h> |
#include <cstddef> |
Namespaces in C++ provide a mechanism to group identifiers (like classes, objects, and functions) under a name. This helps avoid name conflicts in larger projects.
namespace MyNamespace {
int x, y;
};
To access values within a namespace, use the scope operator ::
.
MyNamespace::x = 3; // Assigns 3 to x in MyNamespace
To avoid repeatedly specifying a namespace, you can use the using namespace
directive:
using namespace MyNamespace;
Note: The std
namespace is commonly used for the C++ Standard Library.
#include <string.h>
#include <string>
In C, string length is obtained using strlen
:
char *AString; // Points to a C string
strlen(AString); // Returns length of string
In C++, you use the length()
method:
std::string AString;
AString.length(); // Returns length of string
In C, copying strings involves manual memory allocation and using strcpy
:
char Original[] = "Some string";
char *Copy;
// Allocate space equal to string length plus null terminator
Copy = malloc(strlen(Original) + 1);
strcpy(Copy, Original);
In C++, string copying is simpler:
std::string Original = "Some string";
std::string Copy = Original;
In C, use strcmp
to compare strings:
char *A, *B;
if (strcmp(A, B) == 0) {
// strcmp returns 0 if A == B
} else if (strcmp(A, B) < 0) {
// strcmp returns < 0 if A < B
}
In C++, you can use the ==
and <
operators:
std::string A, B;
if (A == B) {
// do something if A == B
} else if (A < B) {
// do something if A < B
}
In C, string concatenation requires manual allocation and use of strcat
:
char *A, *B, *C;
// Allocate space for concatenated string
C = malloc(strlen(A) + strlen(B) + 1);
strcpy(C, A);
strcat(C, B);
In C++, use the +
operator:
std::string A, B, C;
C = A + B;
#include <stdio.h>
#include <iostream>
In C, output is handled with printf
:
int I = 3;
double D = 2.2;
char S[] = "Hello";
printf("I = %d\n", I);
printf("D = %lf\n", D);
printf("S = %s\n", S);
In C++, use std::cout
:
int I = 3;
double D = 2.2;
std::string S = "Hello";
std::cout << "I = " << I << std::endl;
std::cout << "D = " << D << std::endl;
std::cout << "S = " << S << std::endl;
In C, input is often handled with scanf
and fgets
:
int I;
double D;
char S[128]; // Can only accept strings of 127 chars
scanf("%d%lf", &I, &D);
fgets(S, sizeof(S), stdin); // Safer than scanf
In C++, use std::cin
:
int I;
double D;
std::string S;
std::cin >> I >> D >> S;
In C, file output is done using fopen
and fprintf
:
FILE *OutFile = fopen("out.txt", "w");
fprintf(OutFile, "This is a file!\n");
fclose(OutFile);
In C++, use std::ofstream
:
std::ofstream OutFile("out.txt");
OutFile << "This is a file!" << std::endl;
In C, use fopen
and fscanf
:
FILE *InFile = fopen("in.txt", "r");
int I;
double D;
fscanf(InFile, "%d%lf", &I, &D);
In C++, use std::ifstream
:
std::ifstream InFile("in.txt");
int I;
double D;
InFile >> I >> D;
In C, use fseek
and ftell
:
FILE *InFile = fopen("in.txt", "r");
int FileLength;
fseek(InFile, 0, SEEK_END);
FileLength = ftell(InFile);
fseek(InFile, 0, SEEK_SET);
In C++, use seekg
and tellg
:
std::ifstream InFile("in.txt");
InFile.seekg(0, InFile.end);
int FileLength = InFile.tellg();
InFile.seekg(0, InFile.beg);
Write your solutions to these problems on this class whiteboard.
printf
to C++ using std::cout
:int num = 10;
double pi = 3.14;
printf("Number: %d, Pi: %lf\n", num, pi);