尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
C++
IT 3rd Sem
C & C++
Ken Thompson designed B Language [BCPL(Basic Combined
Programming language)]
 C was developed in 1970s in Bell Laboratories by Dennis
Ritchie (B language modified)
 C was written for UNIX operating system.
History of C Language
History of C++
Language
 designed by Bjarne Stroutstrup in early 1980s
 Named….. C with classes
 In 1983, name changes to C++
IT 3rd Sem
C & C++
 C++ is superset of C
 case-sensitive
Similarities
Differences
 classes and objects
 C is procedural language whereas C++ is object-oriented
programming language.
IT 3rd Sem
C++
Character Set
IT 3rd Sem
Letters A-Z, a-z
Digits 0-9
Special Symbols Space + − * / ^  ( ) [ ] { } = !=
< > . ‘ “ , % ! & _ # <= >=
White Spaces Blank Space, Horizontal Tab (→ )
Carriage Return ( ) Newline
 Source File
 Object File
 Tokens
 Identifiers
 Keywords
Important Terms
IT 3rd Sem
C++
Data Types
IT 3rd Sem
C++
int
 2 bytes
 range -32768 to +32767 (signed)
 range 0 to +65535 (unsigned)
Type Size Range
int 2 -32767 to +32767
unsigned int 2 0 to 65535
signed int 2 -32767 to +32767
short int 2 -32767 to +32767
unsigned short int 2 0 to 65535
signed short int 2 -32767 to +32767
long int 4 -2147483648 to +2147483647
signed long int 4 -2147483648 to +2147483647
unsigned long int 4 0 to +42949667295
IT 3rd Sem
C++
float & double
Type Size Range
float 4 3.4 E-38 to 3.4 E+38
double 8 1.7 E-308 to 1.7 E+308
long double 10 3.4 E-4932 to 1.1 E+4932
IT 3rd Sem
C++
char
Type Size Range
char 1 -128 to +127
unsigned char 1 0 to +255
signed char 1 -128 to +127
void
 size 0 bytes
IT 3rd Sem
C++
Variable
 Rules
Variable Declaration
data-type var1,var2;
For eg:
int a,b,c;
float x;
IT 3rd Sem
C++
Variable initialization
data-type var1;
var1=value;
For eg:
int a;
a=2;
Method 1:
Method 2:
data-type var1=value;
For eg:
int a=2;
IT 3rd Sem
C++
C++
Derived Data Types:
 Array
 Structure
 Union
 Enumerations
 Pointers
 Class
IT 3rd Sem
C++
Enumerations:
 set of values represented by identifiers
Format:
enum name{var1,var2,var3,----------, varn};
name n1,n2;
n1=var1;
n2=var3;
cout<<n1;
cout<<n2;
 output will be 0 and 2
IT 3rd Sem
C++
Operators:
Type Operators Meaning
Arithmetic
+ Addition
− Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
Relational
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
IT 3rd Sem
C++
Operators:
Type Operators Meaning
Logical
&& Logical AND
|| Logical OR
! Logical NOT
Bitwise
& Bitwise AND
I Bitwise OR
^ Bitwise Exclusive-OR (XOR)
>> Bitwise Shift Right
<< Bitwise Shift Left
~ Bitwise Complement
IT 3rd Sem
C++
Operators:
Type Operators Meaning
Assignment
= Assignment
+= a+=b means a=a+b
−= a−=b means a=a−b
*= a*=b means a=a*b
/= a/=b means a=a/b
%= a%=b means a=a%b
>>= a>>=n means a=a>>n
<<= a<<=n means a=a<<n
&= a&=b means a=a&b
|= a|=b means a=a|b
!= a!=b means a=a!b
IT 3rd Sem
C++
Operators:
Type Operators Meaning
Special
* Pointer
& Address
. Membership
:: Scope Resolution
size of()
?: Ternary Operator
++ Increment (pre and post)
−− Decrement (pre and post)
IT 3rd Sem
C++
Operator Precedence:
IT 3rd Sem
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the expression
in the innermost pair is evaluated first. If there are several
pairs of parentheses “on the same level” (i.e., not nested),
they are evaluated left to right.
!, +, - Logical NOT, signs Evaluated second. If there are several, they re
evaluated right to left.
*, /, or % Multiplication Division
Modulus
Evaluated third. If there are several, they re
evaluated left to right.
+ or - Addition
Subtraction
Evaluated fourth. If there are several, they are
evaluated left to right.
>, <, >=, <= Relational Operators Evaluated fifth. If there are several, they are
evaluated left to right.
==, != Equality Operators Evaluated sixth. If there are several, they are
evaluated left to right.
&& Logical AND Evaluated seventh. If there are several, they are
evaluated left to right.
| | Logical OR Evaluated eighth. If there are several, they are
evaluated left to right.
C++
Expression:
IT 3rd Sem
C++
Type Conversion:
 Implicit or Automatic
 Explicit or type casting
