0% found this document useful (0 votes)
19 views

Programming in C++ - Module 01

Uploaded by

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

Programming in C++ - Module 01

Uploaded by

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

Module 01

Partha Pratim
Das Module 01: Programming in C++
Objectives
& Outline
Recap of C
Recap of
C Data
Types
Variables Partha Pratim Das
Literals
Operators
Expressions Department of Computer Science and Engineering
Statements
Control Flow
Indian Institute of Technology, Kharagpur
Arrays
Structures [email protected]
Unions
Pointers
Functions
InputLibrary
Std / Output Tanwi Mallick
Srijoni Majumdar
Organization
Himadri B G S
Build Process Bhuyan
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 01

Partha Pratim Revisit the concepts of C language


Das
Revisit C Standard Library components
Objectives
& Outline Revisit the Organization and Build
Recap of Process for C programs
C Data
Types
Variables
Create the foundation for the concepts of C++
Literals
Operators
with backward compatibility to C
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
InputLibrary
Std / Output

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline

Module 01 Recap of C features


Partha Pratim Data types
Das
Variables
Objectives Literals
& Outline Operators
Recap of Expressions
C Data
Types
Variables
Literals
Statements
Operators
Expressions
Control
Statements Constructs
Control Flow
Arrays –
Structures
Unions
Conditional
Pointers Flow &
Functions
InputLibrary
Std / Output Loops
Organization
Arrays
Build Process
Structures & Unions
Pointers
References
Functions
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 3
Input / Output
Module 01: Lecture
01
Module 01 Recap of C features
Partha Pratim Data types
Das
Variables
Objectives Literals
& Outline Operators
Recap of Expressions
C Data
Types
Variables
Literals
Statements
Operators
Expressions
Control
Statements Constructs
Control Flow
Arrays –
Structures
Unions
Conditional
Pointers Flow &
Functions
InputLibrary
Std / Output Loops
Organization

Build Pro

cess

References
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 4
First C program

Module 01

Partha Pratim Print ”Hello World”


Das
Source Program
Objectives
& Outline #include < std i o. h >

Recap of i n t main() {
C Data
Types p r i n tf ( " H e l l o World");
Variables p r i n tf ( " \ n " ) ;
Literals
Operators return 0 ;
Expressions }
Statements
Control Flow
Arrays • s t d i o . h header included for input / output
Structures • main function is used to start execution
Unions • p r i n tf function is used to print the string ”Hello World”
Pointers
Functions
InputLibrary
Std / Output

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Data Types

Module 01
Data types in C are used for declaring variables and deciding
Partha Pratim
Das
on storage and computations:

Objectives
Built-in / Basic data types are used to define raw data
& Outline
char
Recap of
C Data int
Types
Variables
fl o a t
Literals double
Operators
Expressions
Statements Additionally,
Control Flow
Arrays
C99 defines:
Structures
Unions bool
Pointers
Functions
InputLibrary
/ Output
All data items of a given type has the same size (in bytes).
Std
The size is implementation-defined.
Organization

Build Process Enumerated Type data are internally of i n t type and operates
References on a select subset.
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 6
Data Types

Module 01 Data types in C further include:


Partha Pratim void: The type specifier void indicates no type.
Das
Derived data types include:
Objectives
& Outline
Array
Recap of
C Data
Structure – s t r u c t &
Types union
Variables
Literals Pointer
Operators
Expressions
Function
Statements
Control Flow
String – C-
Arrays Strings
Structures
Unions are
Pointers
Functions
really
InputLibrary
Std / Output not a
Organization type;
Build Process
but can
References
be made
unsigned
Summary
to behave as such using functions from <st rin g.h > in
NPTEL MOOCs Programming in C++ Partha Pratim Das 7
Variables

Module 01 A variable is a name given to a storage


Partha Pratim
Das area Declaration of Variables:

Objectives
Each variable in C has a specific type, which determines
& Outline the size and layout of the storage (memory) for the
Recap of variable The name of a variable can be composed of
C Data
Types letters, digits, and the underscore character. It must begin
Variables
Literals
with either a letter or an underscore
Operators
Expressions
Statements int i, j , noOfData;
Control
Flow char c, endOfSession;
Arrays
Structures
fl o a t f, velocity;
Unions
Input / Output
Pointers
double d, dist_in_light_years;
Functions
Std Library

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 8


