You are on page 1of 26

Programming Fundamentals

Programming in C++

SHAHBAZ NAZEER
shahbaznazeer@gcuf.edu.pk
Outline

 History of C
 Features of C++
 Basic Structure of C++ Program
 Keywords
 Comments
 Debugging
 Variables
 Data Types
 Type Casting
 Operators
C Programming Language

 Developed in 1972 by Dennis Ritchie at AT & T Bell


 Labs (American Telephone & Telegraph Company)
 Very widely used general purpose programming language
 Available on many machines and operating systems
 Design to be flexible and powerful
 Originally a replacement for assembly language (middlelevel)
 C requires extreme care in programming
 C++ is a superset of C
Features of C++

 Convenient Language.
 Well Structured Language
 Case Sensitive
 Machine Independent
 Object Oriented
 Modular Programming
 Standard C++ Libraries
 Hardware control
C++ Example Program

ssor 1. #include "iostream" H ea


roce der
F ile
e-p tive
r
P ec
r 2. using namespace std;
Di
Mai
3. int main() n fu
nctio
n
4. {
ody
ti on B
5. cout<<"Welcome MscIT Session 2020-24" ;
u nc
F
6. return 1; Stat
eme
nt
7. }
Basic Structure of C++ Program

 Structure is the format of writing a C++ program


1. Preprocessor Directives
 Its also called compiler directives. This is the instruction given to the compiler before the execution of actual program. It starts with
the # symbol. The #include preprocessor directive is used to include header file in the program. Header files are collection of
standard library functions to perform different tasks.

2. Main() function
 Main() function is the basic entry point of a C++ program. When a program runs the control enters in main function and start
executing its statements.

3. Program Body
 The portion inside the curly braces {} is called body of the function. The statement of the program runs in theses curly braces.
Known as opening and closing braces. These braces are also called delimiters.
 The cout object with insertion operator ( << ) is used to print a message on the screen.
Basic Structure of C++ Program

 Token
Each word and punctuation is referred to as a token in C++. Tokens are the smallest building block or smallest unit of a C++
program. Below is the list of tokens available in C++
1. Identifiers - Identifiers are names given to different entries such as variables, structures, and functions. Also, identifier
names should have to be unique because these entities are used in the execution of the program.
2. Keywords - Keywords are reserved words which have fixed meaning, and its meaning cannot be changed. The meaning and
working of these keywords are already known to the compiler.
3. Constants - Constants are like a variable, except that their value never changes during execution once defined.
4. Operators - C++ operator is a symbol that is used to perform mathematical (+,-,*,/, % ) or logical ( > , < , = ) manipulations.
5. Strings - object that signify sequence of chracters.
 White Spaces
 Space, Tab and Enter etc are the white space, these are used to increase the readability of the program.
Keywords in C++
Comments in a program

 In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a


computer program.
 It makes a program more readable and error finding become easier.
 One important part of good documentation is Comments.
 Comments are statements that are not executed by the compiler and interpreter.

 In C/C++ there are two types of comments :


1. Single line comment - Represented as // double forward slash.
2. Multi-line comment - Represented as /* any_text single line or multi line */ 
 start with forward slash and asterisk ( /* ) and end with asterisk and forward slash ( */ ).
It is used to denote multi-line comment. It can apply comment to more than a single line.
Debugging

 Debugging
 An error in programming is known as bug. The process of finding and removing the error is called debugging.
Errors in C++

 Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax. This compiler error indicates something
that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-
time errors.
 Most frequent syntax errors are:
 Missing Parenthesis (}) or semi-colon at the end of statement.
 Printing the value of variable without declaring it
1. #include "iostream"
2. using namespace std;
3. int main()
4. {
5. cout<<"Welcome Msc IT Session 2020-22“ // Semi-colon missing
6. return 1;
7. }
Errors in C++

 Run-time Errors : Errors which occur during program execution(run-time) after successful compilation are
called run-time errors. One of the most common run-time error is division by zero also known as Division error.
These types of error are hard to find as the compiler doesn’t point to the line at which the error occurs.
 Linker Errors: These error occurs when after compilation we link the different object files with main’s object
using key(RUN). These are errors generated when the executable of the program cannot be generated. This may
be due to wrong function prototyping, incorrect header files. One of the most common linker error is
writing Main() instead of main().
 Logical Errors : On compilation and execution of a program, desired output is not obtained when certain input
