Codectionary / Developer documentation / C++

C versus C++

Compare C and C++ through the same small program, learn where their rules differ, and see when each language fits a project.

Related languages, separate standards

C++ began as “C with Classes” and retains familiar C-like syntax: functions, braces, loops, pointers and many operators. Both languages can produce native programs and work with low-level interfaces. They are maintained as separate languages with separate standards and evolving libraries. C++ is not simply C plus a few features, and newer versions of C can add features that C++ does not adopt in the same form.

The shared history is useful when reading code or calling a C library from C++, but it does not make every C source file a valid C++ file. Compile a project in its intended language mode and check the interface when crossing a C/C++ boundary.

One task written in both languages

Both programs add three scores and print Total: 16. In C, a fixed array holds the scores and the loop uses the known count 3. printf uses a format string: %d is replaced with the integer total. In C++, std::vector owns a resizable sequence and the range-based for loop visits each stored score without spelling its length. std::cout sends the label, value and newline to the output stream.

These are examples of two reasonable tools, not a claim that a fixed C array is always wrong or a vector always fastest. The C++ example uses C++11-or-later features and is written for C++17. The C example is valid C11. The difference that matters for a beginner is who tracks the sequence length and storage, and which standard-library facilities are available.

C (C11): scores.c

#include <stdio.h>

int main(void) {
  int scores[] = {3, 5, 8};
  int total = 0;
  for (int i = 0; i < 3; ++i) {
    total += scores[i];
  }
  printf("Total: %d\n", total);
  return 0;
}

C++ (C++17): scores.cpp

#include <iostream>
#include <vector>

int main() {
  std::vector<int> scores{3, 5, 8};
  int total = 0;
  for (int score : scores) {
    total += score;
  }
  std::cout << "Total: " << total << '\n';
}

Features and libraries differ

C uses structs to group data and functions to operate on it. C++ can also do that, and adds classes with constructors, member functions and access control. Objects can establish an invariant when created and clean up owned resources when their lifetime ends. C++ templates express reusable operations for types; they are not the same thing as C preprocessor macros.

Both languages have standard libraries, but not identical ones. C provides facilities such as stdio.h and stdlib.h. C++ includes C library facilities and adds containers such as std::vector, strings, algorithms and streams. C++ code can still call appropriate C APIs. For normal C++ application code, prefer standard containers and clear ownership over routine manual allocation.

TopicCC++
Program structureFunctions and structs are common building blocks.Functions, classes, templates and other styles can be combined.
Reusable codeFunctions, separate translation units and macros.Functions plus templates and library abstractions.
Common sequenceArray or explicitly managed storage with a separate size.std::vector manages its elements and knows its size.
Resource cleanupOften explicit functions such as free or fclose.RAII objects can release owned resources at scope exit.
Standard libraryC library facilities such as stdio.h.C facilities plus containers, algorithms, streams and more.

Why valid C may fail as C++

The two languages have different type and keyword rules. The short C example below assigns a void* returned by malloc to int*. That implicit conversion is accepted by C but rejected by standard C++ because C++ requires an explicit conversion from void* to a typed object pointer. This is a compatibility demonstration, not a memory-allocation pattern to copy into a C++ project. It also shows why renaming a .c file to .cpp is not a safe migration plan.

In idiomatic C++, a small sequence of integers is better represented by std::vector<int>, which handles storage and cleanup. Other C/C++ differences include names that became C++ keywords and some declarations with different meanings. C and C++ code can interoperate through carefully declared interfaces, but building and linking mixed-language projects needs toolchain-specific attention.

Valid C illustration; rejected by standard C++

#include <stdlib.h>

int main(void) {
  int *number = malloc(sizeof *number);
  if (number == NULL) return 1;
  *number = 7;
  free(number);
  return 0;
}

Building C and C++ is similar, but the language mode matters

Both languages are commonly preprocessed, compiled into object files, then linked into executables. A .c file is normally treated as C source and a .cpp file as C++ source by GCC and Clang. A build command can override the language mode. The C++ compiler driver also arranges its C++ library and runtime support at link time; invoking the C compiler driver is not necessarily interchangeable when linking C++ objects.

With GCC, gcc -std=c11 scores.c -o scores-c builds the C example and g++ -std=c++17 scores.cpp -o scores-cpp builds the C++ example on a POSIX-like toolchain. Windows MSVC uses different compiler options and executable names. Platform, compiler and project configuration can change the commands, object-file suffixes and libraries involved.

GCC example commands

gcc -std=c11 scores.c -o scores-c
g++ -std=c++17 scores.cpp -o scores-cpp
./scores-c
./scores-cpp

When might you choose C or C++?

Choose the language that fits the project’s platform, libraries, constraints and team. C is common for some operating-system interfaces, small embedded environments and codebases that intentionally use a C toolchain or stable C-facing API. C++ can be useful when a project benefits from standard containers, templates, automatic resource management and existing C++ frameworks. A constrained device may support only part of the C++ library; another team may already have a substantial C++ application.

Neither language guarantees speed or safety by its name alone. Compare the actual algorithms, required features and deployment environment. If you are starting a C++ project, learn its own idioms—std::vector, clear lifetimes and RAII—instead of treating C++ as a place to paste C code with a different filename.

Syntax

#include <iostream>
#include <vector>

int main() {
  std::vector<int> scores{3, 5, 8};
  int total = 0;
  for (int score : scores) {
    total += score;
  }
  std::cout << "Total: " << total << '\n';
}

Examples

Add three scores with a C++ standard container

The C++ half of the comparison uses a vector and a range-based loop to print Total: 16.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> scores{3, 5, 8};
  int total = 0;
  for (int score : scores) {
    total += score;
  }
  std::cout << "Total: " << total << '\n';
}
  1. The goal is to sum the same values as the C example above while showing common modern C++ library and loop features.
  2. #include <vector> makes std::vector available. The vector owns three integers; the range-based for statement visits each value without a hard-coded array bound.
  3. Each score is added to total. std::cout writes Total: 16 followed by a newline. The vector manages its own storage as it is destroyed at the end of main.
  4. This pattern is useful when a collection may grow or when you want its size and lifetime managed together. The side-by-side C program remains a valid choice in its own context.

Best practices

  • Check the language mode and standard of an existing project before reusing C or C++ source.
  • Prefer idiomatic C++ containers and automatic resource management in new C++ code.
  • Choose C or C++ for the project’s real constraints, APIs and team rather than a blanket speed claim.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading