File extensions tell both the computer and the programmer what kind of file we're working with. For C++ programs, we typically use .cpp
, but there are several related extensions you might encounter:
.cpp
- Main C++ source files (most common).cc
- Alternative C++ source extension (common in Linux).h
or .hpp
- Header files (we'll learn about these later).c
- C language source files (C++ is based on C)The main difference is how your computer and IDE treat these files:
// main.cpp - will be compiled as C++
#include <iostream>
using namespace std;
int main() {
cout << "This is a C++ program!";
}
If we saved the exact same code as main.txt
:
The extension tells your development tools:
For example, saving a C++ file as .c
might cause issues:
// main.c - compiler will treat this as C code
// C doesn't have iostream!
#include <iostream>
int main() {
// cout doesn't exist in C
std::cout << "This won't compile as C!";
}
Some IDEs create additional files with their own extensions:
.sln
- Visual Studio solution files.vcxproj
- Visual Studio project files.o
or .obj
- Compiled object files.exe
- Windows executable (the final program)For now, just remember to save your C++ code files with the .cpp
extension - this ensures your development environment will treat them properly as C++ source code.
Answers to questions are automatically generated and may not have been reviewed.
Getting our computer set up so we can create and build C++ programs. Then, creating our very first application