Syntax:
(cast-type) expression;
cast-type (expression);
For eg:
int a,b;
float c;
c = (float) a/b;
IT 3rd Sem
C++
Phases of C++
programs:
IT 3rd Sem
C++ PROGRAM DEVELOPMENT GOES THROUGH SIX STEPS:
Step1: Edit (using text editor to type, correct and save the program
file).
Step2: preprocessor, automatically before compile, executes to
include other text files in the file to be compiled.
Step3: Compile , the compiler translates the C++ code into machine
language (also called object-code).
Step4: Link , it is linking any used functions that are defined elsewhere
such as standard library functions, or private library for a group of
programmers, linker, links the object code with the code for the missing
functions to provide full code
Step5: Load, a program before executing , must be loaded into the
main memory, loader does this task , loading code from disk.
Step6: Execute, the computer under the control of its CPU executes
the program. Instruction by instruction.
C++
IT 3rd Sem
ILLUSTRATION OF C++
PROGRAM PHASES
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Loader
main
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and
stores
it on disk.
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Editor
Preprocessor
Linker
CPU
main
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
C++
Structure of C++ program
IT 3rd Sem
 Every C++ program must have a function named main. The programmer
can choose to decompose the program into several parts (user-defined
functions). Think of main as the master and the other functions are the
servants.
 The execution always starts with main.
1. Comments
2. Load <iostream.h>
3. main
3.1 Print "Welcome to C++n"
3.2 exit (return 0)
1 // Fig. 1.2: fig01_02.cpp
2 // A first program in C++
3 #include <iostream.h>
4
5 int main()
6 {
7 cout << "Welcome to C++!n";
8
9 return 0; // indicate that program ended successfully
10 }
Welcome to C++!
preprocessor directive
Message to the C++ preprocessor.
Lines beginning with # are preprocessor directives.
#include <iostream.h> tells the preprocessor to include
the contents of the file <iostream.h>, which includes
input/output operations (such as printing to the screen).
Comments
Written between /* and */ or following a //.
Improve program readability and do not cause the computer to
perform any action.
C++ programs contain one or more functions, one of which must
be main
Parenthesis are used to indicate a function
int means that main "returns" an integer value.
Prints the string of characters contained between the quotation
marks.
The entire line, including cout, the << operator, the string
"Welcome to C++!n" and the semicolon (;), is called a
statement.
All statements must end with a semicolon.
return is a way to exit a function from a
function.
return 0, in this case, means that the
program terminated normally.
A left brace { begins the body of every function
and a right brace } ends it.
C++
A Simple Program: Printing a Line of Text
IT 3rd Sem
cout
Standard output stream object
“Connected” to the screen
<<
Stream insertion operator
Value to the right of the operator (right operand)
inserted into output stream (which is connected to the
screen)
cout << “Welcome to C++!n”;

Escape character
Indicates that a “special” character is to be output
C++
A Simple Program: Printing a Line of Text
IT 3rd Sem
Escape Sequence Description
n Newline. Position the screen cursor to the
beginning of the next line.
t Horizontal tab. Move the screen cursor to the next
tab stop.
r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
a Alert. Sound the system bell.
 Backslash. Used to print a backslash character.
" Double quote. Used to print a double quote
character.
 There are multiple ways to print text
 Following are more examples
