week11 - clark science center · •simple types • no string type! • no booleans (only 0 for...
TRANSCRIPT
mith College
Computer Science
Dominique Thiébaut [email protected]
CSC231Week 11— Introduction to C
Fall 2019
https://kristv.com/sponsored/bay-area-bicycles/2019/02/25/from-training-wheels-to-two-wheels-tips-for-helping-kids-learn-to-ride/
hw9a.asm
test0.asm
231Lib.asm
hw9a.o
test0.o
231Lib.o
nasm
nasm
nasm
test0
ld
Outline
• Recursion: Towers of Hanoi
• C
D. Thiebaut, Computer Science, Smith College
Towers of Hanoi…in Assembly
• In Python first
• In Assembly next
https://media-cdn.tripadvisor.com/media/photo-s/0f/00/ee/18/ulun-danu-bratan-temple.jpg
D. Thiebaut, Computer Science, Smith College
https://youtu.be/2SUvWfNJSsM
What's the relationship
between Towers of Hanoi
and Serpinski?
Watch on
your own!
D. Thiebaut, Computer Science, Smith College
A B C
D. Thiebaut, Computer Science, Smith College
Write it in Python!
getcopy hanoi0.py
D. Thiebaut, Computer Science, Smith College
moveDisk( 5, Source, Dest, Extra )
moveDisk( 4, Source, Extra, Dest )
moveDisk( 1, Source, Dest, Extra )
moveDisk( 4, Extra, Dest, Source )
D. Thiebaut, Computer Science, Smith College
moveDisk( 5, Source, Dest, Extra )
moveDisk( 4, Source, Extra, Dest )
moveDisk( 1, Source, Dest, Extra )
moveDisk( 4, Extra, Dest, Source )
D. Thiebaut, Computer Science, Smith College
moveDisk( 5, Source, Dest, Extra )
moveDisk( 4, Source, Extra, Dest )
moveDisk( 1, Source, Dest, Extra )
moveDisk( 4, Extra, Dest, Source )
D. Thiebaut, Computer Science, Smith College
getcopy hanoi.asm
D. Thiebaut, Computer Science, Smith College
C for Assembly
Language Programmers
Computer Science Dominique Thiebaut
• Dennis Ritchie
• 1969 to 1973
• AT&T Bell Labs
• Close to Assembly
• Unix
• Standard
• Many languages based on C. (C++, Obj. C, C#)
• Many influenced by C (Java, Python, Perl)
Computer Science Dominique Thiebaut
• Dennis Ritchie
• 1969 to 1973
• AT&T Bell Labs
• Close to Assembly
• Unix
• Standard
• Many languages based on C. (C++, Obj. C, C#)
• Many influenced by C (Java, Python, Perl)
Computer Science Dominique Thiebaut
Missing from C…
• Exceptions
• Garbage collection
• OOP
• Polymorphism
• But…
• Exceptions
• Garbage collection
• OOP
• Polymorphism
• But… it is usually faster!
Missing from C…
Example: N-Queens Problem
http://www.science.smith.edu/dftwiki/index.php/CSC231:_N-Queens_Problem:_interpreted_vs_compiled
Example: N-Queens Problem
http://www.science.smith.edu/dftwiki/index.php/CSC231:_N-Queens_Problem:_interpreted_vs_compiled
Language Execution Time
Python 48 secC 1.4 secC with -O3 optimization 0.4 sec
Good Reference
• Essential C, by Nick Parlante, Stanford U. http://cslibrary.stanford.edu/101/EssentialC.pdf
Computer Science Dominique Thiebaut
JAVA
C
JAVA
C
POINTERS
OOP
(simplification)
Hello World!
#include <stdio.h>
int main() { printf("\nHello World\n"); return 0;}
Computer Science Dominique Thiebaut
Hello World!• Library
• Strings
• Block-structured language
• main() #include <stdio.h>
int main() { printf("\nHello World\n"); return 0;}
Computer Science Dominique Thiebaut
Hello World!• Library
• Strings
• Block-structured language
• main() #include <stdio.h>
int main() { printf("\nHello World\n"); return 0;}
Computer Science Dominique Thiebaut
getcopy C/hello.c
Compiling on Marax
• gcc Gnu compiler
• man gcc for help
[~/handout]$ gcc hello.c[~/handout]$ ./a.out
Hello World
[~/handout]$ gcc -o hello hello.c[~/handout]$ ./hello
Hello World
Computer Science Dominique Thiebaut
Files
Computer Science Dominique Thiebaut
• Write your own Hello World! program
• Make it print something like: ************ * C Rocks! * ************
Exercise
Computer Science Dominique Thiebaut
getcopy C/hello.c
[~/handout]$ gcc -o hello hello.c[~/handout]$ ./helloHello World!
• http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html
Computer Science Dominique Thiebaut
Good Reference
on C Compiler
Printing
•printf( "string with %-operators", list of vars);
•%d int
•%f float
•%s string
Computer Science Dominique Thiebaut
printf( "%3d %1.3f %s\n", 2, 3.14159, "CSC231 Assembly" );
Variables
Computer Science Dominique Thiebaut
• Simple types
• No string type!
• No booleans (only 0 for false and !0 for true)
• No classes, no objects, no OOP!
Variables
int -> integer variableshort -> short integerlong -> long integerfloat -> single precision real (floating point) variabledouble -> double precision real (floating point) variablechar -> character variable (single byte)
Computer Science Dominique Thiebaut
Comments
Computer Science Dominique Thiebaut
We stopped hereLast Time
We stopped hereLast Time
Strings(and String Functions)
Computer Science Dominique Thiebaut
Use "…" for strings
Use 'x' for characters
Strings
Computer Science Dominique Thiebaut
Strings
Computer Science Dominique Thiebaut
Strings
Computer Science Dominique Thiebaut
Strings
Computer Science Dominique Thiebaut
Strings
Computer Science Dominique Thiebaut
[~/handout/C]$ gcc strings.c[~/handout/C]$ ./a.out
sentence = hello world![~/handout/C]
Strings
Computer Science Dominique Thiebaut
A Few String Functionsstrcpy( dest, src )
copies the string pointed to by src to dest
strcat( dest, src )concatenate the string pointed to by src todest
strlen( src )returns the number of characters in thestring
more at https://www.tutorialspoint.com/c_standard_library/string_h.htm
A Few String Functionsstrstr( haystack, needle )
returns a pointer to the needle (string) in the stack (string), or NULL if not found.
strchr( haystack, char-needle )returns a pointer to the needle (char) in the stack (string), or NULL if not found.
more at https://www.tutorialspoint.com/c_standard_library/string_h.htm
Strings end with ‘\0’
Computer Science Dominique Thiebaut
'h' 'e' 'l' 'l' 'o' '\0'msg
Strings end with ‘\0’
Computer Science Dominique Thiebaut
'h' 'e' 'l' 'l' 'o' \0msg
msg2 'h' 'e' 'l' 'l' 'o' ' ' 't' 'h' 'e' 'r' 'e' '\0'
Strings end with ‘\0’
Computer Science Dominique Thiebaut
'h' 'e' 'l' 'l' 'o' \0msg
msg2 'h' 'e' 'l' 'l' 'o' ' ' 't' 'h' 'e' 'r' 'e' '\0'
msg3 'h' 'e' 'l' 'l' 'o' '\0' 't' 'h' 'e' 'r' 'e' '\0'
Strings end with ‘\0’
Computer Science Dominique Thiebaut
'h' 'e' 'l' 'l' 'o' '\0'msg
msg2 'h' 'e' 'l' 'l' 'o' ' ' 't' 'h' 'e' 'r' 'e' '\0'
msg3 'h' 'e' 'l' 'l' 'o' '\0' 't' 'h' 'e' 'r' 'e' '\0'
msg4 '\0' 'e' 'l' 'l' 'o' '\0' 't' 'h' 'e' 'r' 'e' '\0'
Strings end with ‘\0’
Computer Science Dominique Thiebaut
~/handout/C]$ ./a.outsentence = Hello world!sentence = Hello[~/handout/C]$
Strings end with ‘\0’
Computer Science Dominique Thiebaut
~/handout/C]$ ./a.outsentence = Hello world!sentence = Hello[~/handout/C]$
Strings end with ‘\0’
Computer Science Dominique Thiebaut
'h'
'e'
'l'
'l'
'o'
'\0'
Strings
are Mutable
• make the program compute your email account as the first letter of your first name and the first 8 letters of your last name.
Exercise
Computer Science Dominique Thiebaut
getcopy C/exercise1.c
#include <stdio.h> #include <string.h>
int main() { int a = 3; int b = 5; int c = 0; char firstName[] = "Mickey"; char lastName[] = "Mouse"; char account[100];
account[0] = firstName[0]; account[1] = '\0'; strcat( account, lastName ); account[8] = '\0';
printf( "%s %s, your account is: %s\n", firstName, lastName, account ); return 0; }
Solution
Computer Science Dominique Thiebaut
For-Loops
Computer Science Dominique Thiebaut
For-Loops
Computer Science Dominique Thiebaut
While-Loops
Computer Science Dominique Thiebaut
Infinite Loops
Computer Science Dominique Thiebaut
• Write a program that displays the first 10 fibonacci terms. Use a loop!
• You may use this code to get started. You can start printing 1, 2, 3 or 1, 1, 2, 3… Both solutions are fine. int fibn=1, fibn1=1, fibn2=0; fibn = fibn1 + fibn2; … printf( "%d\n", fibn );
Exercise, Part 1
Computer Science Dominique Thiebaut
• Take the solution for the previous exercise (getcopy C/fibonacci0.c) and replace the for-loop with a while loop that prints all the terms that less than 1000.
Computer Science Dominique Thiebaut
Exercise, Part 2
Symbolic Constants
Computer Science Dominique Thiebaut
Symbolic Constants
Computer Science Dominique Thiebaut
Symbolic Constants
Computer Science Dominique Thiebaut
After
preproce
ssing
Conditionals
Computer Science Dominique Thiebaut
Conditionals
Computer Science Dominique Thiebaut
&& and
|| or
! not
Conditionals (cont’d)
Computer Science Dominique Thiebaut
Computer Science Dominique Thiebaut
ints or chars, something countable
Conditionals (cont’d)
Pointers
Computer Science Dominique Thiebaut
Concept
Computer Science Dominique Thiebaut
x6.5
0FFF1F0A
px
RAM
Concept
Computer Science Dominique Thiebaut
x6.5
0FFF1F0A
px
RAM
Example: Initialize an Array
Computer Science Dominique Thiebaut
Using
indexing
3 4 1 9 3 4 6 6 1 0
0 1 2 3 4 5 6 7 8 9
A
Example: Initialize an Array
Computer Science Dominique Thiebaut
Using
indexing
Example: Initialize an Array
Computer Science Dominique Thiebaut
3 4 1 9 3 4 6 6 1 0
0 1 2 3 4 5 6 7 8 9
A
Using
pointers
Computer Science Dominique Thiebaut
Using
pointers
Example: Initialize an Array
./a.outp=0x7fff88d54560 A[0] = 0.00 *p = 0.00p=0x7fff88d54564 A[1] = 1.00 *p = 1.00p=0x7fff88d54568 A[2] = 2.00 *p = 2.00p=0x7fff88d5456c A[3] = 3.00 *p = 3.00p=0x7fff88d54570 A[4] = 4.00 *p = 4.00p=0x7fff88d54574 A[5] = 5.00 *p = 5.00p=0x7fff88d54578 A[6] = 6.00 *p = 6.00p=0x7fff88d5457c A[7] = 7.00 *p = 7.00p=0x7fff88d54580 A[8] = 8.00 *p = 8.00p=0x7fff88d54584 A[9] = 9.00 *p = 9.00
Computer Science Dominique Thiebaut
We Stopped HereLast Time