C++ is a powerful, general-purpose programming language that supports multiple programming paradigms. This guide covers the fundamental syntax and structure you need to get started.
#include<iostream>// Include input/output stream libraryusingnamespacestd;// Use standard namespaceintmain() { // Main function - entry point
cout << "Hello, World!" << endl; // Output statementreturn0; // Return success status
}#include- Preprocessor directive to include librariesusing namespace std- Makes standard library accessibleint main()- Main function, program entry pointcout- Standard output stream<<- Stream insertion operatorendl- End line and flush bufferreturn 0- Exit program successfully
Every statement must end with a semicolon ;
int x = 5; // Correct
cout << "Hello"; // Correctint y = 10// ERROR: Missing semicolonUsed to define code blocks and scope
if (condition) {
// Code block
statement1;
statement2;
}// Single line comment/*Multi-line commentCan span multiple lines*//// Documentation comment (for tools like Doxygen)- Use descriptive names
- Start with letter or underscore
- Case sensitive
- Cannot use reserved keywords
// Good namingint userAge = 25;
string firstName = "John";
bool isActive = true;
// Avoidint a = 25; // Too short
string fn = "John"; // Abbreviationbool flag = true; // UnclearconstintMAX_SIZE = 100; // Runtime constantconstexprintARRAY_SIZE = 50; // Compile-time constant
#definePI3.14159// Preprocessor macro (avoid in modern C++)int number = 42; // Usually 4 bytesshort small = 32767; // 2 byteslong big = 2147483647L; // 4+ byteslonglong huge = 9223372036854775807LL; // 8 bytesfloat pi = 3.14f; // 4 bytes, 6-7 digits precisiondouble pi_double = 3.14159265359; // 8 bytes, 15-17 digits precisionlongdouble pi_long = 3.14159265359L; // 10+ byteschar letter = 'A'; // 1 bytewchar_t wide = L'Ω'; // Wide characterchar16_t utf16 = u'字'; // UTF-16 characterchar32_t utf32 = U'🌍'; // UTF-32 characterbool isTrue = true; // true (1) or false (0)bool isFalse = false;// Declarationint x;
int y, z;
// Initializationint a = 10; // Copy initializationintb(20); // Direct initializationint c{30}; // Brace initialization (C++11)int d = {40}; // Copy brace initialization// Multiple declarationint e = 50, f = 60, g = 70;auto number = 42; // intauto text = "Hello"; // const char*auto flag = true; // boolauto pi = 3.14159; // double// With explicit typeauto value = int{100}; // int#include<iostream>usingnamespacestd;intmain() {
cout << "Text output" << endl;
cout << "Number: " << 42 << endl;
cout << "Multiple " << "values " << "in " << "one " << "line" << endl;
return0;
}#include<iostream>usingnamespacestd;intmain() {
int age;
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello " << name << ", you are " << age << " years old!" << endl;
return0;
}// Badint x;
cout << x << endl; // Undefined behavior!// Goodint x = 0;
cout << x << endl; // Predictable output// Badint a, b, c;
// Goodint width, height, depth;// Badif (score >= 70) { ... }
// GoodconstintPASSING_SCORE = 70;
if (score >= PASSING_SCORE) { ... }// Badif (condition)
statement;
// Goodif (condition) {
statement;
}#include<iostream>usingnamespacestd;intmain() {
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Sum: " << num1 + num2 << endl;
cout << "Difference: " << num1 - num2 << endl;
cout << "Product: " << num1 * num2 << endl;
if (num2 != 0) {
cout << "Quotient: " << num1 / num2 << endl;
} else {
cout << "Cannot divide by zero!" << endl;
}
return0;
}#include<iostream>usingnamespacestd;intmain() {
double celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
cout << celsius << "°C = " << fahrenheit << "°F" << endl;
return0;
}After mastering basic syntax, move on to:
- Control Flow - if/else, loops, switch statements
- Functions - Function definition, parameters, return values
- Arrays & Vectors - Collections of data
- Strings - Text manipulation
Remember: Practice is key! Write small programs to reinforce these concepts.