Variables

Module 01 Initialization of Variables:


Partha Pratim
Das
Initialization is setting an initial value to a variable at its
definition
Objectives
& Outline

Recap of
int i = 10, j = 20, numberOfWorkDays = 22;
C Data char c = ’x’;
Types
Variables
fl o a t weight = 4 . 5 ;
Literals double d ensit y = 0 . 0 ;
Operators
Expressions
Statements
Control
Flow
Arrays
Structures
Unions
Input / Output
Pointers
Functions
Std Library

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 9


Literals

Module 01

Partha Pratim Literals refer to fixed values of a built-in type


Das
Literals can be of any of the basic data
Objectives
& Outline types
212 // ( i n t ) Decimal l i t e r a l
0173 // ( i n t ) Octa l l i t e r a l
Recap of 0b1010 // ( i n t ) Binary l i t e r a l
C Data 0xF2 // ( i n t ) Hexadecimal l i t e r a l
Types 3.14 // (double) F l o ati n g - p o i n t l i t e r a l
Variables ’x’ // ( c h a r ) Character l i t e r a l
Literals " H e ll o" // (char *) St ring l i t e r a l
Operators
Expressions
Statements In C99, literals are constant values having const types as:
Control Flow
Arrays 212 // ( c on st i n t ) Decimal l i t e r a l
Structures 0173 // ( c on st i n t ) Octa l l i t e r a l
Unions 0b1010 // ( c on st i n t ) Binary l i t e r a l
Pointers 0xF2 // ( c on st i n t ) Hexadecimal l i t e r a l
Functions 3.14 // ( c on st double) F l o ati n g - p o i n t l i t e r a l
InputLibrary
Std / Output
’x’ // ( c on st c h a r) Character l i t e r a l
" H e ll o" // ( c on st char * ) S t r i n g l i t e r a l
Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Operator
s
Module 01 An operator denotes a specific operation. C has the following
Partha Pratim types of operators:
Das
Arithmetic Operators: + – * / % ++ – –
Objectives Relational Operators: == != > < >= <=
& Outline Logical Operators: & & || !
Recap of Bit-wise Operators: & | ˜ << >>
C Data Assignment Operators: = += –= *= / = · ·
Types ·
Variables
Literals Miscellaneous Operators: . , s i z e o f & * ?:
Operators Arity of Operators: Number of operand(s) for an operator
Expressions
Statements +, –, *, & operators can be unary (1 operand) or binary (2 operands)
Control Flow
Arrays
==, !=, >, <, >=, <=, &&, ||, +=, –=, *=, =, /=, &, |, <<,
Structures >> can work only as binary (2 operands) operators
Unions
Pointers
s i z e o f ! ˜ ++ – – can work only as unary (1 operand) operators
Functions ?: works as ternary (3 operands) operator. The condition is the first
InputLibrary
Std / Output
operand and the i f true logic and i f false logic corresponds to the
Organization
other two operands.
Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 11


Operator
s
Module 01 Operator Precedence: Determines which operator will be
Partha Pratim performed first in a chain of different operators
Das The precedence of all the operators mentioned above is in the
following order: (left to right – Highest to lowest precedence)
Objectives
& Outline (), [], ++, –, + (unary), –(unary), !˜, *, &, sizeof, *, /, %, +,
–, <
Recap of
C Data <, > >, ==, !=, *=, =, /=, &, |, &&, | |, ?:, =, +=, –=,
Types *=, =,
Variables
Literals
Operator
/=, <Associativity:
<=, > >= Indicates in what order operators of
Operators
Expressions
equal precedence in an expression are applied
Statements
Control Flow Consider the expression a ˜ b ˜ c. If the operator ˜ has left
Arrays
Structures associativity, this expression would be interpreted as (a ˜ b) ˜
Unions
Pointers
c. If the operator has right associativity, the expression would
Functions
InputLibrary
/ Output
be interpreted as a ˜ (b ˜ c).
Std
Right-to-Left: ?:, =, +=, -=, *=, =, /=, <<=, >>=, –, +-, !˜, *,
Organization
&, sizeof
Build Process
Left-to-Right: *, /, %, +, -, <<, >>, ==, !=, *=, =, /=, &, |,
References &&, | |
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 12
Expression
s
Module 01 Every expression has a value
Partha Pratim
Das
A literal is an expression
A variable is an expression
Objectives
& Outline
One, two or three expression/s connected by an operator (of
Recap of
appropriate arity) is an expression
C Data A function call is an expression
Types
Variables
Literals
Examples:
Operators
Expressions
For
Statements i n t i = 10, j = 20, k ;
Control Flow i n t f ( i n t x , i n t y ) { return x + y ; }
Arrays
Structures
Expression are:
Unions 2. 5 / / Value = 2. 5
Pointers i / / Value 10
Functions -i / / Value -10
InputLibrary
Std / Output i - j / / Value -10
k =5 / / Value 5
Organization f(i, j) / / Value 30
i + j == i * 3 / / Value t rue
Build Process ( i == j ) ? 1 : 2 / / Value 2
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 13


Statement

Module 01 A statement is a command for a specific action. It has no


Partha Pratim value A ; (semicolon) is a (null) statement
Das
An expression terminated by a ; (semicolon) is a statement A
Objectives list of one or more statements enclosed within a pair of
& Outline curly braces { and } or block is a compound
Recap of Control constructs like i f , i f - e l s e , switch, for,
statement
C Data
Types while, do-while, goto, conti nue, break, return are
Variables
Literals
statements
Operators
Expressions Example: Expression statements
Statements Expressions Statements
Control Flow i + j i + j;
Arrays k =i +j k =i +j;
Structures
funct(i,j) funct(i,j);
Unions
k = k =
Pointers
funct(i,j) funct(i,j);
Functions
InputLibrary
/ Output
Example: Compound statements
Std {
Organization int i = 2, j = 3, t ;

Build Process t =i;


i =
References j; j
} = t;
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 14
Control Constructs

Module 01 These statements control the flow based on conditions:


Partha Pratim
Das
Selection-statement: i f , i f - e l s e , switch
Labeled-statement: Statements labeled with identifier,
Objectives
& Outline
case, or d efa u l t
Recap of
Iteration-statement: for, while, do-while
C Data Jump-statement: goto, conti nue, break, return
Types
Variables
Literals
Examples:
Operators i f (a < b) i f ( x < 5) switch ( i ) {
Expressions { int t ; x = x + case 1 : x = 5 ;
Statements 1; else { bre ak ;
Control Flow t = a; x = x case 3 : x = 10;
Arrays a = b; + 2; d e f a u l t : x = 15;
Structures b= t; --y; }
Unions } }
Pointers
Functions
InputLibrary
/ Output i n t sum = 0 ; while ( n ) { int f(int x, int y)
Std
fo r ( i = 0; i < 5; ++i) sum += n ; {
Organization { int j = i * i ; i f (sum > 20) return x + y ;
sum += j ; bre ak ; }
Build Process } --n;
}
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 15


Module 01: End of Lecture
01
Module 01 Recap of C features
Partha Pratim Data types
Das
Variables
Objectives Literals
& Outline Operators
Recap of Expressions
C Data
Types
Variables
Literals
Statements
Operators
Expressions
Control
Statements Constructs
Control Flow
Arrays –
Structures
Unions
Conditional
Pointers Flow &
Functions
InputLibrary
Std / Output Loops
Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 16


Module 01: Lecture
02
Module 01 Recap of C features
Partha Pratim Arrays
Das Structures
Objectives Unions
& Outline Pointers
Recap of
C Data
Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Pro

cess
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 17
Array
s
Module 01 An array is a collection of data items, all of the same
Partha Pratim type, accessed using a common name
Das
Declare Arrays:
#defi ne S I Z E 10
Objectives i n t name[SIZE]; / / S I Z E must be an i n te g e r constant g reater than
& Outline zero double ba la nce [10];
Recap of
C Data
Initialize Arrays:
i n t primes[5] = { 2 , 3 , 5 , 7 , 1 1 } ; / / S i z e = 5
Types
Variables
i n t prime s [] = { 2 , 3 , 5 , 7 , 1 1 } ;
Literals
Operators
i n t sizeOfPrimes = s i z e o f ( p r i m e s ) / s i z e o f ( i n t ) ; / / s i z e i s 5 by i n i ti a l i z a ti o n
Expressions
Statements
i n t primes[5] = { 2 , 3 } ; / / S i z e = 5 , l a s t 3 elements s e t to 0
Control Flow
Arrays Access Array elements:
Structures i n t primes[5] = { 2 , 3 } ;
Unions i n t EvenPrime = p ri m e s [ 0 ]; / / Read 1 s t element
Pointers primes[2] = 5 ; / / Write 3rd element
Functions
InputLibrary
Std / Output
Multidimensional Arrays:
i n t mat[3][4];
Organization

Build Process fo r ( i = 0; i < 3; ++i)


for( j = 0; j < 4; ++j)
References mat[i][j] = i + j ;

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 18


Structures

Module 01 A structure is a collection of data items of different types. Data


Partha Pratim items are called members. The size of a structure is the sum of
Das the size of its members.
Declare Structures:
Objectives
s t r u c t Complex { / / Complex Number
& Outline / / Real component
double
Recap of r e ; double / / Imaginary component
} c ; im; / / c i s a v a r i a b l e of s t r u c t Complex type
C Data
p r i n tf ( " s i z e = %d\n", s i z e o f ( s t r u c t Complex)); / / P r i n t s : s i z e = 16
Types
Variables
typedef s t r u c t _Books
Literals
{ char ti t l e [ 5 0 ] ; / / data member
Operators
char a ut hor[50]; / / data member
Expressions
int book_id; / / data member
Statements
Control Flow
} Books; / / Books i s an a l i a s f o r s t r u c t _Books type
Arrays
Structures Initialize Structures:
Unions s t r u c t Complex x = { 2 . 0 , 3 . 5 } ; / / Both members
Pointers s t r u c t Complex y = { 4 . 2 } ; / / Only the fi r s t member
Functions
InputLibrary
Std / Output Access Structure members:
s t r u c t Complex x = { 2 . 0 , 3 . 5 } ;
Organization double norm = s q r t ( x . r e * x . r e + x . i m * x . i m ) ; / / Using . ( d o t ) operator
Build Process
Books book;
References book.book_id = 6495407;
s t r c p y ( b o o k . ti t l e , "C
Summary NPTEL MOOCs Programming");
Programming in C++ Partha Pratim Das 19
Unions

Module 01 A union is a special structure that allocates memory only for


Partha Pratim
the largest data member and holds only one member as a time
Das Declare Union:
typedef union _Packet { / / Mixed Data Packet
int iData; / / i n te g e r data
Objectives
double dData; / / fl o a ti n g point data
& Outline
char cDat a ; / / cha ra cter data
Recap of } Packe t ;
p r i n tf ( " s i z e = %d\n", s i z e o f ( P a c ke t ) ) ; / / P r i n t s : s i z e = 8
C Data
Types
Variables Initialize Union:
Literals
Operators
Packer p = { 1 0 } ; / / I n i ti a l i z e only with a value o f the type o f fi r s t member
Expressions
p r i n tf ( " i D a t a = %d\n", p . i D a t a ) ; / / P r i n t s : iD ata = 10
Statements
Control Flow
Arrays Access Union members:
Structures p . i D ata = 2 ;
Unions p r i n tf ( " i D a t a = %d\n", p . i D a t a ) ; / / P r i n t s : iD ata = 2
Pointers p.dData = 2 . 2 ;
Functions p ri n tf ( " d D ata = % l f \ n " , p. dD ata); / / P r i n t s : dData =
InputLibrary
Std / Output 2.200000
p.cData = ’ a ’ ;
Organization p r i n tf ( " c D a t a = %c\n", p . c D a ta ) ; / / P r i n t s : cData = a
p . i D ata = 97;
Build Process p r i n tf ( " i D a t a = %d\n", p . i D a t a ) ; / / P r i n t s : iD ata = 97
p ri n tf ( " d D ata = % l f \ n " , p. dD ata); / / P r i n t s : dData = 2.199999
References
p r i n tf ( " c D a t a = %c\n", p . c D a ta ) ; / / P r i n t s : cData = a
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 20
Pointers

Module 01 A pointer is a variable whose value is a memory address


Partha Pratim
Das
The type of a pointer is determined by the type of its
pointee / / pointer to an i n te g e r
int *ip;
Objectives double *dp; / / pointer to a double
& Outline fl o a t * f p ; / / pointer to a fl o a t
char *ch / / pointer to a cha ra cter
Recap of
C Data
Types Using a pointer:
Variables
Literals i n t main() {
Operators i n t i = 20; / / v a r i a b l e d e c la rati on
Expressions int *ip; / / pointer d e c la rati on
Statements ip = &i; / / store address of i i n pointer
Control Flow
Arrays p ri n tf ( " A d d re s s of v a r i a b l e : %p\n", & i ) ; / / P r i n t s : Address o f v a r i a b l e : 00A8F73C
Structures
Unions p r i n tf ( " Va l u e of p o i n t e r : %p\n", i p ) ; / / P r i n t s : Value o f pointer : 00A8F73C
Pointers
Functions p r i n tf ( " Va l u e of pointee : %d\n", * i p ) ; / / P r i n t s : Value o f pointee : 20
InputLibrary
Std / Output
return 0 ;
Organization }
Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 21


Pointers

Module 01
• Pointer-Array Duality • Pointer to a structure
Partha Pratim int a[] = {1, 2, 3, 4, 5}; s t r u c t Complex { / / Complex Number
Das i n t *p; double r e ; / / Real component
double im; / / Imaginary component
p = a; } c = { 0 . 0 , 0. 0 };
Objectives
p r i n tf ( " a [ 0 ] = %d\n", * p ) ; // a[0] = 1
& Outline s t r u c t Complex *p = &c;
p r i n tf ( " a [ 1 ] = %d\n", *++p); // a[1] = 2
Recap of p r i n tf ( " a [ 2 ] = %d\n", * ( p + 1 ) ) ; / / a [ 2 ] = 3
(*p).re = 2.5;
C Data
p = &a [2]; p->im = 3 . 6 ;
Types
*p = - 1 0 ;
Variables
p r i n tf ( " a [ 2 ] = %d\n", a [ 2 ] ) ; / / a [ 2 ] = -10 p r i n tf ( " r e = % l f \ n " , c . r e ) ; / / re = 2.500000
Literals
p r i n tf ( " i m = % l f \ n " , c . i m ) ; / / im = 3.600000
Operators
Expressions
Statements
Control Flow • malloc-free • Dynamically allocated arrays
Arrays i n t *p = ( i n t * ) m a l l o c ( s i z e o f ( i n t ) ) ; i n t *p = ( i n t * ) m a l l o c ( s i z e o f ( i n t ) * 3 ) ;
Structures
Unions *p = 0x8F7E1A2B; p[0] = 1; p[1] = 2; p[2] = 3;
Pointers p ri n tf ( " % X \n " , * p ) ; / / 8F7E1A2B
Functions
p r i n tf ( " p [ 1 ] = %d\n", * ( p + 1 ) ) ; / / p [ 1 ] = 2
InputLibrary
Std / Output
unsigned char *q = p ; free(p);
p ri n tf ( " % X \n " , *q++); // 2B
Organization p ri n tf ( " % X \n " , *q++); // 1A
p ri n tf ( " % X \n " , *q++); // 7E
Build Process
p ri n tf ( " % X \n " , *q++); // 8F
References
free(p);
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 22
Module 01: End of Lecture
02
Module 01 Recap of C features
Partha Pratim Arrays
Das Structures
Objectives Unions
& Outline Pointers
Recap of
C Data
Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Pro

cess
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 23
Module 01: Lecture
03
Module 01 Recap of C features
Partha Pratim Functions
Das Input / Output
Objectives C Standard
& Outline

Recap of
Library
C Data
Types
Source Organization for a C program
Variables
Literals Build Process
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
InputLibrary
Std / Output

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 24


Functions

Module 01 A function performs a specific task or


Partha Pratim computation Has 0, 1, or more parameters /
Das
arguments. Every argument has a type (void for
Objectives no argument)
& Outline
May or may not return a result. Return value has a type
Recap of
(void for no result)
C Data
Types Function declaration:
/ / Functi on Prototype / Header / Sig nature
Variables / / Name of the f u n c ti o n : funct
Literals / / Parameters: x and y. Types of parameters: i n t
Operators / / Return t y p e : i n t
Expressions
Statements int funct(int x , int y ) ;
Control Flow
Arrays
Structures
Function definition:
Unions
Pointers / / Functi on Implementati on
Functions int funct(int x , int y)
InputLibrary
Std / Output
/ / Functi on Body
Organization {
return ( x + y ) ;
Build Process }

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 25


Functions

Module 01 Call-by-value mechanism for passing arguments. The value of


Partha Pratim
an actual parameter copied to the formal parameter
Das
Return-by-value mechanism to return the value, if any.
Objectives int funct(int x , int y) {
& Outline ++x; ++y; / / Formal parameters changed
return ( x + y ) ;
Recap of
}
C Data
Types i n t main() {
Variables i n t a = 5 , b = 10, z ;
Literals
Operators p r i n tf ( " a = %d, b = %d\n", a , b ) ; / / p r i n t s : a = 5 , b = 10
Expressions
Statements z = funct(a, b); // functi on c a l l by value
Control Flow // a copied to x . x becomes 5
Arrays
// b copied to y. y becomes 10
Structures
// x i n funct changes to 6 ( + + x )
Unions
// y i n funct changes to 11 ( + + y )
Pointers
// return value ( x + y ) copied to z
Functions
InputLibrary
Std / Output
p r i n tf ( " f u n c t = %d\n", z ) ; / / p r i n t s : funct = 17
Organization
/ / A ct ual parameters do not change on return ( c a l l - b y - v a l u e )
Build Process p r i n tf ( " a = %d, b = %d\n", a , b ) ; / / p r i n t s : a = 5 , b = 10

References return 0 ;
}
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 26
Functions

Module 01 A function may be recursive (call itself)


Partha Pratim
Das
Has recursive step/s
Has exit condition/s
Objectives
& Outline Example:
Recap of
/ / F a c t o r i a l of n
C Data unsigned i n t fa c t o r i a l ( u n s i g n e d i n t n )
Types { i f (n > 0)
Variables return n * f a c t o r i a l ( n - 1 ) ; / /
Literals Recursive step
Operators else return 1 ; / / E x i t conditi on
Expressions
}
Statements
Control Flow
/ / Number of 1 ’s i n the binar y representati on of n
Arrays
unsigned i n t nOnes(unsigned i n t n ) {
Structures
i f ( n == 0 )
Unions
Pointers
return 0 ; / / E x i t conditi on
Functions e l s e / / Recursive ste ps
InputLibrary
/ Output i f ( n % 2 == 0 )
Std return nOnes(n / 2 ) ;
else
Organization
return nOnes(n /
Build Process 2) + 1;
}
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 27


Function pointers

Module 01 #include < std i o. h >


DrawFunc DrawArr[] = { / / Array o f f u n c . p t r s
s t r u c t GeoObject {
draw C ir, drawRec, drawTrg } ;
Partha Pratim enum { CIR =
Das 0 , REC, TRG }
gCode; i n t main() {
union { s t r u c t GeoObject go;
Objectives s t r u c t C i r { double x , y, r ; } c ;
& Outline go.gCode = C I R ;
s t r u c t Rec { double x , y, w, h ; } r ;
g o . c . x = 2 . 3 ; g o . c .y = 3 . 6 ;
s t r u c t Trg { double x , y, b , h ; } t ;
Recap of }; go.c.r = 1.2;
C Data }; DrawArr[go.gCode](go); / / C a l l by p t r
typedef void(*DrawFunc) ( s t r u c t GeoObject);
Types
Variables go.gCode = REC;
void d ra w C i r( st ru c t GeoObject go)
Literals g o . r. x = 4 . 5 ; g o . r. y = 1 . 9 ;
{ p r i n tf ( " C i r c l e : ( % l f, % l f, % l f ) \
Operators go. r.w = 4 . 2 ; g o . r. h = 3 . 8 ;
n",
Expressions DrawArr[go.gCode](go); / / C a l l by p t r
g o . c . x , g o . c . y, g o . c . r ) ; }
Statements
void drawRec(struct GeoObject go)
Control Flow go.gCode = TRG;
{ p r i n tf ( " R e c t : ( % l f, % l f, % l f, % l f ) \
Arrays go.t . x = 3. 1; go.t .y = 2. 8;
n",
Structures go.t.b = 4.4; go.t.h = 2.7;
g o . r. x , g o . r. y, g o . r.w, g o . r. h ) ; }
Unions DrawArr[go.gCode](go); / / C a l l by p t r
void drawTrg(struct GeoObject go)
Pointers
{ p r i n tf ( " Tr i a g : ( % l f, % l f, % l f, % l f ) \
Functions return 0 ;
n",
InputLibrary
Std / Output }
g o . t . x , g o . t . y, g o . t . b , g o . t . h ) ; }
Organization
C i r c l e : (2.300000, 3.600000, 1.200000)
Build Process Re c t : (4.500000, 1.900000, 4.200000, 3.800000)
Tr i a g : (3.100000, 2.800000, 4.400000, 2.700000)
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 28


Input / Output

Module 01 i n t p r i n tf ( c o n s t char *format, . . . ) writes to stdout


Partha Pratim by the format and returns the number of characters written
Das
i n t sca nf(co nst char *format, . . . ) reads from s t d i n
Objectives
& Outline
by the format and returns the number of characters read
Recap of Use %s, %d, %c, %lf, to print/scan string, i n t , char, double
C Data
Types #include < std i o. h >
Variables
Literals i n t main() {
Operators
Expressions char
Statements str[100]; int
Control Flow i;
Arrays p r i n tf ( " E n t e r a value : " ) ; / / p r i n t s a constant s t r i n g
Structures
Unions
scanf("%s %d", s t r, & i ) ; / / scans a s t r i n g value and an i n te g e r value
Pointers
Functions
p r i n tf ( " Yo u entered: %s %d " , s t r, i ) ; / / p r i n t s s t r i n g and i n te g e r
InputLibrary
Std / Output

return 0 ;
Organization }
Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 29


Input / Output

Module 01
To write to or read from file:
Partha Pratim #include < std i o. h >
Das
i n t main() {

Objectives F I L E *fp = NULL;


& Outline int i ;
Recap of f p = fop e n ( " I n p u t . d at " , " r " ) ;
C Data f s c a n f ( f p , "%d", / / scan from I n p u t . d at
Types &i); fclose(fp);
Variables
Literals f p = fopen("Output.dat", " w " ) ;
Operators fprintf ("% d^ 2 = %d\n", i , i * i ) ; / / p r i n t s to Output.dat
Expressions fclose(fp);
Statements
Control return 0 ;
Structures
Flow Arrays }
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Process

References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 30


C Standard Library

Common Library Components:


Module 01

Partha Pratim
Das Component Data Types, Manifest Constants, Macros, Functions, ...
stdio.h Formatted and un-formatted file input and output including
Objectives & functions
Outline
• p r i n tf , scanf, f p r i n tf , fsca nf , s p r i n tf , sscanf, feof,
Recap of etc.
C Data
Types
stdlib.h Memory allocation, process control, conversions, pseudo-
Variables random numbers, searching, sorting
Literals • malloc, free, e x i t , abort, at o i , s t r t o l d , rand,
Operators
Expressions
bsearch, qsort, etc.
Statements string.h Manipulation of C strings and arrays
Control
Flow
• s t r c a t , strcpy, strcmp, s t r l e n , st r t o k , memcpy,
Arrays memmove, etc.
Structures
Functions
math.h Common mathematical operations and transformations
Unions
Input / Output
Pointers • cos, s i n , tan, acos, asin, atan, exp, log , pow, sqrt, etc.
Std Library errno.h Macros for reporting and retrieving error conditions through
error codes stored in a static memory location called errno
Organization
• EDOM (parameter outside a function’s domain – sqrt(-1)),
Build Process • ERANGE (result outside a function’s range), or
References • EILSEQ (an illegal byte sequence), etc.
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 31
Source Organization for a C
program
Module 01
Header Files
Partha Pratim
Das A header file has extension . h and contains C function
Objectives
declarations and macro definitions to be shared between several
& Outline source files
Recap of
C Data There are two types of header files:
Types
Variables Files that the programmer writes
Literals
Operators Files from standard library
Expressions
Statements
Control Flow Header files are included using the #include pre-processing
Arrays
Structures
directive
Unions
Pointers #include < fi l e > for system header files
Functions
InputLibrary
Std / Output #include " fi l e " for header files of your own
Organization
program
Build Pro

cess

References
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 32
Source Organization for a C
program
Module 01

Partha Pratim
Example:
Das
/ / S o l v e r. h - - Header fi l e s
i n t quadrati cEquati onSolver(double, double, double, double*, double*);
Objectives
& Outline / / S o l v e r. c - - Implementati on fi l e s
#include " S o l v e r. h "
Recap of
C Data i n t quadrati cEquati onSolver(double a , double b , doublec , double* r 1 , double* r 2 ) {
Types // . . .
Variables // . . .
Literals // . . .
Operators return 0 ;
Expressions }
Statements
Control Flow / / main.c - - A p p li c ati on fi l e s
Arrays #include " S o l v e r. h "
Structures
Unions
i n t main() {
Pointers
double a , b ,
Functions
c ; double r 1 ,
InputLibrary
Std / Output
r2;
i n t s t a t u s = qua drati cEquati onSolve r(a, b , c , &r1, &r2);
Organization
return 0 ;
Build Process
}
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 33


Build Flow

Module 01

Partha Pratim
Das

Objectives
& Outline

Recap of
C Data
Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Process
References

Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 34


Build Process

Module 01
C Pre-processor (CPP) substitutes and includes functions,
Partha Pratim
Das headers and macros before compilation
i n t s u m ( in t ,
Objectives i n t ) ; i n t main() {
& Outline int a =
s um(1,2);
Recap of return a ;
C Data }
Types
Variables
The compiler translates the pre-processed C code into assembly
Literals
Operators
language, which is a machine level code that contains
Expressions instructions that manipulate the memory and processor directly
Statements
Control Flow
Arrays
The linker links our program with the pre-compiled libraries for
Structures
Unions
using their functions
Pointers
Functions
In the running example, f u n c ti o n . c and main.c are
InputLibrary
Std / Output first compiled and then linked
Organization i n t sum(int a , i n t b ) { return a+b; }

Build Process i n t main() {


i n t a = s um(1,2); / / as fi l e s are l i n k e d , uses functi ons d i r e c t l y
References return a ;
}
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 35
Tools

Module 01

Partha Pratim Development IDE: Code::Blocks 16.01


Das
Compiler: -std=c++98 and -
Objectives
& Outline std=c99
Recap of
C Data
Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Pro

cess
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 36
References

Module 01

Partha Pratim
Das Kernighan, Brian W., and Dennis M. Richie. The C
Programming Language. Vol. 2. Englewood Cliffs:
Objectives
& Outline Prentice-Hall, 1988.
Recap of
C Data
King, Kim N., and Kim King. C programming: A Modern
Types
Variables
Approach. Norton, 1996.
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
InputLibrary
Std / Output

Organization

Build Pro

cess

References
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 37
Module Summary

Module 01

Partha Pratim Revised the concept of variables and literals in C


Das
Revised the various data types and operators of
Objectives
& Outline C Re-iterated through the control constructs of
Recap of
C Data
C
Types
Variables Re-iterated through the concepts of functions and
Literals
Operators pointers of C
Expressions
Statements
Control Flow
Re-iterated through the program organization of C and
Arrays
Structures
the build process.
Unions
Pointers
Functions
InputLibrary
Std / Output

Organization

Build Pro

cess

References
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 38
Instructor and TAs

Module 01

Partha Pratim
Das Name Mail Mobile
Objectives
Partha Pratim Das, Instructor [email protected] 9830030880
& Outline Tanwi Mallick, TA [email protected] 9674277774
Recap of
Srijoni Majumdar, TA [email protected] 9674474267
C Data Himadri B G S Bhuyan, TA [email protected] 9438911655
Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output

Std Library

Organization

Build Pro

cess
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 39

You might also like