0% found this document useful (0 votes)
39 views51 pages

Brief Introduction To The C Programming Language

1) The document provides an introduction to the C programming language, covering its history, standardization, elements of a C program like main() and header files. 2) It discusses basic C programming concepts like data types, variables, operators, functions, input/output and control flow. Pointers and arrays are explained in detail. 3) The preprocessor is described along with macros and conditional compilation. Standard library header files commonly used in C are listed. Examples of simple C programs are provided to demonstrate key concepts.

Uploaded by

Sudip Adhikari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views51 pages

Brief Introduction To The C Programming Language

1) The document provides an introduction to the C programming language, covering its history, standardization, elements of a C program like main() and header files. 2) It discusses basic C programming concepts like data types, variables, operators, functions, input/output and control flow. Pointers and arrays are explained in detail. 3) The preprocessor is described along with macros and conditional compilation. Standard library header files commonly used in C are listed. Examples of simple C programs are provided to demonstrate key concepts.

Uploaded by

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

Washington

WASHINGTON UNIVERSITY IN ST LOUIS


Brief Introduction to the C
Programming Language
Fred Kuhns
[email protected]
Applied Research Laorator!"
#epartment of Computer $cience and %ngineering"
&ashington 'ni(ersit! in $t. Louis
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)
Introduction
* +he C programming language was designed ! #ennis
Ritchie at Bell Laoratories in the earl! ,-./s
* Influenced !
0 AL12L 3/ 4,-3/5"
0 CPL 4Camridge" ,-365"
0 BCPL 47artin Richard" ,-3.5"
0 B 4Ken +hompson" ,-./5
* +raditionall! used for s!stems programming" though
this ma! e changing in fa(or of C88
* +raditional C9
0 +he C Programming Language" ! Brian Kernighan and #ennis
Ritchie" )
nd
%dition" Prentice :all
0 Referred to as K;R
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6
$tandard C
* $tandardi<ed in ,-=- ! A>$I 4American >ational
$tandards Institute5 known as A>$I C
* International standard 4I$25 in ,--/ which was
adopted ! A>$I and is known as C=-
* As part of the normal e(olution process the standard
was updated in ,--? 4C-?5 and ,--- 4C--5
* C88 and C
0 C88 e@tends C to include support for 2Aect 2riented
Programming and other features that facilitate large software
de(elopment proAects
0 C is not strictl! a suset of C88" ut it is possile to write
BClean CC that conforms to oth the C88 and C standards.
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D
%lements of a C Program
* A C de(elopment en(ironment includes
0 $!stem liraries and headers9 a set of standard liraries and
their header files. For e@ample see /usr/include and glibc.
0 Application $ource9 application source and header files
0 Compiler9 con(erts source to oAect code for a specific platform
0 Linker9 resol(es e@ternal references and produces the
e@ecutale module
* 'ser program structure
0 there must e one main function where e@ecution egins when
the program is run. +his function is called main

int main (void) { ... },

int main (int argc, char *argv[]) { ... }


* '>IE $!stems ha(e a 6
rd
wa! to define main45" though it is not
P2$IE., compliant
int main (int argc, char *argv[], char *envp[])
0 additional local and e@ternal functions and (ariales
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
?
A $imple C Program
* Create e@ample file9 try.c
* Compile using gcc9
gcc o try try.c
* +he standard C lirar! lic is included
automaticall!
* %@ecute program
./try
* >ote" I alwa!s specif! an asolute path
* >ormal termination9
void exit(int status)!
0 calls functions registered with
ate"it()
0 flush output streams
0 close all open streams
0 return status (alue and control to host
en(ironment
/* you generally want to
* include stdio.h and
* stdlib.h
* */
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
printf(Hello orld!n")#
exit($)#
%
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
3
$ource and :eader files
* Fust as in C88" place related code within the same module
4i.e. file5.
* :eader files 4*.h5 e@port interface definitions
0 function protot!pes" data t!pes" macros" inline functions and other
common declarations
* #o not place source code 4i.e. definitions5 in the header
file with a few e@ceptions.
0 inlineGd code
0 class definitions
0 const definitions
* C preprocessor 4cpp5 is used to insert common definitions
into source files
* +here are other cool things !ou can do with the
preprocessor
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
.
Another %@ample C Program
[email protected]
/* this is a C-style comment
* You generally want to palce
* all file includes at start of file
* */
#include <stdio.h>
#include <stdlib.h>
int
main (int ar&c' char **ar&v)
{
// this is a ())*st+le comment
// printf protot+pe in stdio.h
printf(Hello' ,ro& name - .s!n"'
ar&v/$0)#
exit($)#
%
/* comments */
#i$nde$ %&'()*%+
#de$ine %&'()*%+
... de$initions and protoypes
#endi$
HusrHincludeHstdio.h
/* prevents including file
* contents multiple
* times */
#i$nde$ %&'(,)-%+
#de$ine %&'(,)-%+
... de$initions and protoypes
#endi$
HusrHincludeHstdli.h
#include directs the preprocessor
to BincludeC the contents of the file
at this point in the source file.
#de$ine directs preprocessor to
define macros.
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
=
Passing Command Line Arguments
* &hen !ou e@ecute a program
!ou can include arguments on
the command line.
* +he run time en(ironment will
create an argument (ector.

argv is the argument (ector

argc is the numer of


arguments
* Argument (ector is an arra! of
pointers to strings.
* a string is an arra! of
characters terminated ! a
inar! / 4>'LL or IJ/G5.
* arg(K/L is alwa!s the program
name" so argc is at least ,.
./try g . $red
argc / 0,
argv / <address0>
1t21r21y21342
argv5
[4] <addres1>
[6] <addres2>
[.] <addres3>
[7] <addres4>
[0] 1233
1821g21342
1.21342
1$21r21e21d21342
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
-
C $tandard :eader Files !ou ma! want to use
* $tandard :eaders !ou should know aout9

stdio.h 0 file and console 4also a file5 I29 perror, printf,


open, close, read, write, scanf" etc.

stdlib.h M common utilit! functions9 malloc, calloc,


strtol, atoi, etc

string.h M string and !te manipulation9 strlen, strcpy,


strcat, memcpy, memset, etc.

ctype.h 0 character t!pes9 isalnum, isprint,


isupport, tolower, etc.

errno.h 0 defines errno used for reporting s!stem errors

math.h 0 math functions9 ceil, exp, floor, sqrt, etc.

signal.h 0 signal handling facilit!9 raise, signal, etc

stdint.h 0 standard integer9 int!t, uint!t, etc

time.h 0 time related facilit!9 asctime, cloc", time!t,


etc.
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,/
+he Preprocessor
* +he C preprocessor permits !ou to define simple
macros that are e(aluated and e@panded prior to
compilation.
* Commands egin with a ING. Are(iated list9

#de$ine 9 defines a macro

#unde$ 9 remo(es a macro definition

#include 9 insert te@t from file

#i$ 9 conditional ased on (alue of e@pression

#i$de$ 9 conditional ased on whether macro defined

#i$nde$ 9 conditional ased on whether macro is not defined

#else 9 alternati(e

#eli$ 9 conditional alternati(e

de$ined() 9 preprocessor function9 , if name defined" else /


#i$ de$ined(%%9et-&(%%)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,,
Preprocessor9 7acros
* 'sing macros as functions" e@ercise caution9
0 flawed e@ample9 #de$ine mymult(a,b) a*b
* $ource9 : / mymult(i86, ;<=)!
* Post preprocessing9 : / i 6 * ; < =!
0 etter9 #de$ine mymult(a,b) (a)*(b)
* $ource9 : / mymult(i86, ;<=)!
* Post preprocessing9 : / (i 6)*(; < =)!
* Be careful of side effects" for e@ample what if we did
the following
0 7acro9 #de$ine mys>(a) (a)*(a)
0 flawed usage9
* $ource9 : / mys>(i<<)
* Post preprocessing9 : / (i<<)*(i<<)
* Alternati(e is to use inlineGed functions

inline int mys>(int a) {return a*a}!

mys>(i<<) works as e@pected in this case.


Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,)
Preprocessor9 Conditional Compilation
* Its generall! etter to use inlineGed functions
* +!picall! !ou will use the preprocessor to define
constants" perform conditional code inclusion" include
header files or to create shortcuts

#de$ine (?@AB,'%&ACD,?& 644

#i$de$ %%linu"
static inline intE0%t
gettime(void) {...}

#eli$ de$ined(sun)
static inline intE0%t
gettime(void) {return (intE0%t)gethrtime()}

#else
static inline intE0%t
gettime(void) {... gettimeo$day()...}

#endi$
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,6
Another $imple C Program
int main (int argc, char **argv) {
int i!
print$(F'here are Gd arguments3nH, argc)!
for (i - $# i < argc# i)))
print$(FArg Gd / Gs3nH, i, argv[i])!
return 4!
%

>otice that the s!nta@ is similar to Fa(a


*&hatGs new in the ao(e simple programO
0 of course !ou will ha(e to learn the new interfaces and utilit!
functions defined ! the C standard and '>IE
0 Pointers will gi(e !ou the most troule
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,D
* A (ariale declared as an arra! represents a contiguous
region of memor! in which the arra! elements are stored.
int "[=]! // an array o$ = 08byte ints.
* All arra!s egin with an inde@ of /
* An arra! identifier is ePui(alent to a pointer that
references the first element of the arra!

int "[=], *ptr!


ptr / I"[4] is ePui(alent to ptr / "!
* Pointer arithmetic and arra!s9

int "[=]!
"[.] is the same as Q(" < .)" the compiler will assume !ou
mean ) oAects e!ond element @.
Arra!s and Pointers
0
1
2
3
4
1 0 2 3
little endian byte ordering
#e#r% &"%ut 'r "rr"% (
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,?
Pointers
* For an! t!pe +" !ou ma! form a pointer t!pe to +.
0 Pointers ma! reference a function or an oAect.
0 +he (alue of a pointer is the address of the corresponding oAect or
function
0 %@amples9 int *i! char *"! int (*my$unc)()!
* Pointer operators9 * dereferences a pointer" ; creates a pointer
4reference to5

int i / 7! int *; / Ii!


*; / 0! print$(Fi / Gd3nH, i)! // prints i / 0

int my$unc (int arg)!


int (*$ptr)(int) / my$unc!
i / $ptr(0)! // same as calling my$unc(0)!
* 1eneric pointers9
0 +raditional C used 4char Q5
0 $tandard C uses 4(oid Q5 0 these can not e dereferenced or used in
pointer arithmetic. $o the! help to reduce programming errors
* >ull pointers9 use NULL or 0. It is a good idea to alwa!s initiali<e
pointers to >'LL.
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,3
Pointers in C 4and C885
Address
0x3d
0x3d!
"rogra# $e#or%
0x3
0x3!
0x3&
0x30
Note' The o#(i)er on*erts +,-. or /0+1-2 to
Value at address 0Address of z 1 sizeof(int))3
In 4 %o5 6o5)d 6rite the 7%te address as'
(char *)J < siJeo$(int)!
or )etting the o#(i)er do the 6or8 9or %o5
(int *)J < 6!
&tep 65
int main (int argc, argv) {
int x - 4#
int *+ - 5x#
int *6/40 - {NULL' NULL' NULL' NULL%#
int a/40 - {7' 8' 9' 4%#
...
0x37
0x37!
0x37&
0x370
0x3d&
0x3d0
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
4
0x3dc
0
0
0
0
4
3
2
1
NA
NA
x
y
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,.
Pointers Continued
&
0x3d
Address
0x3d
0x3d!
"rogra# $e#or%
0x3bc
0x3b8
0x3b4
0x3b0
0x3
0x3!
0x3&
0x30
&tep 65
int main (int argc, argv) {
int " / 0!
int *y / I"!
int *J[0] / {9B,,, 9B,,, 9B,,, 9B,,}!
int a[0] / {6, ., 7, 0}!
&tep .5 Assign addresses to array K
6/$0 - a# // same as Ia[4]!
6/70 - a ) 7# // same as Ia[6]!
6/80 - a ) 8# // same as Ia[.]!
6/90 - a ) 9# // same as Ia[7]!
0x37
0x37!
0x37&
0x370
&
3
:
-
NA
0x3d&
0x3d0
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
NA
x
y
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,=
Pointers Continued
&
0x3d
Address
0x3d
0x3d!
"rogra# $e#or%
0x3bc
0x3b8
0x3b4
0x3b0
0x3
0x3!
0x3&
0x30
&tep 65
int main (int argc, argv) {
int " / 0!
int *y / I"!
int *J[0] / {9B,,, 9B,,, 9B,,,
9B,,}!
int a[0] / {6, ., 7, 0}!
&tep .5
J[4] / a!
J[6] / a < 6!
J[.] / a < .!
J[7] / a < 7!
&tep 75 9o change in J2s values
6/$0 - (int *)((char *)a)#
6/70 - (int *)((char *)a
) si6eof(int))#
6/80 - (int *)((char *)a
) 8 * si6eof(int))#
6/90 - (int *)((char *)a
) 9 * si6eof(int))#
0x37
0x37!
0x37&
0x370
&
3
:
-
NA
0x3d&
0x3d0
z[3]
z[2]
z[1]
z[0]
a[3]
a[2]
a[1]
a[0]
NA
x
y
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
,-
1etting Fanc! with 7acros
#define :1;<=(t+pe) !
struct { !
struct t+pe *next# !
struct t+pe **prev# !
%
#de$ine :1;<=>?1?@(node, $ield) 3
do { 3
(node)8L$ield.ne"t / (node)! 3
(node)8L$ield.prev / 3
I(node)8L$ield.ne"t! 3
} Mhile ( /* */ 4 )!
#de$ine :A?BC@(head, $ield) 3
((head)8L$ield.ne"t)
#de$ine :1=D@(node, $ield) 3
((node)8L$ield.ne"t)
#de$ine :=E,@F(head, $ield) 3
((head)8L$ield.ne"t // (head))
#de$ine :A;B=G(H(head, var, $ield) 3
$or ((var) / (head)8L$ield.ne"t! 3
(var) N/ (head)! 3
(var) / (var)8L$ield.ne"t)
#de$ine :?1C=B@>H=A;B=(loc, node, $ield) 3
do { 3
*(loc)8L$ield.prev / (node)! 3
(node)8L$ield.prev / 3
(loc)8L$ield.prev! 3
(loc)8L$ield.prev / 3
I((node)8L$ield.ne"t)! 3
(node)8L$ield.ne"t / (loc)! 3
} Mhile (/* */4)
#de$ine :?1C=B@>GA@=B(loc, node, $ield) 3
do { 3
((loc)8L$ield.ne"t)8L$ield.prev / 3
I(node)8L$ield.ne"t! 3
(node)8L$ield.ne"t / (loc)8L$ield.ne"t! 3
(loc)8L$ield.ne"t / (node)! 3
(node)8L$ield.prev / I(loc)8L$ield.ne"t! 3
} Mhile ( /* */ 4)
#de$ine :B=E;I=(node, $ield) 3
do { 3
*((node)8L$ield.prev) / (node)8L$ield.ne"t! 3
((node)8L$ield.ne"t)8L$ield.prev / 3
(node)8L$ield.prev! 3
(node)8L$ield.ne"t / (node)! 3
(node)8L$ield.prev / I((node)8L$ield.ne"t)! 3
} Mhile ( /* */ 4)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)/
typede$ struct Mth%t
{
int state!
O9*(?(Mth%t) alist!
} Mth%t!
#de$ine O9*(?(type) 3
struct { 3
struct type *ne"t! 3
struct type **prev! 3
}
After Preprocessing and Compiling
typede$ struct Mth%t {
int state!
struct {
struct Mth%t *ne"t!
struct Mth%t **prev!
} alist!
} Mth%t!
)integer* st"te
)address* ne(t
)address* +re,
3 -rds in #e#r%
0
0(00100
0(00104
0(100
head. inst"nce ' -th/t
0(104
0(100
memory layout after GCC
CPP
QNODE_INIT(head, alist)
#de$ine O9*(?%)9)'(node, $ield) 3
do { 3
(node)8L$ield.ne"t / (node)! 3
(node)8L$ield.prev / I(node)8L$ield.ne"t!3
} Mhile ( /* */ 4 )!
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
),
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
R>2#% 7anipulations
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
!
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
))
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDa)istE(re* F 0head2CDa)istE(re*3 A
0head2CDa)istE(re* F G0node2CDa)istEnext3A
0node2CDa)istEnext F 0head23 A
H 6hi)e 0I/ /I02
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
0(100
0
0xa0
0(104
head
0(104
0(100
0(1"0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
R>2#% 7anipulations
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)6
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
0(100
0
0(1"0
0(104
head
0(104
0(100
0(1"0
0
0(1"0
0x0"
node0
0(1"4
0(1"0
R>2#% 7anipulations
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)D
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
0(100
0
0(1"0
0xa"
head
0(104
0(100
0(1"0
0
0(1"0
0(104
node0
0(1"4
0(1"0
R>2#% 7anipulations
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)?
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
0(100
0
0(1"0
0(1"4
head
0(104
0(100
0(1"0
0
0x00
0(104
node0
0(1"4
0(1"0
R>2#% 7anipulations
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)3
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0x00
0
0(100
0(104
head
0(104
0(100
0xa0
0
0(1"0
0(1"4
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde07 "&ist)8
0x00
0
0xa0
0xa"
head
0x0"
0(100
0xa0
0
0x00
0x0"
node0
0xa"
0(1"0
R>2#% 7anipulations
before
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
).
Adding a +hird >ode
0x00
0
0(1"0
0(1"4
head
0(104
0(100
0xa0
0
0(100
0(104
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde17 "&ist)8
0x#00
0
0(200
0(204
node1
0(204
0(200
;de9ine <INSERT=>E?ORE0head@ node@ alist2A
do B A
/0head2CDalistE(re* F 0node23 A
0node2CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node2CDalist.next3 A
0node2CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0(200
0
0(200
0(204
node1
0(204
0(200
0(100
0
0(1"0
0(1"4
head
0(104
0(100
0(1"0
0
0(100
0(104
node0
0(1"4
0(1"0
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)=
Adding a +hird >ode
0x00
0
0(1"0
0(1"4
head
0(104
0(100
0xa0
0
0(100
0(104
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde17 "&ist)8
0(100
0
0(1"0
0(1"4
head
0(104
0(100
0(1"0
0
0x#00
0(104
node0
0(1"4
0(1"0
0x#00
0
0(200
0(204
node1
0(204
0(200
;de9ine <INSERT=>E?ORE0head@ node1@ alist2A
do B A
/0head2CDalistE(re* F 0node123 A
0node12CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node12CDalist.next3 A
0node12CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0x#00
0
0(200
0(204
node1
0(204
0(200
(1)
(1)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
)-
Adding a +hird >ode
0x00
0
0(1"0
0(1"4
head
0(104
0(100
0xa0
0
0(100
0(104
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde17 "&ist)8
0(100
0
0(1"0
0(1"4
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(200
0(204
node1
0(204
0(200
;de9ine <INSERT=>E?ORE0head@ node1@ alist2A
do B A
/0head2CDalistE(re* F 0node123 A
0node12CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node12CDalist.next3 A
0node12CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0(200
0
0(200
0xa"
node1
0(204
0(200
(1)
(2)
(2)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6/
Adding a +hird >ode
0x00
0
0(1"0
0(1"4
head
0(104
0(100
0xa0
0
0(100
0(104
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde17 "&ist)8
0(100
0
0(1"0
0x#0"
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(200
0(204
node1
0(204
0(200
;de9ine <INSERT=>E?ORE0head@ node1@ alist2A
do B A
/0head2CDalistE(re* F 0node123 A
0node12CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node12CDalist.next3 A
0node12CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0(200
0
0(200
0(1"4
node1
0(204
0(200
(1)
(1)
(2)
(2)
(3)
(3)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6,
Adding a +hird >ode
0x00
0
0(1"0
0(1"4
head
0(104
0(100
0xa0
0
0(100
0(104
node0
0(1"4
0(1"0
123SE45/6EFO4E(he"d7 nde17 "&ist)8
0(100
0
0(1"0
0(204
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(200
0(204
node1
0(204
0(200
;de9ine <INSERT=>E?ORE0head@ node1@ alist2A
do B A
/0head2CDalistE(re* F 0node123 A
0node12CDalistE(re* F 0head2CDalistE(re*3 A
0head2CDalistE(re* F G0node12CDalist.next3 A
0node12CDalistEnext F 0head23 A
H 6hi)e 0I/ /I02
0(200
0
0x00
0(1"4
node1
0(204
0(200
(1)
(1)
(2)
(2)
(3)
(3)
(4)
(4)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6)
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
0x00
0
!!
!!
head
0x0"
0(100
0xa0
0
!!
!!
node0
0xa"
0(1"0
0x#00
0
!!
!!
node1
0x#0"
0(200
;de9ine <RE$OVE0node@ alist2 A
do B A
0-2 /00node2CDalistE(re*2 F 0node2CDalistEnext3 A
0:2 00node2CDalistEnext2CDalistE(re* F 0node2CDalistE(re*3A
032 0node2CDalistEnext F 0node23 A
0&2 0node2CDalistE(re* F G00node2CDalistEnext23 A
H 6hi)e 0 I/ /I 02
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
66
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
;de9ine <RE$OVE0node@ alist2 A
do B A
/00node2CDalistE(re*2 F 0node2CDalistEnext3 A
00node2CDalistEnext2CDalistE(re* F 0node2CDalistE(re*3A
0node2CDalistEnext F 0node23 A
0node2CDalistE(re* F G00node2CDalistEnext23 A
H 6hi)e 0 I/ /I 02
0(100
0
0(1"0
0(204
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0(200
0
0x00
0(1"4
node1
0(204
0(200
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6D
0(100
0
0x#00
0(204
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0(200
0
0(100
0(1"4
node1
0(204
0(200
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
;de9ine <RE$OVE0node0@ alist2 A
do B A
(1) *((node0)->alist.prev) = (node0)->alist.next;
00node02CDa)istEnext2CDa)istE(re* F 0node02CDa)istE(re*3A
0node02CDa)istEnext F 0node023 A
0node02CDa)istE(re* F G00node02CDa)istEnext23A
H 6hi)e 0 I/ /I 02
()
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6?
0(100
0
0(200
0(204
head
0(104
0(100
0(1"0
0
0(200
0(104
node0
0(1"4
0(1"0
0(200
0
0(100
0x0"
node1
0(204
0(200
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
;de9ine <RE$OVE0node0@ alist2 A
do B A
*((node0)->alist.prev) = (node0)->alist.next;
(2) 00node02CDa)istEnext2CDa)istE(re* F 0node02CDa)istE(re*3A
0node02CDa)istEnext F 0node023 A
0node02CDa)istE(re* F G00node02CDa)istEnext23 A
H 6hi)e 0 I/ /I 02
(#)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
63
0(100
0
0(200
0(204
head
0(104
0(100
0(1"0
0
0xa0
0(104
node0
0(1"4
0(1"0
0(200
0
0(100
0(104
node1
0(204
0(200
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
;de9ine <RE$OVE0node0@ alist2 A
do B A
*((node0)->alist.prev) = (node0)->alist.next;
00node02CDa)istEnext2CDa)istE(re* F 0node02CDa)istE(re*3A
(3) 0node02CDa)istEnext F 0node023 A
0node02CDa)istE(re* F G00node02CDa)istEnext23 A
H 6hi)e 0 I/ /I 02
($)
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6.
0(100
0
0x#00
0(204
head
0(104
0(100
0(1"0
0
0(1"0
0xa"
node0
0(1"4
0(1"0
0(200
0
0(100
0(104
node1
0(204
0(200
Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
;de9ine <RE$OVE0node0@ alist2 A
do B A
*((node0)->alist.prev) = (node0)->alist.next;
00node02CDa)istEnext2CDa)istE(re* F 0node02CDa)istE(re*3A
0node02CDa)istEnext F 0node023 A
(4) 0node02CDa)istE(re* F G00node02CDa)istEnext23 A
H 6hi)e 0 I/ /I 02
(")
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6=
$olution to Remo(ing a >ode
0x00
0
0(1"0
0(204
head
0(104
0(100
0xa0
0
0(200
0(104
node0
0(1"4
0(1"0
0x#00
0
0(100
0(1"4
node1
0(204
0(200
14E9O:E(nde07 "&ist)8
0x00
0
0x#00
0x#0"
head
0x0"
0(100
0xa0
0
0xa0
0xa"
node0
0xa"
0(1"0
0x#00
0
0x00
0x0"
node1
0x#0"
0(200
;de9ine <RE$OVE0node@ alist2 A
do B A
0-2 /00node2CDalistE(re*2 F 0node2CDalistEnext3 A
0:2 00node2CDalistEnext2CDalistE(re* F 0node2CDalistE(re*3A
032 0node2CDalistEnext F 0node23 A
0&2 0node2CDalistE(re* F G00node2CDalistEnext23 A
H 6hi)e 0 I/ /I 02
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
6-
Functions
* Alwa!s use function protot!pes
int my$unc (char *, int, struct Cy&truct *)!
int my$unc%noargs (void)!
void my$unc%noreturn (int i)!
* C and C88 are call ! (alue" cop! of parameter passed to function
0 C88 permits !ou to specif! pass ! reference
0 if !ou want to alter the parameter then pass a pointer to it 4or use
references in C885
* If performance is an issue then use inline functions" generall!
etter and safer than using a macro. Common con(ention
0 define protot!pe and function in header or name.i file

static inline int myin$unc (int i, int ;)!

static inline int myin$unc (int i, int ;) { ... }


Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D/
Basic +!pes and 2perators
* Basic data t!pes
0 +!pes9 char" int" float and doule
0 Rualifiers9 short" long" unsigned" signed" const
* Constant9 /@,)6D" ,)" B$ome stringC
* %numeration9
0 >ames in different enumerations must e distinct

enum Pee:(ay%t {Con, 'ue, Ped, 'hur, @ri}!


enum Pee:end(ay%t {&at / 4, &un / 0}!
* Arithmetic9 8" M" Q" H" S
0 prefi@ 88i or MMi T incrementHdecrement efore (alue is used
0 postfi@ i88" iMMT incrementHdecrement after (alue is used
* Relational and logical9 U" V" UW" VW" WW" XW" ;;" YY
* Bitwise9 ;" Y" Z 4@or5" UU" VV" [4ones complement5
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D,
2perator Precedence 4from BC a Reference 7anualC" ?
th
%dition5
!
o
"
e
n
s
#
p
e
r
a
t
o
r
$
l
a
s
s
%
r
e
c
e
d
e
n
c
e
&
s
s
o
c
i
a
t
e
s
names,
literals
si#()e to8ens (ri#ar%
1'
nIa
a/J0 s57sri(ting (ost9ix )e9tCtoCright
f(...) 95ntion a)) (ost9ix )e9tCtoCright
. diret se)etion (ost9ix )e9tCtoCright
*> indiret se)etion (ost9ix )e9t to right
)) ** inre#ent@ dere#ent post(ix )e9tCtoCright
(type){init% o#(o5nd )itera) (ost9ix )e9tCtoCright
)) ** inre#ent@ dere#ent pre(ix
1)
rightCtoC)e9t
si6eof si+e 5nar% rightCtoC)e9t
K 7it6ise not 5nar% rightCtoC)e9t
L )ogia) not 5nar% rightCtoC)e9t
* ) negation@ ()5s 5nar% rightCtoC)e9t
5 address o9 5nar% rightCtoC)e9t
*
indiretion
0dereference2
5nar% rightCtoC)e9t
!
o
"
e
n
s
#
p
e
r
a
t
o
r
$
l
a
s
s
%
r
e
c
e
d
e
n
c
e
&
s
s
o
c
i
a
t
e
s
(type) asts 5nar% 14 rightCtoC)e9t
* / . #5)ti()iati*e 7inar% 13 )e9tCtoCright
) * additi*e 7inar% 12 )e9tCtoCright
<< >> )e9t@ right shi9t 7inar% 11 )e9tCtoCright
< <- > >- re)ationa) 7inar% 10 )e9tCtoCright
-- L- eJ5a)it%IineJE 7inar% * )e9tCtoCright
5 7it6ise and 7inar% 8 )e9tCtoCright
M 7it6ise xor 7inar% + )e9tCtoCright
N 7it6ise or 7inar% ' )e9tCtoCright
55 )ogia) and 7inar% ) )e9tCtoCright
NN )ogia) or 7inar% 4 )e9tCtoCright
OP onditiona) ternar% 3 rightCtoC)e9t
- )- *-
*- /- .-
5- M- N-
<<- >>-
assign#ent 7inar% 2 rightCtoC)e9t
' seJ5entia) e*a)E 7inar% 1 )e9tCtoCright
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D)
$tructs and 'nions
* structures

struct CyDoint {int ", int y}!

typede$ struct CyDoint CyDoint%t!

CyDoint%t point, *ptr!

point." / 4!point.y / 64!

ptr / Ipoint! ptr8L" / 6.! ptr8Ly / 04!


* unions

union CyBnion {int "! CyDoint%t pt! struct {int


7! char c[0]} &!}!

union CyBnion "!


0 Can onl! use one of the elements. 7emor! will e allocated for
the largest element
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D6
Conditional $tatements 4ifHelse5
if (a Q 64)
print$(Fa is less than 643nH)!
else if (a // 64)
print$(Fa is 643nH)!
else
print$(Fa is greater than 643nH)!
* If !ou ha(e compound statements then use rackets 4locks5

if (a Q 0 II b L 64) {
c / a * b! b / 4!
print$(Fa / Gd, a32s address / 4"G4R"3nH, a, (uint7.%t)Ia)!
} else {
c / a < b! b / a!
}
* +hese two statements are ePui(alent9

if (a) " / 7! else if (b) " / .! else " / 4!

if (a) " / 7! else {if (b) " / .! else " / 4!}


* Is this correctO

if (a) " / 7! else if (b) " / .!


else (J) " / 4! else " / 8.!
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
DD
Conditional $tatements 4switch5
int c / 64!
sQitch (c) {
case 45
print$(Fc is 43nH)!
breaJ!
...
default5
print$(FBn:noMn value o$ c3nH)!
breaJ!
}
* &hat if we lea(e the reak statement outO
* #o we need the final reak statement on the default caseO
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D?
Loops
* flow control
0 break 0 e@it innermost loop
0 continue 0 perform ne@t iteration of loop
* >ote" all these forms permit one statement to e e@ecuted. B!
enclosing in rackets we create a lock of statements.
for (i / 4! i Q CASTA,B?! i<<) {
doMor:()!
}
Qhile (c N/ 6.) {
doMor:()!
}
do {
doMor:()!
} Qhile (c Q 6.)!
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D3
Building !our program
* For all las and programming assignments9
0 !ou must suppl! a make file
0 !ou must suppl! a R%A#7% file that descries the assignment
and results. +his must e a te@t file" no 7$ word.
0 of course the source code and an! other liraries or utilit!
code !ou used
0 !ou ma! sumit plots" the! must e postscript or pdf
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D.
make and 7akefiles" 2(er(iew
* &h! use makeO
0 con(enience of onl! entering compile directi(es once
0 make is smart enough 4with !our help5 to onl! compile and link modules
that ha(e changed or which depend on files that ha(e changed
0 allows !ou to hide platform dependencies
0 promotes uniformit!
0 simplifies m! 4and hopefull! !our5 life when testing and (erif!ing !our
code
* A makefile contains a set of rules for uilding a program
target ... 9 prerePuisites ...
command
...
* $tatic pattern rules.
0 each target is matched against targetMpattern to deri(e stem which is
used to determine prerePs 4see e@ample5
targets ... 9 targetMpattern 9 prerePMpatterns ...
command
...
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D=
7akefiles
* #efining (ariales
Cy*D& 5/ 8(P'+
Cy()U V/ /home/$red
CyTar / W(&+?,,)
* 'sing (ariales
Cy@,AX& 5/ W(Cy*D&)
* BuiltMin \ariales
0 ]@ W filename of target
0 ]U W name of the first prerePuisites
* Patterns
0 use S character to determine stem
0 foo.o matches the pattern S.o with foo as the stem.
0 foo.o moo.o 9 S.o 9 S.c N sa!s that foo.o depends on foo.c and
moo.o depends on moo.c
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
D-
%@ample 7akefile for wuli
# Dro;ect speci$ic
include ../Ca:e$ile.inc
)9Y,B(?& / W{PB)9Y,B(?&} ).
,)-& / W{P),)-&} W{*&,)-&}
Y@,AX& / W{PBY,@AX&} (PB(?-BX
YY / W{PBYY}
+(U& 5/ util.h
Y&UY& 5/ testapp6.c testapp..c
&UY& 5/ util.c callout.c
Y*-Z& / W(addpre$i" W{*-Z()U}/, 3
W(patsubst G.c,G.o,W(Y&UY&)))
*-Z& / W(addpre$i" W{*-Z()U}/, 3
W(patsubst G.c,G.o,W(&UY&)))
YC(& / W(addpre$i" W{*-Z()U}/, W(basename W(Y&UY&)))
all 5 W(*-Z()U) W(YC(&)
install 5 all
W(*-Z()U) 5
m:dir W(*-Z()U)
W(*-Z&) W(Y*-Z&) 5 W{*-Z()U}/G.o 5 G.c W(+(U&)
W{YY} W{Y@,AX&} W{)9Y,B(?&} o W[ 8c WQ
W(YC(&) 5 W{*-Z()U}/G 5 W{*-Z()U}/G.o W(*-Z&)
W{YY} W{Y@,AX&} 8o W[ W[.o W{,)-&}
chmod 4\== W[
clean 5
/bin/rm 8$ W(YC(&) W(*-Z&)
# Ca:e$ile.inc
# Yontains common de$initions
Cy*& 5/ W(shell uname 8s)
Cy)( 5/ W(shell Mhoami)
Cy+ost 5/ W(shell hostname)
PAU9&'U)Y' 5/ 8P 3
8Pstrict8
prototypes 3
8Pmissing8prototypes
PAU9,)X+' 5/ 8Pall
PAU9 5/ W{PAU9,)X+'}
A,,@,X& 5/ 8(%X9B%&*BUY? 3
8(%U??9'UA9' 3
8(%'+U?A(%&A@?
ADDY@,X& / W(A,,@,X&) 3
W(PAU9)
PBYY 5/ gcc
PBY@,AX& 5/ 8(Cy*&/W(Cy*&) 3
W(*&@,AX&) 3
W(A,,@,X&) W(PAU9)
PB)9Y,B(?& 5/
PB,)-& 5/ 8lm
i$e> (W{Cy*&), &un*&)
*&,)-&</ 8lrt
endi$
9";e'i&e<inc 9";e'i&e
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
?/
ProAect #ocumentation
* R%A#7% file structure
0 Section A9 Introduction
descrie the proAect" paraphrase the rePuirements and state !our
understanding of the assignments (alue.
0 Section B9 #esign and Implementation
List all files turned in with a rief description for each. %@plain !our
design and pro(ide simple psuedoMcode for !our proAect. Pro(ide a simple
flow chart of !ou code and note an! constraints" in(ariants" assumptions
or sources for reused code or ideas.
0 Section C9 Results
For each proAect !ou will e gi(en a list of Puestions to answer" this is
where !ou do it. If !ou are not satisfied with !our results e@plain wh!
here.
0 Section D9 Conclusions
&hat did !ou learn" or not learn during this assignment. &hat would !ou
do differentl! or what did !ou do well.
Fred Kuhns (
06/27/14)
CSE332 Object Oriented
Pr!r"##in! $"b
?,
Attacking a ProAect
* RePuirements and scope9 Identif! specific rePuirements and or goals.
Also note an! design andHor implementation en(ironment
rePuirements.
0 knowing when !ou are done" or not done
0 estimating effort or areas which rePuire more research
0 programming language" platform and other de(elopment en(ironment
issues
* Approach9 :ow do !ou plan to sol(e the prolem identified in the first
step. #e(elop a protot!pe design and document. >e@t figure out how
!ou will (erif! that !ou did satisf! the rePuirementsHgoals. #esigning
the tests will help !ou to etter understand the prolem domain and
!our proposed solution
* Iterati(e de(elopment9 It is good practice to uild !our proAect in
small pieces. +esting and learning as !ou go.
* Final +ouches9 Put it all together and run the tests identified in the
approach phase. \erif! !ou met rePuirements. Polish !ou code and
documentation.
* +urn it in9

You might also like