0% found this document useful (0 votes)
2 views4 pages

Lecture 1 C - Program Structure & Basics

This document provides an overview of the structure and basics of C++ programming, including the essential components like the main() function, preprocessors, header files, and namespaces. It explains the use of input/output operations with cout and cin, as well as the concept of tokens in C++. Key points include the importance of returning an integer from the main() function and the use of comments for code readability.

Uploaded by

rajmourya5864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Lecture 1 C - Program Structure & Basics

This document provides an overview of the structure and basics of C++ programming, including the essential components like the main() function, preprocessors, header files, and namespaces. It explains the use of input/output operations with cout and cin, as well as the concept of tokens in C++. Key points include the importance of returning an integer from the main() function and the use of comments for code readability.

Uploaded by

rajmourya5864
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture 1: C++ Program

Structure & Basics

1. First C++ Program


Basic Structure:

#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!";
return 0;
}

Key Points:
Every C++ program must have a main() function.

#include <iostream> is a preprocessor directive.

cout is used for output.

2. Preprocessors in C++
Preprocessors are directives that begin with # and are processed before
compilation.

Common ones:

#include – include files

#define – define macros

#include <iostream> // inserts iostream header

3. Header Files in C++

Lecture 1: C++ Program Structure & Basics 1


Header files contain declarations of standard input/output, strings, math
functions, etc.

Header Use
<iostream> Input/output using cin, cout
<cmath> Math functions like sqrt(), pow()
<string> String class
<vector> Dynamic arrays

#include <cmath>

4. Namespaces in C++
Avoids name conflicts in large projects.

std is the standard namespace.

#include <iostream>
using namespace std;

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

Without using namespace std; , we write:

std::cout << "Hello";

5. main() Function
Starting point of C++ program.

Must return an int :

int main() {
// code

Lecture 1: C++ Program Structure & Basics 2


return 0;
}

Important:
Returning 0 indicates successful execution.

6. Comments in C++
Used to improve readability and add documentation.

Type Syntax

Single Line // This is a comment

Multi-line /* Comment block */

7. Input and Output in C++


Output: cout

cout << "Output text";

Input: cin

int x;
cin >> x;

Chaining Example:

int a, b;
cin >> a >> b;
cout << "Sum: " << a + b;

8. Tokens in C++
Tokens = smallest individual elements of a program.

Token Type Example

Lecture 1: C++ Program Structure & Basics 3


Keywords int, return, if, for

Identifiers myVar, count, getValue

Constants 10, 3.14, 'a'

Operators +, -, =, ==, &&

Special Symbols (), {}, [], ;

Strings "Hello"

Lecture 1: C++ Program Structure & Basics 4

You might also like