Programming Languages
Classification of Programming Languages
● Programming languages vary in the level of abstraction they provide from the
hardware.
● Some programming languages provide less or no abstraction while some provide
higher abstraction.
● Based on the levels of abstraction, they can be classified into two
categories:
○ Low-level language
○ High-level language
Low-level languages
● No or little abstraction from hardware
● 0 and 1 are machine instructions
● Languages
○ Machine language
○ Assembly language
Machine Language
● Consists of a set of instructions that are in the binary form 0 or 1.
● Creating a program in a machine-level language is a very difficult task
● Error-prone as it is not easy to understand
● Maintenance is high
● Not portable
Assembly Language
● Contains some human-readable commands such as mov,
add, sub, etc.
● Easier to write and understand
● The translator used for translating assembly code to machine
code is known as an assembler.
● Assembly language code is not portable
● Not portable
● Assembly language has some abstraction from the hardware
● Assembly language is slower than machine language
High-Level Language
● Allows a programmer to write the programs which are independent of
a particular type of computer
● Closer to human languages than low-level languages.
● A compiler is required to translate a high-level language into a low-
level language
● easy to read, write, and maintain as it is written in English like words.
● eg: C, C++, Java, Python, Ruby, PHP, Javascript, Visual Basic
C++
● Developed by Bjarne Stroustrup at AT & T Bell Laboratory during 1979.
● Originally derived from C language and influenced by many languages like Simula,
BCPL, Ada, ML, CLU and ALGOL 68.
● Advantages
○ Highly portable and is often used for multi-device, multi-platform app development
○ Object Oriented
○ Has rich function library, ○ Fast and efficient
Identifiers
● User-defined names given to different parts of the C++ program viz. variables,
functions, arrays, classes etc.
● Rules for naming
○ First character must be an alphabet or an underscore (_)
○ Only alphabets, digits and underscore are permitted. Other special characters are
not allowed as part of an identifier
○ C++ is case sensitive
○ Reserved words or keywords cannot be used
Literals (Constants)
● Values do not change during the execution of a program
● Types
○ Numeric Constants
■ Integer Constants
■ Real Constants
○ Boolean Constants
○ Character Constants
○ Strings
Numeric Constants
● Numeric values used as constants Types
1. Integer Constants (or) Fixed point constants.
■ Whole numbers without decimal
■ May be signed or unsigned
■ Types
● Decimal
● Octal
● Hexadecimal
2. Real constants (or) Floating point constants.
■ Has fractional component (decimal point)
■ May be written in fractional or exponent form
■ Exponent form has two parts Mantissa and Exponent
Boolean Literals
● Used to represent one of the Boolean values (True or
false).
● Internally true has value 1 and false has value 0.
Character Constant
● Valid single character enclosed within single quote
● Escape Sequence or Non-graphic character
○ Cannot be typed directly from a keyboard
during execution
○ Can be represented by using escape
sequences.
○ Represented by a backslash followed by one or
two characters.
String Literals
● Sequence of characters enclosed within double
quotes are called as String literals.
● By default, string literals are automatically added with
a special character ‘\0’ (Null) at the end
Operators
● Symbols used to do some mathematical or logical operations are called as
“Operators”.
● Data items or values that the operators act upon are called as “Operands”.
● Types
1. Unary Operators - Require only one operand
2. Binary Operators - Require two operands
i. Arithmetic Operators
ii. Relational Operators
iii. Logical Operators
iv. Assignment Operators
v. Conditional Operator
3. Ternary Operators - Require three operands
Punctuators
● Symbols used as delimiters, while constructing a C++
program are called punctuators or Separators.
1. Curly braces { }
2. Parenthesis ()
3. Square brackets []
4. Comma ,
5. Semicolon ;
6. Colon :
7. Comments
i. //
ii. /* */
Input Operator
● The operator >> is used to get input.
● It extracts the value through the keyboard and assigns it to the variable on its right;
hence, it is called as “Stream extraction” or “get from” operator.
● The first operand is the pre-defined identifier cin (pronounced as C-In) that
identifies keyboard as the input device.
● The second operand must be a variable.
● eg: cin >> num;
● To receive or extract more than one value at a time, >> operator should be used
for each variable. This is called cascading of operator.
○ eg: cin >> num1 >> num2;
Output Operator
● The operator << is used to get output.
● It is called the “Stream insertion” or “put to” operator.
● It is used to send the strings or values of the variables on its right to the object on
its left.
● The first operand is the predefined identifier cout (pronounced as C-Out) that
identifies monitor as the standard output object.
● The second operand may be a constant, variable or an expression.
● eg: cout << “Welcome to Sparks Academy”;
● To send more than one value at a time, << operator should be used for each
constant/variable/expression. This is called cascading of operator
● eg: cout << “The sum is = “ << sum;
Program Execution
● Create source code and save with .cpp extension. Header file might be in .h
or .hpp extension
● Compile the program with a compiler to check for errors. If compilation success
executable is created.
○ GCC, Clang, Borland C++, Microsoft C++ compiler are examples of compilers
● Executable is then run
● Integrated Development Environment (IDE) is used to create a C++ Program
○ Dev C++, Code::blocks, Visual Studio, Clion are IDEs
Example Program
// C++ program to print a string
#include <iostream>
using namespace std;
int main()
{
cout << “Welcome to Sparks Academy”;
return 0;
}
Example Program explained
1. // C++ program to print a string
● This is a comment. It is used to provide information for a person who is reading the
program
2. #include <iostream>
● # is a directive for the preprocessor which means the statement is processed
before compilation
● #include <iostream> tells the compiler’s preprocessor to include the header file
iostream in the program
● namespace std and cout are available in iostream header
3. using namespace std;
● Tells the compiler to use standard namespace.
● Namespaces provide a method of preventing name conflicts in large projects
Example Program explained
1. int main()
● main( ) function is the starting point where all C++ programs begin their execution
● Executable statements should be inside the main( ) function
2. {
cout << “Welcome to Sparks Academy”;
return 0;
}
● Statements between the curly braces are executable statements.
● Called as a block of code
● cout simply sends the string constant “Welcome to Sparks Academy” to the screen
● Return is a keyword which is used to return the value what you specified to a
function.
● In this case, it will return 0 to main( ) function.
● Every programming language has two fundamental elements, viz., data types and
variables.
● They are very essential elements to write even the most elementary programs.
● C++ provides a predefined set of data types for handling the data items.
● Such data types are known as fundamental or built-in data types.
● Apart from the built-in data types, a programmer can also create his own data
types called as User-defined data types
● In a programming language, fields are referred as variables and the values are
referred to as data.
● Before handling any data, it should be clearly specified to the language compiler,
regarding what kind of data it is, with some predefined set of data types.
Data type modifiers
● Used to modify the storing capacity of a fundamental data type except void type.
● Modifiers can be used to modify (expand or reduce) the memory allocation of any
fundamental
data type. They are also called as Qualifiers.
● There are four modifiers used in C++.
○ signed
○ unsigned
○ long
○ short
Number Suffixes
● Suffix can be used to assign the same value as a different type.
● Eg: if you want to store 45 in int, long, unsigned int and unsigned long int, you can
use suffix letter L or U (either case) with 45 i.e. 45L or 45U.
● This type of declaration instructs the compiler to store the given values as long and
unsigned.
● ‘F’ can be used for floating point values, example: 3.14F
Variables
● User-defined names assigned to specific memory locations in which the values are
stored.
● Rules for naming the identifiers should be followed while naming a variable
Declaring Variables
● Every variable should be declared before they are actually used in a program.
● Declaration is a process to instruct the compiler to allocate memory as per the type
that is specified along with the variable name.
● More than one variable of the same type can be declared as a single statement
using a comma separating the individual variables.
● Syntax: <data type> <var1>, <var2>, <var3> …… <var_n>;
● eg: int num1, num2, sum;
● If you declare a variable without any initial value, the memory space allocated to
that variable will be occupied with some unknown value called as “Junk” or
“Garbage” values
Variable Initialization
● Assigning an initial value to a variable during its declaration is called as
“Initialization”.
● eg: int num = 100;
double price = 231.45;
Dynamic Initialization of Variables
● Initializing variable during the execution of a program is called Dynamic
Initialization.
● eg: int sum = num1+num2;
Constants
● const is the keyword used to declare a constant.
● const keyword modifies / restricts the accessibility of a variable. So, it is known as
Access modifier. ● eg: const int num = 100;
References
● A reference provides an alias for a previously defined variable.
● Declaration of a reference consists of base type and an & (ampersand) symbol;
● Reference variable name is assigned the value of a previously declared variable.
● Syntax
<type> <& reference_variable> = <original_variable>;
● Manipulators are used to format the output of any C++ program.
● Manipulators are functions specifically designed to use with the insertion (<<) and
extraction(>>) operators.
● C++ offers several input and output manipulators for formatting.
● Commonly used manipulators are: endl, setw, setfill, setprecision and setf.
● In order to use these manipulators, you should include the appropriate header file
● endl (End the Line)
○ endl is used as a line feeder in C++. It can be used as an alternative to ‘\n’.
○ In other words, endl inserts a new line and then makes the cursor to point to the
beginning of the next line
○ eg: cout << "The value of num = " << num <<endl;
● setw ( )
○ setw manipulator sets the width of the field assigned for the output.
○ The field width determines the minimum number of characters to be written in
output.
○ Syntax: setw(number of characters
○ eg: cout << setw(25) << "Basic Pay : " << setw(10)<< basic<< endl;
● setprecision ( )
○ This is used to display numbers with fractions in specific number of digits.
○ Syntax: setprecision (number of digits);
○ eg: cout << setprecision (5) << hra;
● An expression is a combination of operators, constants and variables arranged as
per the rules of C++.
● It may also include function calls which return values.
● An expression may consist of one or more operands, and zero or more operators
to produce a value.
● In C++, there are seven types of expressions, and they are:
○ Constant Expression
○ Integer Expression
○ Floating Expression
○ Relational Expression
○ Logical Expression
○ Bitwise Expression
○ Pointer Expression