Sintaxis basica
Initial project setup in C++
Section titled “Initial project setup in C++”In C++ you usually work with files and a project folder structure. To start (and to grow without chaos), use one of these two setups:
Option 1: single file (for practice)
Section titled “Option 1: single file (for practice)”- Create a folder, e.g.
my-project/ - Inside it, create
main.cpp
Structure:
my-project/ main.cppOption 2: typical structure (recommended)
Section titled “Option 2: typical structure (recommended)”Structure:
my-project/ CMakeLists.txt src/ main.cpp greeting.cpp include/ greeting.hsrc/: source code (.cpp)include/: headers (.h) with declarations (what a function/class “promises”)CMakeLists.txt: build configuration file for CMake
Minimal program (and what each part means)
Section titled “Minimal program (and what each part means)”#include <iostream>
int main() { std::cout << "Hola C++" << std::endl; return 0;}Line-by-line explanation
Section titled “Line-by-line explanation”#include <iostream>: imports the standard input/output library so you can usestd::cout.<>means it’s a standard library header.
int main(): program entry point. Most console programs start here.intmeans the function returns an integer to the operating system.
{ ... }: braces define the block of code that belongs tomain.std::cout: output to the console (terminal).<<: “insertion” operator used to send data intocout.std::endl: newline + flush. You can also use"\n"in many cases.;(semicolon): ends a statement.return 0;: means “finished successfully”. (Modern C++ compilers often assume0if omitted inmain, but we keep it explicit here.)
Basic syntax rules (common beginner pitfalls)
Section titled “Basic syntax rules (common beginner pitfalls)”- Case matters:
Mainis not the same asmain. - Most statements end with
;(except constructs likeif (...) {}). - Braces control structure: they define what belongs to an
if,for,while, functions, classes, etc. - Indentation: doesn’t change meaning, but it makes code readable. Use 2 or 4 spaces consistently.
Comments (to explain your code)
Section titled “Comments (to explain your code)”// Single-line comment
/* Multi-line comment*/Variables and types (quick base)
Section titled “Variables and types (quick base)”int age = 20; // integerdouble pi = 3.14159; // floating pointchar letter = 'A'; // one characterbool active = true; // true/falseConditionals (if / else)
Section titled “Conditionals (if / else)”int grade = 15;
if (grade >= 10) { std::cout << "Passed\n";} else { std::cout << "Failed\n";}Loops (for / while)
Section titled “Loops (for / while)”for (int i = 0; i < 5; i++) { std::cout << i << "\n";}
int j = 0;while (j < 5) { std::cout << j << "\n"; j++;}Build your first project (with CMake)
Section titled “Build your first project (with CMake)”If you’re using the recommended structure, create a minimal CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.16)project(my_project LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(my_project src/main.cpp)Then, in a terminal inside your project folder:
cmake -S . -B buildcmake --build buildThis will generate the executable inside build/ (exact name/location depends on your OS).
Agrega aqui operadores, condicionales, ciclos y ejemplos de clase.