C++ Basic Syntax and Program Structure | main Function, Include Directives, Namespaces and Keywords


This complete tutorial on C++ Basic Syntax and Structure explains how a C++ program is written and executed. It covers the structure of a C++ program, #include directives, the main() function, namespaces such as std, comments, and C++ keywords. The tutorial follows best coding practices and is ideal for beginners who want to build strong fundamentals before moving to advanced C++ concepts.

Basic Syntax and Structure – Complete Tutorial

1. Structure of a C++ Program

A C++ program follows a well-defined structure that allows the compiler to understand and execute it correctly.

Basic structure:


#include <iostream>

using namespace std;

int main() {
cout << "Hello C++";
return 0;
}

Components:

  1. Preprocessor directives
  2. Namespace declaration
  3. main() function
  4. Statements and expressions
  5. Return statement

2. #include Directives

#include is a preprocessor directive used to include header files that contain declarations of functions, classes, and variables.

Types of includes:

  1. System header files

#include <iostream>
#include <vector>
  1. User-defined header files

#include "myheader.h"

Best Practices:

  1. Include only required headers
  2. Avoid unnecessary includes to reduce compile time
  3. Prefer standard headers (<iostream>) over old C headers (<stdio.h>)

3. main() Function

The main() function is the entry point of every C++ program. Program execution starts from main().

Syntax:


int main() {
// code
return 0;
}

Key Points:

  1. Must return an integer value
  2. return 0 indicates successful execution
  3. Only one main() function is allowed

Alternative form:


int main(int argc, char* argv[]) {
return 0;
}

4. Namespaces (std)

Namespaces are used to avoid name conflicts between identifiers.

Example:


std::cout << "Hello";
std::endl;

Instead of:


using namespace std;
cout << "Hello";

Best Practices:

  1. Avoid using namespace std; in large programs
  2. Use scope resolution operator (std::) explicitly
  3. Use using std::cout; when required

5. Comments

Comments are used to explain code and improve readability. They are ignored by the compiler.

Single-line comment:


// This is a single-line comment

Multi-line comment:


/*
This is a
multi-line comment
*/

Best Practices:

  1. Write meaningful comments
  2. Avoid obvious comments
  3. Update comments when code changes

6. Keywords

Keywords are reserved words with predefined meanings in C++. They cannot be used as identifiers.

Common C++ Keywords:

  1. Data types: int, float, double, char
  2. Control: if, else, switch, for, while
  3. OOP: class, public, private, protected
  4. Memory: new, delete
  5. Others: return, const, static, virtual

Example (invalid usage):


int class = 10; // Error: class is a keyword

Best Practices for C++ Syntax

  1. Follow consistent indentation
  2. Use meaningful variable and function names
  3. Avoid global variables
  4. Use braces {} even for single-line blocks
  5. Write readable and maintainable code

Summary

In this chapter, you learned the basic syntax and structure of a C++ program, including #include directives, the main() function, namespaces, comments, and keywords. These concepts form the foundation of all C++ programs and must be mastered before moving to data types and control structures.