PF Lecture 1
PF Lecture 1
PF Lecture 1
return 0;
}
1. Header File
The header files contain the definition of the functions and macros we are using in
our program. They are defined on the top of the C++ program.
In line #1, we used the #include <iostream> statement to tell the compiler to
include an iostream header file library which stores the definition of the cin and
cout standard input/output streams that we have used for input and
output. #include is a preprocessor directive using which we import header files.
Syntax:
#include <library_name>
2. Namespace
Syntax:
using namespace std;
3. Main Function
Functions are basic building blocks of a C++ program that contains the instructions for
performing some specific task. Apart from the instructions present in its body, a function
definition also contains information about its return type and parameters. To know more
about C++ functions, please refer to the article Functions in C++.
In line #3, we defined the main function as int main(). The main function is the most
important part of any C++ program. The program execution always starts from the main
function. All the other functions are called from the main function. In C++, the main
function is required to return some value indicating the execution status.
int main() {
Blocks are the group of statements that are enclosed within { } braces. They define the
scope of the identifiers and are generally used to enclose the body of functions and
control statements.
The body of the main function is enclosed within { }.
Syntax:
return 0;
}
7. Keywords
In the C++ programming language, there are some reserved words that are used for some
special meaning in the C++ program. It can’t be used for identifiers.
For example, the words int, return, and using are some keywords used in our program. These
all have some predefined meaning in the C++ language.
There are total 95 keywords in C++. These are some keywords.
int main() {
cout << "Hello World!";
return 0;
}
You can add as many cout objects as you want. However, note that it does not insert a new line
at the end of the output:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
To insert a new line, you can use the \n character:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
Another way to insert a new line, is with the endl manipulator:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It can also be used
to prevent execution when testing alternative code. Comments can be singled-lined or multi-
lined.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not be executed).
This example uses a single-line comment before a line of code:
// This is a comment
cout << "Hello World!";
C++ Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:
/* The code below will print the words Hello World!
to the screen, and it is amazing */
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:
•int - stores integers (whole numbers), without decimals, such as 123 or -123
•double - stores floating point numbers, with decimals, such as 19.99 or -19.99
•char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
•string - stores text, such as "Hello World". String values are surrounded by double quotes
•bool - stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, specify the type and assign it a value:
Syntax
type variableName ;
Declare Many Variables
To declare more than one variable of the same type, use a comma-separated list:
type variableName1,variableName2;
Example
int x, y, z ;
Initializing Variables:
To create a variable, specify the type and assign it a value:
Syntax
type variableName = value;
OR
type variableName;
variableName=value;
Initializing Many Variables
To declare more than one variable of the same type, use a comma-separated list:
type variableName1=value ,variableName2=value;
Example
int x = 5, y = 6, z = 50;
C++ Identifiers
All C++ variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
Constants
When you do not want others (or yourself) to change existing variable values, use
the const keyword (this will declare the variable as "constant", which means unchangeable
and read-only):
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
You should always declare the variable as constant when you have values that are unlikely to
change:
const int minutesPerHour = 60;
const float PI = 3.14;
C++ User Input
cin is a predefined variable that reads data from the keyboard with the extraction operator (>>).
In the following example, the user can input a number, which is stored in the variable x. Then
we print the value of x:
Syntax of the cin object is:
cin >> var_name;
Here, >> is the extraction operator.
Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
Data Type Size Description
boolean 1 byte Stores true or false
values
Char 1 byte Stores a single
character/letter/number,
or ASCII values
Int 2 or 4 bytes Stores whole numbers,
without decimals
C++ Data Types
float 4 bytes Stores fractional
Basic Data Types numbers, containing one
The data type specifies the size or more decimals.
Sufficient for storing 6-7
and type of information the decimal digits
variable will store:
double 8 bytes Stores fractional
numbers, containing one
or more decimals.
Sufficient for storing 15
decimal digits
1 byte is equal to 8 bits.
C++ Operators
C++ divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Operator Name Description Example
+ Addition Adds together two x + y
Arithmetic values
Operator - Subtraction Subtracts one x-y
value from another
* Multiplication Multiplies two x*y
values
/ Division Divides one value x/y
by another
% Modulus Returns the x%y
division remainder
++ Increment Increases the value ++x
of a variable by 1
-- Decrement Decreases the --x
value of a variable
by 1
Assignment Operators
== Equal to x == y
!= Not equal x != y