C++
IT 3rd Sem
1. Load
<iostream.h>
2. main
2.1 Print "Welcome"
2.2 Print "to C++!"
2.3 newline
2.4 exit (return 0)
Program Output
Welcome to C++!
1 // Fig. 1.4: fig01_04.cpp
2 // Printing a line with multiple statements
3 #include <iostream.h>
4
5 int main()
6 {
7 cout << "Welcome ";
8 cout << "to C++!n";
9
10 return 0; // indicate that program ended successfully
11 }
Unless new line 'n' is specified, the text continues
on the same line.
C++
IT 3rd Sem
1. Load
<iostream.h>
2. main
2.1 Print "Welcome"
2.2 newline
2.3 Print "to"
2.4 newline
2.5 newline
2.6 Print "C++!"
2.7 newline
2.8 exit (return 0)
Program Output
1 // Fig. 1.5: fig01_05.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream.h>
4
5 int main()
6 {
7 cout << "WelcomentonnC++!n";
8
9 return 0; // indicate that program ended successfully
10 }
Welcome
to
C++!
Multiple lines can be printed with one
statement.
C++
Another Program: Adding Two Integers
IT 3rd Sem
>> (stream extraction operator)
When used with cin, waits for the user to input a value and
stores the value in the variable to the right of the operator
The user types a value, then presses the Enter (Return) key
to send the data to the computer
Example:
int myVariable;
cin >> myVariable;
Waits for user input, then stores input in myVariable
= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
sum = variable1 + variable2;
1. Load <iostream>
2. main
2.1 Initialize variables
integer1, integer2,
and sum
2.2 Print "Enter first
integer"
2.2.1 Get input
2.3 Print "Enter
second integer"
2.3.1 Get input
2.4 Add variables and put
result into sum
2.5 Print "Sum is"
2.5.1 Output sum
2.6 exit (return 0)
Program Output
1 // Fig. 1.6: fig01_06.cpp
2 // Addition program
3 #include <iostream.h>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 cout << "Enter first integern"; // prompt
10 cin >> integer1; // read an integer
11 cout << "Enter second integern"; // prompt
12 cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 cout << "Sum is " << sum << endl; // print sum
15
16 return 0; // indicate that program ended successfully
17 }
Enter first integer
45
Enter second integer
72
Sum is 117
Variables can be output using
cout << variableName.
endl flushes the buffer and prints a
newline.
Notice how cin is used to get user input.
C++
C++
Built-in (Library) Function
IT 3rd Sem
Function Exponentiation
pow (x, y) x is raised to power y; (xy )
sqrt (x) Square-root of x
sin (x) Sine of x
cos (x) Cosine of x
tan (x) Tangent of x
exp (x) Exponentiation of x, ( ex)
fabs (x) Absolute Value of x, | x |
log (x) Logarithm of x (base e)
log10 (x) Logarithm of x (base 10)
floor (x) Rounding-down x
ceil (x) Rounding-up x
______etc. {there are more}_______________________
Eg.1. floor (9.2) = 9.0
Eg.2. floor (-9.8) = -10.0
Remark:you need to include <math.h> to be able to use these
functions. In newer versions it is #include<cmath>
C++
Common programming Errors
IT 3rd Sem
Divide by zero.
Not including iostream for input/output operations.
Forgetting the (;) at end of each statement.
If a space found between the pair-of-symbols for
the relational operators. (i.e., > = instead of >= ).
Confusing the equality == with the assignment =.
Reversing the order of the relational operators
(i.e., => instead of >=).
C++
Formatted Output
IT 3rd Sem
setw can be used to specify the width of the field that the next value of
output will be printed in. To use it, you must include <iomanip.h> header file.
eg1:-
int num=12;
cout<< setw(4) << num; //num will be printed in a field width of 4
character.
eg2:-
int n1=12;
int n2=197;
cout<<setw(5)<< n1 << setw(6) << n2;
eg3:- (if the value is more than the specified setw value the compiler ignores the
setw effect and print it with the minimum number of positions required.)
int num=1977;
cout<<setw(3)<<num;

More Related Content

What's hot

Introduction python
Introduction pythonIntroduction python
Introduction python
Jumbo Techno e_Learning
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
Bharat Kalia
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel
 
Programming in c
Programming in cProgramming in c
Programming in c
ankitjain851
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
Harish Kumawat
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Manoj Tyagi
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
Vivek chan
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
Input output statement
Input output statementInput output statement
Input output statement
sunilchute1
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri
 
Python ppt
Python pptPython ppt
Python ppt
Rachit Bhargava
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Amit Soni (CTFL)
 

What's hot (20)

Introduction python
Introduction pythonIntroduction python
Introduction python
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Programming in c
Programming in cProgramming in c
Programming in c
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Input output statement
Input output statementInput output statement
Input output statement
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python ppt
Python pptPython ppt
Python ppt
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 

Similar to basics of C and c++ by eteaching