values are given. These types of errors which provide incorrect output but appears to be error free are called
logical errors. These are one of the most common errors done by beginners of programming. These errors solely
depend on the logical thinking of the programmer and are easy to detect if we follow the line of execution and
determine why the program takes that path of execution.
 Semantic errors : This error occurs when the statements written in the program are not meaningful to the
compiler.
Variables

 A variable is a name given to a memory location. It is the basic


unit of storage in a program.
 The value stored in a variable can be changed during program
execution.
 A variable is only a name given to a memory location, all the
operations done on the variable effects that memory location.
 In C++, all the variables must be declared before use.
 A variable name can consist of alphabets (both upper and lower
case), numbers and the underscore ‘_’ character.
 Variable name should not start with a number.
X = X + 1;

+ 1
X=
10 = 11
C++ Data Types

 All variables use data-type during


declaration to restrict the type of data
to be stored.
 Whenever a variable is defined in C+
+, the compiler allocates some
memory for that variable based on the
data-type.
 Every data type requires a different
amount of memory.
C++ Data Types

DATA TYPE SIZE (IN BYTES) RANGE


SHORT INT 2 -32,768 TO 32,767

UNSIGNED SHORT INT 2 0 TO 65,535

UNSIGNED INT 4 0 TO 4,294,967,295

INT 4 -2,147,483,648 TO 2,147,483,647

LONG INT 8 -2,147,483,648 TO 2,147,483,647

UNSIGNED LONG INT 8 0 TO 4,294,967,295

UNSIGNED LONG LONG INT 8 0 TO 18,446,744,073,709,551,615

SIGNED CHAR 1 -128 TO 127

UNSIGNED CHAR 1 0 TO 255

FLOAT 4  

DOUBLE 8  

LONG DOUBLE 12  
C++ program to sizes of data types

1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. cout << "Size of char : " << sizeof(char) << " byte" << endl;
6. cout << "Size of int : " << sizeof(int) << " bytes" << endl;
7. cout << "Size of short int : " << sizeof(short int) << " bytes" << endl;
8. cout << "Size of long int : " << sizeof(long int) << " bytes" << endl;
9. cout << "Size of float : " << sizeof(float) << " bytes" <<endl;
10. cout << "Size of double : " << sizeof(double) << " bytes" << endl;
11. return 0;
12. }
Type Casting

 A type cast is basically a conversion from one data type to another. There are two types of type conversion:
1. Implicit Type Conversion 
2. Explicit Type Conversion

 Implicit Type Conversion, Also known as ‘automatic type conversion’. Done by the compiler on its own,
without any external trigger from the user.
 Generally takes place when in an expression more than one data type is present. In such condition type
conversion (type promotion) takes place to avoid lose of data.
 All the data types of the variables are upgraded to the data type of the variable with largest data type.
Example of implicit conversion

1. #include <iostream>
Program Output
2. using namespace std;
3. int main() x = 107
4. { y=a
z = 108
5. int x = 10; // integer x
6. char y = 'a';
7. x = x + y; // character y implicitly converted to int. ASCII value of 'a' is 97
8. float z = x + 1.0; // x is implicitly converted to float
9. cout << "x = " << x << endl << "y = " << y << endl << "z = " << z << endl;
10. return 0;
11. }
Explicit Type Conversion

Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result
to make it of a particular data type. In C++, it can be done by two ways:
 Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis.
This can be also considered as forceful casting.
1. #include <iostream>
2. using namespace std;
3. int main()
4. { // Explicit conversion from double to int
5. double x = 1.2;
6. int sum = (int) x + 1;
7. cout << "Sum = " << sum;
8. return 0;
9. }
Operators in C++
Unary operator

 Unary operator: are operators that act upon a single operand to produce a new value.
 Types of unary operators:
1. Increment ( ++ ) : Used to increment the value of a variable by 1.
2. Decrement ( - - ) : Used to decrement the value of a variable by 1.

 Increment or decrement unary operator can be used in two method.

1. Prefix : In this method, the operator preceeds the operand (e.g., ++a, --a). The value of operand
will be altered before it is used.
2. Postfix : In this method, the operator follows the operand (e.g., a++, a--). The value operand
will be altered after it is used.
% = Remainder

5%2=1
2%2=0
8%3=2
19 % 5 = ?
Solve below Expression.

100 + 200 / 10 - 3 * 10 =

?
Precedence
 Operator precedence determines which operator is performed
first in an expression with more than one operators with
different precedence.

 Highest: ( )
 Next: *,/,%
 Lowest: +,-

 If more than one with similar precedence then Left to Right Rule
OPERATOR Precedence

You might also like