Title: Welcome%20to%20PLAB
1Welcome to PLAB
2Course Staff
- Teacher
- Nir Friedman
- Teaching Assistants
- Yoseph Barash
- Liad Blumrosen
- Michael Okun
3Communications
- WWWhttp//www.cs.huji.ac.il/plab
- Email Personal questions should be sent only to
plab_at_cs - Newsgroups
- local.course.plab.stud
- local.course.plab.ta (moderated)
4Course Objectives
- Procedural programming language (C).
- Pointers (C/C)
- Generic programming (C templates)
- Design patterns (STL, streams, and more)
- Practice of programming
- Style
- Testing Debugging
- Efficiency Portability
- Modularity
5Books
- The C Programming Language , 2nd Edition,
Brian W. Kernighan Dennis M.Ritchie - C Programming Language, 3rd Edition, Bjarne
Strousrtup - The C Primer, Stanley Lippman
- C for Java Programmers, Timothy Budd
- C How to Program, Harvey Deitel Paul Deitel
- The Practice of Programming, Brian W. Kernighan
Rob Pike - Programming Pearls 2nd Edition, Jon Bentley
6Course Grading
- 6-7 programming exercises
- Mid-term exam
- Final exam
- Final grade 60 exercises and 40 exam.
- Shortage in computers - dont wait for the last
minute.
7History
70s Development of UNIX. (RichieKernigham
Bell labs)
80s Large efficient code. (Stroustrup Bell
labs)
90s Language for the web. (Sun Microsystems)
Advanced Programming
Simple to convert to machine code.
Fast and Efficient
Secure and Safe
Welcome to Plab
Easy to avoid bugs Easy to Debug
8C Design Decisions
- Bare bones the language leaves maximal
flexibility with the programmer - Efficient code (operating systems)
- Full control on memory CPU usage
- High-level
- Type checking
- High-level constructs
- Portable
- Standard language definition
- Standard library
9C Warning Signs
- No run-time checks
- Array boundary overruns
- Illegal pointers
- No memory management
- Programmer has to manage memory
10C - OO extension of C
- Classes methods
- OO design of classes
- Generic programming
- Template allow for code reuse
- Stricter type system
- Some run-time checks memory control
11First Program in C
- // This is a comment
- // This line defines standard I/O library
- include ltstdio.hgt
- // main name of the main part of the program
- int
- main()
- // define a block
- printf("Hello class!\n")
- return 0
-
12Compiling Running
- gt g -o hello hello.c
- gt hello
- Hello class!
- gt
13Second Program
- include ltstdio.hgt
- int
- main()
-
- int i // declares i as an integer
- int j 0 // declares j as an integer, and
initializes it to 0 - // for( initial test condition update step
) - for( i 0 i lt 10 i )
-
- j i // shorthand for j j i
- printf("d d d\n", i, j, (i(i1))/2)
- return 0
-
14Running
- gt g -o loop loop.c
- gt loop
- 0 0 0
- 1 1 1
- 2 3 3
- 3 6 6
- 4 10 10
- 5 15 15
- 6 21 21
- 7 28 28
- 8 36 36
- 9 45 45
15Character Input/Output
- include ltstdio.hgt
- int
- main()
-
- char c
- while( (c getchar()) ! EOF )
- putchar(c)
- return 0
-
16Print header
- include ltstdio.hgt
- define HEADER 10
- int main()
-
- int n 0
- char c
- while( ((c getchar()) ! EOF) (n lt HEADER)
) -
- putchar(c)
- if( c '\n' )
- n
-
- return 0
-
17Functions
- C allows to define functions
- Syntax
- int
- power( int a, int b )
-
-
- return 7
Parameter declaration
Return type
Return statement
18Procedures
- Functions that return void
- void
- proc( int a, int b )
-
-
- return
-
Return w/o value(optional)
19Example printing powers
- include ltstdio.hgt
- int
- power( int base, int n )
-
- int i, p
- p 1
- for( i 0 i lt n i )
- p p base
- return p
- int
- main()
-
- int i
- for( i 0 i lt 10 i )
- printf("d d d\n",
- i,
- power(2,i),
- power(-3,i) )
- return 0
20Functions Declaration
Rule 1 A function knows only functions which
were defined above it.
void funcA() ... void funcB()
funcA() void funcC() funcB()
funcA() funcB()
void funcA() ... void funcB()
funcC() void funcC() funcA()
Error funcC is not known yet.
21Functions Declaration
Amendment to Rule 1 Use forward declarations.
void funcC(int param) // or void
funcC(int) void funcA() ... void
funcB() . funcC(7) void funcC(int
param) .
22Powers revisted
- include ltstdio.hgt
- // Forward decleration
- int power( int m, int n)
- int main()
-
- int i
- for( i 0 i lt 10i )
- printf("d d d\n",
- i, power(2,i),
- power(-3,i) )
- return 0
-
- int
- power( int base, int n )
-
- int i, p
- p 1
- while( n gt 0 )
-
- if( n 2 )
- p base
- base base
- n / 2
-
- return p
-
23Program Style
- Readability
- Common Sense
- Clarity
- Right focus
24Whats in a name
- Example
- define ONE 1
- define TEN 10
- define TWENTY 20
- More reasonable
- define INPUT_MODE 1
- define INPUT_BUFSIZE 10
- define OUTPUT_BUFSIZE 20
25Whats in a name
- Use descriptive names for global variables
- int npending 0 // current length of input
queue - Naming conventions vary
- numPending
- num_pending
- NumberOfPendingEvents
-
26Whats in a name
- Compare
- for( theElementIndex 0
- theElementIndex lt numberOfElements
- theElementIndex )
- elementArraytheElementIndex
theElementIndex - and
- for( i 0 i lt nelems i )
- elemi i
- Use short names for locals
27Whats in a name
- Consider
- int noOfItemsInQ
- int frontOfTheQueue
- int queueCapacity
-
- The word queue appears in 3 different ways
- Be Consistent
- Follow naming guidelines used by your peers
28Whats in a name
- Use active name for functions
- now getDate()
- Compare
- if( checkdigit(c) )
- to
- if( isdigit(c) )
- Accurate active names makes bugs apparent
29Indentation
- Use indentation to show structure
- Compare
- for(n n lt100 fieldn 0)
- c 0 return \n
- to
- for( n n lt100 n)
- fieldn 0
- c 0
- return \n
30Expressions
- Use parens to resolve ambiguity
- Compare
- leap_year y 4 0 y 100 ! 0
- y 400 0
- to
- leap_year ((y 4 0) (y 100 ! 0))
- (y 400 0)
31Statements
- Use braces to resolve ambiguity
- Compare
- if( i lt 100 )
- x i
- i
- to
- if( i lt 100 )
-
- x i
-
- i
32Idioms
- Do not try to make code interesting
- i 0
- while( i lt n 1 )
- arrayi 1
-
- for( i 0 i lt n )
- arrayi 1
-
- for( i n --i gt 0 )
- arrayi 1
-
- for( i 0 i lt n i )
- arrayi 1
This is the common idiom that any programmer
will recognize
33Idioms
- Use else if for multiway decisions
- if ( cond1 )
- statement1
- else if ( cond2 )
- statement2
-
- else if ( condn )
- statementn
- else
- default-statement
34Idioms
- if( x gt 0 )
- if( y gt 0 )
- if( xy lt 100 )
-
- ...
- else
- printf(Too large!\n" )
- else
- printf("y too small!\n")
- else
- printf("x too small!\n")
-
- if( x lt 0 )
- printf("x too small!\n")
- else if( y lt 0 )
- printf("y too small!\n")
- else if( xy gt 100 )
- printf("Sum too large!\n" )
- else
-
- ...
-
35Comments
- Dont belabor the obvious
- // return SUCCESS
- return SUCCESS
- // Initialize total to number_received
- total number_received
- Test does comment add something that is not
evident from the code
36Comments
- Introduce each function
- // random return a random integer in 0..r
- int random( int r )
-
- return (int)floor(rand()r)
37Comments
- A more elaborate function comment
- //
- // GammaGreaterThanOne( Alpha )
- //
- // Generate a gamma random variable when alpha gt
1. - //
- // Assumption Alpha gt 1
- //
- // Reference Ripley, Stochastic Simulation, p.90
- // Chang and Feast, Appl.Stat. (28) p.290
- //
- double GammaGreaterThanOne( double Alpha )
-
-
38Comments
- Dont comment bad code rewrite it!
-
- // If result 0 a match was found so return
- // true otherwise return false
- return !result
- Instead
-
- return matchfound
39Style recap
- Descriptive names
- Clarity in expressions
- Straightforward flow
- Readability of code comments
- Consistent conventions idioms
40Why Bother?
- Good style
- Easy to understand code
- Smaller polished
- Makes errors apparent
- Sloppy code ?bad code
- Hard to read
- Broken flow
- Harder to find errors correct them