Basics Of C++.pptx
Basics Of C++.pptxBasics Of C++.pptx
Basics Of C++.pptx
DineshDhuri4
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
HNDE Labuduwa Galle
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
Widad Jamaluddin
 
Chapter2
Chapter2Chapter2
Chapter2
Anees999
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
sajjad ali khan
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
Bussines man badhrinadh
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
Manzoor ALam
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
SONU KUMAR
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
MOHAMAD NOH AHMAD
 
C++
C++C++
c++
c++c++
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
Vivek chan
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
kelleyc3
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
Sumit Satam
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 

Similar to basics of C and c++ by eteaching (20)

Basics Of C++.pptx
Basics Of C++.pptxBasics Of C++.pptx
Basics Of C++.pptx
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
Chapter2
Chapter2Chapter2
Chapter2
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C++
C++C++
C++
 
c++
c++c++
c++
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 

Recently uploaded

The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
Derek Wenmoth
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
Kalna College
 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Catherine Dela Cruz
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
MattVassar1
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
yarusun
 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
Celine George
 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
EducationNC
 
The Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptxThe Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptx
PriyaKumari928991
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Celine George
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
Kalna College
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
heathfieldcps1
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
chaudharyreet2244
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
Celine George
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 

Recently uploaded (20)

The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
 
220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
 
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
Get Success with the Latest UiPath UIPATH-ADPV1 Exam Dumps (V11.02) 2024
 
How to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRMHow to Create a Stage or a Pipeline in Odoo 17 CRM
How to Create a Stage or a Pipeline in Odoo 17 CRM
 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
 
Opportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive themOpportunity scholarships and the schools that receive them
Opportunity scholarships and the schools that receive them
 
The Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptxThe Rise of the Digital Telecommunication Marketplace.pptx
The Rise of the Digital Telecommunication Marketplace.pptx
 
Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17Creation or Update of a Mandatory Field is Not Set in Odoo 17
Creation or Update of a Mandatory Field is Not Set in Odoo 17
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
 
nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...nutrition in plants chapter 1 class 7...
nutrition in plants chapter 1 class 7...
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 

