C_programming_Chapter03_SEN2201
C_programming_Chapter03_SEN2201
Review
Control flow
I/O
Standard I/O
String I/O
File I/O
1
Blocks
{
i n t x =0;
{
i n t y =0; / ∗ both x and y v i s i b l e ∗ /
}
/∗ only x v i s i b l e ∗/
}
1
Conditional blocks
2
Conditional blocks
3
Iterative blocks
4
Review
Control flow
I/O
Standard I/O
String I/O
File I/O
5
goto
start :
{
i f ( cond )
goto o u t s i d e ;
/ ∗ some code ∗ /
goto s t a r t ;
}
outside :
/∗ outside block ∗/
5
Spaghetti code
6
error handling
Language like C++ and Java provide exception mechanism to
recover from errors. In C, goto provides a convenient way to exit
from nested blocks.
c o n t _ f l a g =1;
for ( . . )
for ( . . ) {
{ for ( i n i t ; cont_flag ; i t e r )
for ( . . ) {
{ i f ( error_cond )
i f ( error_cond ) {
goto e r r o r ; c o n t _ f l a g =0;
/∗ skips 2 blocks ∗/ break ;
} }
} / ∗ inner loop ∗ /
error : }
i f ( ! c o n t _ f l a g ) break ;
/ ∗ outer loop ∗ /
}
7
Review
Control flow
I/O
Standard I/O
String I/O
File I/O
8
Preliminaries
8
Standard input and output
int putchar(int)
• putchar(c) puts the character c on the standard output.
• it returns the character printed or EOF on error.
int getchar()
• returns the next character from standard input.
• it returns EOF on error.
9
Standard input and output
What does the following code do?
i n t main ( )
{
char c ;
while ( ( c= g e t c h a r ( ) ) ! = EOF)
{
i f ( c>=’A’ && c<=’Z’ )
c=c−’A’+’a’ ;
putchar ( c ) ;
}
return 0;
}
11
printf format specification
The format specification has the following components
%[flags ][ width ][. precision ][ length]<type>
type:
12
printf format specification (cont.)
format output
printf ("%d",10) "10"
printf ("%4d",10) bb10 (b:space)
printf ("%s","hello") hello
printf ("%7s","hello") bbhello
13
printf format specification (cont.)
format output
printf ("%d,%+d,%+d",10,−10) 10,+10,-10
printf ("%04d",10) 0010
printf ("%7s","hello") bbhello
printf ("%-7s","hello") hellobb
14
printf format specification (cont.)
format output
printf ("%.2f,%.0f,1.141,1.141) 1.14,1
printf ("%.2e,%.0e,1.141,100.00) 1.14e+00,1e+02
printf ("%.4s","hello") hell
printf ("%.1s","hello") h
15
printf format specification (cont.)
modifier meaning
h interpreted as short. Use with i,d,o,u,x
l interpreted as long. Use with i,d,o,u,x
L interpreted as double. Use with e,f,g
16
Digression: character arrays
17
Digression: character arrays
18
Formatted input
19
Formatted input
20
String input/output
21
File I/O
So far, we have read from the standard input and written to the
standard output. C allows us to read data from text/binary files
using fopen().
FILE∗ fopen(char name[],char mode[])
• mode can be "r" (read only),"w" (write only),"a" (append)
among other options. "b" can be appended for binary files.
• fopen returns a pointer to the file stream if it exists or
NULL otherwise.
• We don’t need to know the details of the FILE data type.
• Important: The standard input and output are also FILE*
datatypes (stdin,stdout).
• Important: stderr corresponds to standard error
output(different from stdout).
22
File I/O(cont.)
23
File input
int getc(FILE∗ fp)
24
File output
int putc(int c,FILE∗ fp)
• writes a single character c to the output stream.
• returns the character written or EOF on error.
Note: putchar simply uses the standard output to write a
character. We can implement it as follows:
#define putchar(c) putc(c,stdout)
• similar to scanf,sscanf
• reads items from input stream fp.
25
Command line input
26
Command line input (cont.)
27
Review
Strings
String Utility Functions
1
Review: Unconditional jumps
1
Review: I/O Functions
2
Review: printf() and scanf()
• Formatted output:
int printf (char format [], arg1, arg2, ...)
• Takes variable number of arguments
• Format specification:
%[flags][width][.precision][length]<type>
• types: d, i (int), u, o, x, X (unsigned int), e, E, f, F, g, G
(double), c (char), s (string)
• flags, width, precision, length - modify meaning and number
of characters printed
• Formatted input: scanf() - similar form, takes pointers to
arguments (except strings), ignores whitespace in input
3
Review: Strings and character arrays
4
Review
Strings
String Utility Functions
5
Pointers and addresses
5
Physical and virtual memory
6
Physical memory considerations
7
Virtual memory
8
Addressing variables
9
Dereferencing pointers
10
Casting pointers
11
Functions with multiple outputs
12
Accessing caller’s variables
13
Variables passing out of scope
char ∗ get_message ( ) {
char msg [ ] = "Aren’t pointers fun?" ;
r e t u r n msg ;
}
i n t main ( void ) {
char ∗ s t r i n g = get_message ( ) ;
puts ( s t r i n g ) ;
return 0;
}
14
Variables passing out of scope
char ∗ get_message ( ) {
char msg [ ] = "Aren’t pointers fun?" ;
r e t u r n msg ;
}
i n t main ( void ) {
char ∗ s t r i n g = get_message ( ) ;
puts ( s t r i n g ) ;
return 0;
}
14
Review
Strings
String Utility Functions
15
Arrays and pointers
15
The sizeof() operator
16
Pointer arithmetic
17
Pointer arithmetic
17
Review
Strings
String Utility Functions
18
Strings as arrays
18
String utility functions
19
More string utility functions
20
Summary
Topics covered:
• Pointers: addresses to memory
• physical and virtual memory
• arrays and strings
• pointer arithmetic
33
Review
1
Review: pointers
1
Review: string.h
2
Structure
s t r u c t employee
struct point {
{ char fname [ 1 0 0 ] ;
int x ; char lname [ 1 0 0 ] ;
int y ; i n t age ;
}; };
/ ∗ n o t i c e t h e ; a t t h e end ∗ / / ∗ members o f d i f f e r e n t
type ∗ /
4
Structure
5
Structure (cont.)
More examples:
struct t r i a n g l e s t r u c t chain_element
{ {
s t r u c t p o i n t ptA ; i n t data ;
s t r u c t p o i n t ptB ; s t r u c t chain_element ∗ n e x t ;
s t r u c t p o i n t ptC ; };
}; / ∗ members can be
/ ∗ members can be s t r u c t u r e s ∗ / s e l f r e f e r e n t i a l ∗ /
6
Structure (cont.)
7
Structure pointers
• Structures are copied element wise.
• For large structures it is more efficient to pass pointers.
void foo(struct point ∗ pp); struct point pt ; foo(&pt)
• Members can be accesses from structure pointers using
’->’ operator.
struct point p={10 ,20};
s t r u c t p o i n t ∗ pp=&p ;
pp−>x = 1 0 ; / ∗ changes p . x ∗ /
i n t y= pp−>y ; / ∗ same as y=p . y ∗ /
Other ways to access structure members?
struct point p={10 ,20};
s t r u c t p o i n t ∗ pp=&p ;
( ∗ pp ) . x = 1 0 ; / ∗ changes p . x ∗ /
i n t y= ( ∗ pp ) . y ; / ∗ same as y=p . y ∗ /
why is the () required?
8
Arrays of structures
9
Size of structures
10
Union
11
Unions (cont.)
12
Bit fields
Definition: A bit-field is a set of adjacent bits within a single
’word’. Example:
struct f l a g {
unsigned i n t i s _ c o l o r : 1 ;
unsigned i n t has_sound : 1 ;
unsigned i n t i s _ n t s c : 1 ;
};
13
Digression: dynamic memory allocation
void∗ malloc(size_t n)
• malloc() allocates blocks of memory
• returns a pointer to unitialized block of memory on
success
• returns NULL on failure.
• the returned value should be cast to appropriate type using
(). int∗ ip=(int∗)malloc(sizeof(int)∗100)
void∗ calloc( size_t n,size_t size)
• allocates an array of n elements each of which is ’size’
bytes.
• initializes memory to 0
void free(void∗)
• Frees memory allocated my malloc()
• Common error: accessing memory after calling free
14
Review
1
Review: Compound data types
1
Review: Compound data types
struct foo {
short s ;
union {
int i ;
char c ;
} u;
unsigned i n t f l a g _ s : 1 ;
unsigned i n t f l a g _ u : 2 ;
unsigned i n t bar ;
};
2
Review: Compound data types
struct foo {
short s ; ← 2 bytes
union { ← 4 bytes,
int i ; 4 byte-aligned
char c ;
} u;
unsigned i n t f l a g _ s : 1 ; ← bit fields
unsigned i n t f l a g _ u : 2 ;
unsigned i n t bar ; ← 4 bytes,
}; 4 byte-aligned
2
Pointer review
5
Pointers to pointers
6
Pointer pointers example
7
Pointer pointers example
7
Pointer arrays
8
Pointer array example
9
Pointer array example
Insertion sort:
/ ∗ move p r e v i o u s elements down u n t i l
i n s e r t i o n p o i n t reached ∗ /
void s h i f t _ e l e m e n t ( unsigned i n t i ) {
i n t ∗ pvalue ;
/ ∗ guard a g a i n s t going o u t s i d e a r r a y ∗ /
f o r ( pvalue = s o r t e d _ a r r a y [ i ] ; i &&
∗ s o r t e d _ a r r a y [ i −1] > ∗ pvalue ; i −−) {
/ ∗ move p o i n t e r down ∗ /
s o r t e d _ a r r a y [ i ] = s o r t e d _ a r r a y [ i −1];
}
s o r t e d _ a r r a y [ i ] = pvalue ; / ∗ i n s e r t p o i n t e r ∗ /
}
10
Pointer array example
11
String arrays
12
Multidimensional arrays
13
Summary
Topics covered:
• Pointers to pointers
• pointer and string arrays
• multidimensional arrays
37