Download .CPP Sample Files

Jump to:     File Information     Download     How to Open

What is a CPP File?

A CPP file is a plain-text source code file written in C++, the language Bjarne Stroustrup created as an extension of C. C++ adds classes, templates and a large standard library. CPP files usually hold the program logic, while declarations go into header files (.H or .HPP).

Like C code, C++ has to be compiled before it can run: a compiler such as GCC (g++), Clang or Microsoft Visual C++ turns CPP files into an executable program. C++ is used for performance-critical software, including Google Chrome, Mozilla Firefox, MySQL and Adobe Photoshop.

Download CPP Sample Files

The three samples below are small, complete console programs. Each one compiles on its own with any modern C++ compiler and reads a number from standard input, which makes them handy for testing compilers, build pipelines, code editors and online judges.

[226 bytes]
[343 bytes]
[575 bytes]

CPP Sample File Details

SampleWhat it doesLinesSizeExample input → output
Sample1.CPPChecks whether an integer is even or odd16226 bytes7 → “7 is odd.”
Sample2.CPPReverses the digits of an integer19343 bytes12345 → “Reversed Number = 54321”
Sample3.CPPChecks whether a number is prime, using a separate function36575 bytes29 → “29 is a prime number.”

All three files are ASCII text with Windows (CRLF) line endings and only use <iostream>. They compile without warnings with g++ -std=c++17 -Wall -Wextra (tested with GCC 11).

What You Can Test With These CPP Files

  • Compilers and builds: GCC, Clang, MSVC, CMake projects and CI jobs.
  • Editors and IDEs: syntax highlighting, formatting and language-server features.
  • Online judges and code runners: programs that read their input from stdin.
  • Uploads and previews: features that should treat .cpp as text (MIME type text/x-c++src).
  • Line endings: the files use CRLF, a common source of problems in Git and Linux tools.

How to Compile and Run a CPP File

# Linux or macOS (GCC or Clang)
g++ -std=c++17 -Wall sample-1.cpp -o sample-1
./sample-1

# Windows (Developer Command Prompt for Visual Studio)
cl /EHsc sample-1.cpp
sample-1.exe

To run a sample without typing, send the input through a pipe, for example echo 12345 | ./sample-2.

CPP vs Related Source Files

ExtensionLanguageHow it runs
.CPPC++Compiled to a native program
.CCCompiled to a native program
.HC and C++ headersIncluded by .C and .CPP files
.CSC#Compiled for the .NET runtime
.JAVAJavaCompiled to JVM bytecode
.PYPythonRun by the Python interpreter