basics of C and c++ by eteaching

  • 2. C & C++ Ken Thompson designed B Language [BCPL(Basic Combined Programming language)]  C was developed in 1970s in Bell Laboratories by Dennis Ritchie (B language modified)  C was written for UNIX operating system. History of C Language History of C++ Language  designed by Bjarne Stroutstrup in early 1980s  Named….. C with classes  In 1983, name changes to C++ IT 3rd Sem
  • 3. C & C++  C++ is superset of C  case-sensitive Similarities Differences  classes and objects  C is procedural language whereas C++ is object-oriented programming language. IT 3rd Sem
  • 4. C++ Character Set IT 3rd Sem Letters A-Z, a-z Digits 0-9 Special Symbols Space + − * / ^ ( ) [ ] { } = != < > . ‘ “ , % ! & _ # <= >= White Spaces Blank Space, Horizontal Tab (→ ) Carriage Return ( ) Newline
  • 5.  Source File  Object File  Tokens  Identifiers  Keywords Important Terms IT 3rd Sem C++
  • 7. int  2 bytes  range -32768 to +32767 (signed)  range 0 to +65535 (unsigned) Type Size Range int 2 -32767 to +32767 unsigned int 2 0 to 65535 signed int 2 -32767 to +32767 short int 2 -32767 to +32767 unsigned short int 2 0 to 65535 signed short int 2 -32767 to +32767 long int 4 -2147483648 to +2147483647 signed long int 4 -2147483648 to +2147483647 unsigned long int 4 0 to +42949667295 IT 3rd Sem C++
  • 8. float & double Type Size Range float 4 3.4 E-38 to 3.4 E+38 double 8 1.7 E-308 to 1.7 E+308 long double 10 3.4 E-4932 to 1.1 E+4932 IT 3rd Sem C++
  • 9. char Type Size Range char 1 -128 to +127 unsigned char 1 0 to +255 signed char 1 -128 to +127 void  size 0 bytes IT 3rd Sem C++
  • 10. Variable  Rules Variable Declaration data-type var1,var2; For eg: int a,b,c; float x; IT 3rd Sem C++
  • 11. Variable initialization data-type var1; var1=value; For eg: int a; a=2; Method 1: Method 2: data-type var1=value; For eg: int a=2; IT 3rd Sem C++
  • 12. C++ Derived Data Types:  Array  Structure  Union  Enumerations  Pointers  Class IT 3rd Sem
  • 13. C++ Enumerations:  set of values represented by identifiers Format: enum name{var1,var2,var3,----------, varn}; name n1,n2; n1=var1; n2=var3; cout<<n1; cout<<n2;  output will be 0 and 2 IT 3rd Sem
  • 14. C++ Operators: Type Operators Meaning Arithmetic + Addition − Subtraction * Multiplication / Division % Modulus (Remainder) Relational < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to IT 3rd Sem
  • 15. C++ Operators: Type Operators Meaning Logical && Logical AND || Logical OR ! Logical NOT Bitwise & Bitwise AND I Bitwise OR ^ Bitwise Exclusive-OR (XOR) >> Bitwise Shift Right << Bitwise Shift Left ~ Bitwise Complement IT 3rd Sem
  • 16. C++ Operators: Type Operators Meaning Assignment = Assignment += a+=b means a=a+b −= a−=b means a=a−b *= a*=b means a=a*b /= a/=b means a=a/b %= a%=b means a=a%b >>= a>>=n means a=a>>n <<= a<<=n means a=a<<n &= a&=b means a=a&b |= a|=b means a=a|b != a!=b means a=a!b IT 3rd Sem
  • 17. C++ Operators: Type Operators Meaning Special * Pointer & Address . Membership :: Scope Resolution size of() ?: Ternary Operator ++ Increment (pre and post) −− Decrement (pre and post) IT 3rd Sem
  • 18. C++ Operator Precedence: IT 3rd Sem Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right. !, +, - Logical NOT, signs Evaluated second. If there are several, they re evaluated right to left. *, /, or % Multiplication Division Modulus Evaluated third. If there are several, they re evaluated left to right. + or - Addition Subtraction Evaluated fourth. If there are several, they are evaluated left to right. >, <, >=, <= Relational Operators Evaluated fifth. If there are several, they are evaluated left to right. ==, != Equality Operators Evaluated sixth. If there are several, they are evaluated left to right. && Logical AND Evaluated seventh. If there are several, they are evaluated left to right. | | Logical OR Evaluated eighth. If there are several, they are evaluated left to right.
  • 20. C++ Type Conversion:  Implicit or Automatic  Explicit or type casting Syntax: (cast-type) expression; cast-type (expression); For eg: int a,b; float c; c = (float) a/b; IT 3rd Sem
  • 21. C++ Phases of C++ programs: IT 3rd Sem C++ PROGRAM DEVELOPMENT GOES THROUGH SIX STEPS: Step1: Edit (using text editor to type, correct and save the program file). Step2: preprocessor, automatically before compile, executes to include other text files in the file to be compiled. Step3: Compile , the compiler translates the C++ code into machine language (also called object-code). Step4: Link , it is linking any used functions that are defined elsewhere such as standard library functions, or private library for a group of programmers, linker, links the object code with the code for the missing functions to provide full code Step5: Load, a program before executing , must be loaded into the main memory, loader does this task , loading code from disk. Step6: Execute, the computer under the control of its CPU executes the program. Instruction by instruction.
  • 22. C++ IT 3rd Sem ILLUSTRATION OF C++ PROGRAM PHASES Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Loader main Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Editor Preprocessor Linker CPU main Memory . . . . . . . . . . . . Disk Disk Disk Disk Disk
  • 23. C++ Structure of C++ program IT 3rd Sem  Every C++ program must have a function named main. The programmer can choose to decompose the program into several parts (user-defined functions). Think of main as the master and the other functions are the servants.  The execution always starts with main.
  • 24. 1. Comments 2. Load <iostream.h> 3. main 3.1 Print "Welcome to C++n" 3.2 exit (return 0) 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++ 3 #include <iostream.h> 4 5 int main() 6 { 7 cout << "Welcome to C++!n"; 8 9 return 0; // indicate that program ended successfully 10 } Welcome to C++! preprocessor directive Message to the C++ preprocessor. Lines beginning with # are preprocessor directives. #include <iostream.h> tells the preprocessor to include the contents of the file <iostream.h>, which includes input/output operations (such as printing to the screen). Comments Written between /* and */ or following a //. Improve program readability and do not cause the computer to perform any action. C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main "returns" an integer value. Prints the string of characters contained between the quotation marks. The entire line, including cout, the << operator, the string "Welcome to C++!n" and the semicolon (;), is called a statement. All statements must end with a semicolon. return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally. A left brace { begins the body of every function and a right brace } ends it.
  • 25. C++ A Simple Program: Printing a Line of Text IT 3rd Sem cout Standard output stream object “Connected” to the screen << Stream insertion operator Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) cout << “Welcome to C++!n”; Escape character Indicates that a “special” character is to be output
  • 26. C++ A Simple Program: Printing a Line of Text IT 3rd Sem Escape Sequence Description n Newline. Position the screen cursor to the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. a Alert. Sound the system bell. Backslash. Used to print a backslash character. " Double quote. Used to print a double quote character.  There are multiple ways to print text  Following are more examples
  • 27. C++ IT 3rd Sem 1. Load <iostream.h> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output Welcome to C++! 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 3 #include <iostream.h> 4 5 int main() 6 { 7 cout << "Welcome "; 8 cout << "to C++!n"; 9 10 return 0; // indicate that program ended successfully 11 } Unless new line 'n' is specified, the text continues on the same line.
  • 28. C++ IT 3rd Sem 1. Load <iostream.h> 2. main 2.1 Print "Welcome" 2.2 newline 2.3 Print "to" 2.4 newline 2.5 newline 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0) Program Output 1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream.h> 4 5 int main() 6 { 7 cout << "WelcomentonnC++!n"; 8 9 return 0; // indicate that program ended successfully 10 } Welcome to C++! Multiple lines can be printed with one statement.
  • 29. C++ Another Program: Adding Two Integers IT 3rd Sem >> (stream extraction operator) When used with cin, waits for the user to input a value and stores the value in the variable to the right of the operator The user types a value, then presses the Enter (Return) key to send the data to the computer Example: int myVariable; cin >> myVariable; Waits for user input, then stores input in myVariable = (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;
  • 30. 1. Load <iostream> 2. main 2.1 Initialize variables integer1, integer2, and sum 2.2 Print "Enter first integer" 2.2.1 Get input 2.3 Print "Enter second integer" 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output 1 // Fig. 1.6: fig01_06.cpp 2 // Addition program 3 #include <iostream.h> 4 5 int main() 6 { 7 int integer1, integer2, sum; // declaration 8 9 cout << "Enter first integern"; // prompt 10 cin >> integer1; // read an integer 11 cout << "Enter second integern"; // prompt 12 cin >> integer2; // read an integer 13 sum = integer1 + integer2; // assignment of sum 14 cout << "Sum is " << sum << endl; // print sum 15 16 return 0; // indicate that program ended successfully 17 } Enter first integer 45 Enter second integer 72 Sum is 117 Variables can be output using cout << variableName. endl flushes the buffer and prints a newline. Notice how cin is used to get user input. C++
  • 31. C++ Built-in (Library) Function IT 3rd Sem Function Exponentiation pow (x, y) x is raised to power y; (xy ) sqrt (x) Square-root of x sin (x) Sine of x cos (x) Cosine of x tan (x) Tangent of x exp (x) Exponentiation of x, ( ex) fabs (x) Absolute Value of x, | x | log (x) Logarithm of x (base e) log10 (x) Logarithm of x (base 10) floor (x) Rounding-down x ceil (x) Rounding-up x ______etc. {there are more}_______________________ Eg.1. floor (9.2) = 9.0 Eg.2. floor (-9.8) = -10.0 Remark:you need to include <math.h> to be able to use these functions. In newer versions it is #include<cmath>
  • 32. C++ Common programming Errors IT 3rd Sem Divide by zero. Not including iostream for input/output operations. Forgetting the (;) at end of each statement. If a space found between the pair-of-symbols for the relational operators. (i.e., > = instead of >= ). Confusing the equality == with the assignment =. Reversing the order of the relational operators (i.e., => instead of >=).
  • 33. C++ Formatted Output IT 3rd Sem setw can be used to specify the width of the field that the next value of output will be printed in. To use it, you must include <iomanip.h> header file. eg1:- int num=12; cout<< setw(4) << num; //num will be printed in a field width of 4 character. eg2:- int n1=12; int n2=197; cout<<setw(5)<< n1 << setw(6) << n2; eg3:- (if the value is more than the specified setw value the compiler ignores the setw effect and print it with the minimum number of positions required.) int num=1977; cout<<setw(3)<<num;
  翻译: