Programming in C++ - Module 01
Programming in C++ - 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
Module 01
Organization
Build Process
References
Build Pro
cess
References
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 4
First C program
Module 01
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
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
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
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
Module 01
Build Process
References
References
Build Process
References
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
References
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
References
References return 0 ;
}
Summary NPTEL MOOCs Programming in C++ Partha Pratim Das 26
Functions
return 0 ;
Organization }
Build Process
References
Module 01
To write to or read from file:
Partha Pratim #include < std i o. h >
Das
i n t main() {
Std Library
Organization
Build Process
References
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
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
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; }
Module 01
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
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