Module 01
Partha Pratim
Das
Module 01: Programming in C++
Objectives &
Outline
Recap of C
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
Std Library
Organization
Partha Pratim Das
Department of Computer Science and Engineering
Indian Institute of Technology, Kharagpur
[email protected]Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Module Objectives
Module 01
Partha Pratim
Das
Revisit the concepts of C language
Revisit C Standard Library components
Objectives &
Outline
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
Revisit the Organization and Build Process for C programs
Create the foundation for the concepts of C++ with
backward compatibility to C
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Module Outline
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
Recap of C features
Data types
Variables
Literals
Operators
Expressions
Statements
Control Constructs Conditional Flow & Loops
Arrays
Structures & Unions
Pointers
Functions
Input / Output
Std Library
C Standard Library
Organization
Source Organization for a C program
Build Process
References
Build Process
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Module 01: Lecture 01
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
Recap of C features
Data types
Variables
Literals
Operators
Expressions
Statements
Control Constructs Conditional Flow & Loops
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
First C program
Module 01
Partha Pratim
Das
Print Hello World
Source Program
Objectives &
Outline
#include <stdio.h>
Recap of C
int main() {
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
printf("Hello World");
printf("\n");
return 0;
}
stdio.h header included for input / output
main function is used to start execution
printf function is used to print the string Hello World
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Data Types
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
Data types in C are used for declaring variables and deciding on
storage and computations:
Built-in / Basic data types are used to define raw data
char
int
float
double
Additionally, C99 defines:
bool
All data items of a given type has the same size (in bytes). The
size is implementation-defined.
Enumerated Type data are internally of int type and operates
on a select subset.
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Data Types
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
Data types in C further include:
void: The type specifier void indicates no type.
Derived data types include:
Array
Structure struct & union
Pointer
Function
String C-Strings are really not a type; but can be made
to behave as such using functions from <string.h> in
standard library
Type modifiers include:
short
long
signed
unsigned
NPTEL MOOCs Programming in C++
Partha Pratim Das
Variables
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
A variable is a name given to a storage area
Declaration of Variables:
Each variable in C has a specific type, which determines
the size and layout of the storage (memory) for the variable
The name of a variable can be composed of letters, digits,
and the underscore character. It must begin with either a
letter or an underscore
int
char
float
double
i,
c,
f,
d,
j, noOfData;
endOfSession;
velocity;
dist_in_light_years;
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Variables
Module 01
Partha Pratim
Das
Initialization of Variables:
Initialization is setting an initial value to a variable at its
definition
Objectives &
Outline
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
int
char
float
double
i = 10, j = 20, numberOfWorkDays = 22;
c = x;
weight = 4.5;
density = 0.0;
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
Literals
Module 01
Partha Pratim
Das
Literals refer to fixed values of a built-in type
Literals can be of any of the basic data types
Objectives &
Outline
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
Std Library
Organization
212
0173
0b1010
0xF2
3.14
x
"Hello"
//
//
//
//
//
//
//
(int) Decimal literal
(int) Octal literal
(int) Binary literal
(int) Hexadecimal literal
(double) Floating-point literal
(char) Character literal
(char *) String literal
In C99, literals are constant values having const types as:
212
0173
0b1010
0xF2
3.14
x
"Hello"
//
//
//
//
//
//
//
(const
(const
(const
(const
(const
(const
(const
int) Decimal literal
int) Octal literal
int) Binary literal
int) Hexadecimal literal
double) Floating-point literal
char) Character literal
char *) String literal
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
10
Operators
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
An operator denotes a specific operation. C has the following
types of operators:
Arithmetic Operators: + * / % ++
Relational Operators: == != > < >= <=
Logical Operators: && || !
Bit-wise Operators: & | << >>
Assignment Operators: = += = *= /=
Miscellaneous Operators: . , sizeof & * ?:
Arity of Operators: Number of operand(s) for an operator
+, , *, & operators can be unary (1 operand) or binary (2 operands)
==, !=, >, <, >=, <=, &&, ||, +=, =, *=, =, /=, &, |, <<,
>> can work only as binary (2 operands) operators
sizeof ! ++ can work only as unary (1 operand) operators
?: works as ternary (3 operands) operator. The condition is the first
operand and the if true logic and if false logic corresponds to the
other two operands.
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
11
Operators
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
Operator Precedence: Determines which operator will be
performed first in a chain of different operators
The precedence of all the operators mentioned above is in the
following order: (left to right Highest to lowest precedence)
(), [], ++, , + (unary), (unary), !, *, &, sizeof, *, /, %, +, , <
<, > >, ==, !=, *=, =, /=, &, |, &&, | |, ?:, =, +=, =, *=, =,
/=, < <=, > >=
Operator Associativity: Indicates in what order operators of
equal precedence in an expression are applied
Consider the expression a b c. If the operator has left
associativity, this expression would be interpreted as (a b) c.
If the operator has right associativity, the expression would be
interpreted as a (b c).
Std Library
Organization
Build Process
References
Summary
Right-to-Left: ?:, =, +=, -=, *=, =, /=, <<=, >>=, , +-, !, *,
&, sizeof
Left-to-Right: *, /, %, +, -, <<, >>, ==, !=, *=, =, /=, &, |,
&&, | |
NPTEL MOOCs Programming in C++
Partha Pratim Das
12
Expressions
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
Every expression has a value
A literal is an expression
A variable is an expression
One, two or three expression/s connected by an operator
(of appropriate arity) is an expression
A function call is an expression
Examples:
For
int i = 10, j = 20, k;
int f(int x, int y) { return x + y; }
Expression are:
2.5
//
i
//
-i
//
i - j
//
k = 5
//
f(i, j)
//
i + j == i * 3 //
(i == j)? 1: 2 //
Value
Value
Value
Value
Value
Value
Value
Value
= 2.5
10
-10
-10
5
30
true
2
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
13
Statement
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
A statement is a command for a specific action. It has no value
A ; (semicolon) is a (null) statement
An expression terminated by a ; (semicolon) is a statement
A list of one or more statements enclosed within a pair of
curly braces { and } or block is a compound statement
Control constructs like if, if-else, switch, for, while,
do-while, goto, continue, break, return are
statements
Example: Expression statements
Expressions
i + j
k = i + j
funct(i,j)
k = funct(i,j)
Example: Compound statements
{
Organization
int i = 2, j = 3, t;
Build Process
t = i;
i = j;
j = t;
References
Summary
Statements
i + j;
k = i + j;
funct(i,j);
k = funct(i,j);
}
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: if, if-else, switch
Labeled-statement: Statements labeled with identifier,
case, or default
Iteration-statement: for, while, do-while
Jump-statement: goto, continue, break, return
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
Examples:
if (a < b) {
int t;
t = a;
a = b;
b = t;
if (x < 5)
x = x + 1;
else {
x = x + 2;
--y;
}
switch (i) {
case 1: x = 5;
break;
case 3: x = 10;
default: x = 15;
}
while (n) {
sum += n;
if (sum > 20)
break;
--n;
}
int f(int x, int y)
{
return x + y;
}
}
int sum
for(i =
int
sum
}
= 0;
0; i < 5; ++i) {
j = i * i;
+= j;
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
15
Module 01: End of Lecture 01
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
Recap of C features
Data types
Variables
Literals
Operators
Expressions
Statements
Control Constructs Conditional Flow & Loops
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
16
Module 01: Lecture 02
Module 01
Partha Pratim
Das
Objectives &
Outline
Recap of C features
Arrays
Structures
Unions
Pointers
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
17
Arrays
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
An array is a collection of data items, all of the same type,
accessed using a common name
Declare Arrays:
#define SIZE 10
int name[SIZE];
// SIZE must be an integer constant greater than zero
double balance[10];
Initialize Arrays:
int primes[5] = {2, 3, 5, 7, 11}; // Size = 5
int primes[] = {2, 3, 5, 7, 11};
int sizeOfPrimes = sizeof(primes)/sizeof(int); // size is 5 by initialization
int primes[5] = {2, 3};
// Size = 5, last 3 elements set to 0
Access Array elements:
int primes[5] = {2, 3};
int EvenPrime = primes[0]; // Read 1st element
primes[2] = 5;
// Write 3rd element
Multidimensional Arrays:
int mat[3][4];
for(i = 0; i < 3; ++i)
for(j = 0; j < 4; ++j)
mat[i][j] = i + j;
NPTEL MOOCs Programming in C++
Partha Pratim Das
18
Structures
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
A structure is a collection of data items of different types. Data
items are called members. The size of a structure is the sum of
the size of its members.
Declare Structures:
struct Complex { // Complex Number
double re;
// Real component
double im;
// Imaginary component
} c;
// c is a variable of struct Complex type
printf("size = %d\n", sizeof(struct Complex)); // Prints: size = 16
typedef struct _Books {
char title[50];
char author[50];
int
book_id;
} Books; // Books is an
// data member
// data member
// data member
alias for struct _Books type
Initialize Structures:
struct Complex x = {2.0, 3.5}; // Both members
struct Complex y = {4.2};
// Only the first member
Std Library
Access Structure members:
Organization
struct Complex x = {2.0, 3.5};
double norm = sqrt(x.re*x.re + x.im*x.im); // Using . (dot) operator
Build Process
References
Summary
Books book;
book.book_id = 6495407;
strcpy(book.title, "C Programming");
NPTEL MOOCs Programming in C++
Partha Pratim Das
19
Unions
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
A union is a special structure that allocates memory only for the
largest data member and holds only one member as a time
Declare Union:
typedef union _Packet { // Mixed Data Packet
int
iData;
// integer data
double dData;
// floating point data
char
cData;
// character data
} Packet;
printf("size = %d\n", sizeof(Packet)); // Prints: size = 8
Initialize Union:
Packer p = {10}; // Initialize only with a value of the type of first member
printf("iData = %d\n", p.iData);
// Prints: iData = 10
Access Union members:
p.iData = 2;
printf("iData = %d\n", p.iData);
p.dData = 2.2;
printf("dData = %lf\n", p.dData);
p.cData = a;
printf("cData = %c\n", p.cData);
// Prints: iData = 2
// Prints: dData = 2.200000
// Prints: cData = a
Organization
Build Process
References
Summary
p.iData = 97;
printf("iData = %d\n", p.iData);
printf("dData = %lf\n", p.dData);
printf("cData = %c\n", p.cData);
NPTEL MOOCs Programming in C++
// Prints: iData = 97
// Prints: dData = 2.199999
// Prints: cData = a
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
Objectives &
Outline
int
double
float
char
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
*ip;
*dp;
*fp;
*ch
//
//
//
//
pointer
pointer
pointer
pointer
to
to
to
to
an integer
a double
a float
a character
Using a pointer:
int main() {
int i = 20;
int *ip;
ip = &i;
// variable declaration
// pointer declaration
// store address of i in pointer
printf("Address of variable: %p\n", &i); // Prints: Address of variable : 00A8F73C
printf("Value of pointer: %p\n", ip);
// Prints: Value of pointer : 00A8F73C
printf("Value of pointee: %d\n", *ip);
// Prints: Value of pointee : 20
Std Library
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
Das
int a[] = {1, 2, 3, 4, 5};
int *p;
Objectives &
Outline
p = a;
printf("a[0] = %d\n", *p);
// a[0] = 1
printf("a[1] = %d\n", *++p);
// a[1] = 2
printf("a[2] = %d\n", *(p+1)); // a[2] = 3
struct Complex {
double re;
double im;
} c = { 0.0, 0.0
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
p = &a[2];
*p = -10;
printf("a[2] = %d\n", a[2]);
// Complex Number
// Real component
// Imaginary component
};
struct Complex *p = &c;
(*p).re = 2.5;
p->im = 3.6;
// a[2] = -10
printf("re = %lf\n", c.re); // re = 2.500000
printf("im = %lf\n", c.im); // im = 3.600000
malloc-free
Dynamically allocated arrays
int *p = (int *)malloc(sizeof(int));
int *p = (int *)malloc(sizeof(int)*3);
*p = 0x8F7E1A2B;
printf("%X\n", *p);
p[0] = 1; p[1] = 2; p[2] = 3;
// 8F7E1A2B
unsigned char *q = p;
printf("%X\n", *q++);
printf("%X\n", *q++);
printf("%X\n", *q++);
printf("%X\n", *q++);
//
//
//
//
printf("p[1] = %d\n", *(p+1)); // p[1] = 2
free(p);
2B
1A
7E
8F
free(p);
NPTEL MOOCs Programming in C++
Partha Pratim Das
22
Module 01: End of Lecture 02
Module 01
Partha Pratim
Das
Objectives &
Outline
Recap of C features
Arrays
Structures
Unions
Pointers
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
23
Module 01: Lecture 03
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
Recap of C features
Functions
Input / Output
C Standard Library
Source Organization for a C program
Build Process
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
24
Functions
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
A function performs a specific task or computation
Has 0, 1, or more parameters / arguments. Every
argument has a type (void for no argument)
May or may not return a result. Return value has a type
(void for no result)
Function declaration:
//
//
//
//
Function Prototype / Header / Signature
Name of the function: funct
Parameters: x and y. Types of parameters: int
Return type: int
int funct(int x, int y);
Function definition:
// Function Implementation
int funct(int x, int y)
// Function Body
{
return (x + y);
}
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
25
Functions
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
Call-by-value mechanism for passing arguments. The value of
an actual parameter copied to the formal parameter
Return-by-value mechanism to return the value, if any.
int funct(int x, int y) {
++x; ++y;
return (x + y);
}
// Formal parameters changed
int main() {
int a = 5, b = 10, z;
printf("a = %d, b = %d\n", a, b); // prints: a = 5, b = 10
z = funct(a, b); //
//
//
//
//
//
function call by value
a copied to x. x becomes 5
b copied to y. y becomes 10
x in funct changes to 6 (++x)
y in funct changes to 11 (++y)
return value (x + y) copied to z
printf("funct = %d\n", z); // prints: funct = 17
// Actual parameters do not change on return (call-by-value)
printf("a = %d, b = %d\n", a, b); // prints: a = 5, b = 10
return 0;
}
NPTEL MOOCs Programming in C++
Partha Pratim Das
26
Functions
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
A function may be recursive (call itself)
Has recursive step/s
Has exit condition/s
Example:
// Factorial of n
unsigned int factorial(unsigned int n) {
if (n > 0)
return n * factorial(n - 1); // Recursive step
else
return 1;
// Exit condition
}
// Number of 1s in the binary representation of n
unsigned int nOnes(unsigned int n) {
if (n == 0)
return 0; // Exit condition
else // Recursive steps
if (n % 2 == 0)
return nOnes(n / 2);
else
return nOnes(n / 2) + 1;
}
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
27
Function pointers
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
#include <stdio.h>
struct GeoObject {
enum { CIR = 0, REC, TRG }
union {
struct Cir { double x,
struct Rec { double x,
struct Trg { double x,
};
};
gCode;
y, r; } c;
y, w, h; } r;
y, b, h; } t;
DrawFunc DrawArr[] = { // Array of func. ptrs
drawCir, drawRec, drawTrg };
int main() {
struct GeoObject go;
go.gCode = CIR;
go.c.x = 2.3; go.c.y = 3.6;
go.c.r = 1.2;
DrawArr[go.gCode](go); // Call by ptr
typedef void(*DrawFunc) (struct GeoObject);
go.gCode = REC;
go.r.x = 4.5; go.r.y = 1.9;
go.r.w = 4.2; go.r.h = 3.8;
DrawArr[go.gCode](go); // Call by ptr
void drawCir(struct GeoObject go) {
printf("Circle: (%lf, %lf, %lf)\n",
go.c.x, go.c.y, go.c.r); }
void drawRec(struct GeoObject go) {
printf("Rect: (%lf, %lf, %lf, %lf)\n",
go.r.x, go.r.y, go.r.w, go.r.h); }
void drawTrg(struct GeoObject go) {
printf("Triag: (%lf, %lf, %lf, %lf)\n",
go.t.x, go.t.y, go.t.b, go.t.h); }
go.gCode = TRG;
go.t.x = 3.1; go.t.y = 2.8;
go.t.b = 4.4; go.t.h = 2.7;
DrawArr[go.gCode](go); // Call by ptr
return 0;
}
Organization
Build Process
References
Summary
Circle: (2.300000, 3.600000, 1.200000)
Rect: (4.500000, 1.900000, 4.200000, 3.800000)
Triag: (3.100000, 2.800000, 4.400000, 2.700000)
NPTEL MOOCs Programming in C++
Partha Pratim Das
28
Input / Output
Module 01
Partha Pratim
Das
int printf(const char *format, ...) writes to stdout
by the format and returns the number of characters written
Objectives &
Outline
int scanf(const char *format, ...) reads from stdin
by the format and returns the number of characters read
Recap of C
Use %s, %d, %c, %lf, to print/scan string, int, char, double
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a value :");
// prints a constant string
scanf("%s %d", str, &i);
// scans a string value and an integer value
printf("You entered: %s %d ", str, i); // prints string and integer
Std Library
Organization
return 0;
}
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
29
Input / Output
Module 01
Partha Pratim
Das
To write to or read from file:
#include <stdio.h>
int main() {
Objectives &
Outline
FILE *fp = NULL;
int i;
Recap of C
fp = fopen("Input.dat", "r");
fscanf(fp, "%d", &i);
fclose(fp);
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
// scan from Input.dat
fp = fopen("Output.dat", "w");
fprintf("%d^2 = %d\n", i, i*i); // prints to Output.dat
fclose(fp);
return 0;
}
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
30
C Standard Library
Module 01
Partha Pratim
Das
Common Library Components:
Component
stdio.h
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
stdlib.h
string.h
math.h
errno.h
Data Types, Manifest Constants, Macros, Functions, ...
Formatted and un-formatted file input and output including
functions
printf, scanf, fprintf, fscanf, sprintf, sscanf, feof,
etc.
Memory allocation, process control, conversions, pseudorandom numbers, searching, sorting
malloc, free, exit, abort, atoi, strtold, rand,
bsearch, qsort, etc.
Manipulation of C strings and arrays
strcat, strcpy, strcmp, strlen, strtok, memcpy,
memmove, etc.
Common mathematical operations and transformations
cos, sin, tan, acos, asin, atan, exp, log, pow, sqrt, etc.
Macros for reporting and retrieving error conditions through
error codes stored in a static memory location called errno
EDOM (parameter outside a functions domain sqrt(-1)),
ERANGE (result outside a functions range), or
EILSEQ (an illegal byte sequence), etc.
NPTEL MOOCs Programming in C++
Partha Pratim Das
31
Source Organization for a C program
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
Header Files
A header file has extension .h and contains C function
declarations and macro definitions to be shared between several
source files
There are two types of header files:
Files that the programmer writes
Files from standard library
Header files are included using the #include pre-processing
directive
#include <file> for system header files
#include "file" for header files of your own program
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
32
Source Organization for a C program
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
Example:
// Solver.h -- Header files
int quadraticEquationSolver(double, double, double, double*, double*);
// Solver.c -- Implementation files
#include "Solver.h"
int quadraticEquationSolver(double a, double b, doublec , double* r1, double* r2) {
// ...
// ...
// ...
return 0;
}
// main.c -- Application files
#include "Solver.h"
int main() {
double a, b, c;
double r1, r2;
Std Library
int status = quadraticEquationSolver(a, b, c, &r1, &r2);
Organization
Build Process
return 0;
}
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
Partha Pratim
Das
Objectives &
Outline
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
C Pre-processor (CPP) substitutes and includes functions,
headers and macros before compilation
int sum(int, int);
int main() {
int a = sum(1,2);
return a;
}
The compiler translates the pre-processed C code into assembly
language, which is a machine level code that contains
instructions that manipulate the memory and processor directly
Std Library
The linker links our program with the pre-compiled libraries for
using their functions
In the running example, function.c and main.c are first
compiled and then linked
Organization
int sum(int a,int b) { return a+b; }
Build Process
References
Summary
int main() {
int a = sum(1,2); // as files are linked, uses functions directly
return a;
}
NPTEL MOOCs Programming in C++
Partha Pratim Das
35
Tools
Module 01
Partha Pratim
Das
Development IDE: Code::Blocks 16.01
Compiler: -std=c++98 and -std=c99
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
36
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
Kernighan, Brian W., and Dennis M. Richie. The C
Programming Language. Vol. 2. Englewood Cliffs:
Prentice-Hall, 1988.
King, Kim N., and Kim King. C programming: A Modern
Approach. Norton, 1996.
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
37
Module Summary
Module 01
Partha Pratim
Das
Revised the concept of variables and literals in C
Revised the various data types and operators of C
Objectives &
Outline
Recap of C
Data Types
Variables
Literals
Operators
Expressions
Statements
Control Flow
Arrays
Structures
Unions
Pointers
Functions
Input / Output
Re-iterated through the control constructs of C
Re-iterated through the concepts of functions and pointers
of C
Re-iterated through the program organization of C and the
build process.
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
38
Instructor and TAs
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
Name
Partha Pratim Das, Instructor
Tanwi Mallick, TA
Srijoni Majumdar, TA
Himadri B G S Bhuyan, TA
Mail
[email protected]
[email protected]
[email protected]
[email protected]
Mobile
9830030880
9674277774
9674474267
9438911655
Std Library
Organization
Build Process
References
Summary
NPTEL MOOCs Programming in C++
Partha Pratim Das
39