Spr23 - 02 - Git - C
Spr23 - 02 - Git - C
@synkevych
@pawel_czerwinski
1
@afgprogrammer
Agenda
2
Revision Control Systems
Problems often faced by programmers:
•Help! I’ve deleted my code! How do I get it back?
•How can I try out one way of writing this function, and go back if it doesn’t work?
•Help! I’ve introduced a subtle bug that I can’t find. How can I see what I’ve
changed since the last working version?
•How do I work with source code on multiple computers?
•How do I work with others (e.g., a COS 217 partner) on the same program?
•What changes did my partner just make?
•If my partner and I make changes to different parts of a program,
how do we merge those changes?
5
https://xkcd.com/1296/
Local vs. Remote Repositories
LOCAL REPOSITORY
git push
REMOTE REPOSITORY
• Located in .git directory
• Only accessible from the • Located in the cloud
current computer E.g., github.com
• Commit early, commit often – • Can clone working copies
you can only go back to on multiple machines
versions you’ve committed • Any clone can pull the
git clone
• Can push current state (i.e., git pull current state
complete checked-in history)
to a remote repository
6
COS 217 🧡 GitHub
We distribute assignment code through a github.com repo
• But you can’t push to our repo!
Need to create your own (private!) repo for each assignment
• Two methods in git primer handout
• One clone on armlab, to test and submit
• If developing on your own machine, another clone there:
be sure to commit and push "up" to github,
then pull "down" onto armlab
7
Agenda
8
The C Programming Language
Algol
LISP Smalltalk
10
C vs. Java: Design Goals
C Design Goals (1972) Java Design Goals (1995)
Build the Unix OS Language of the Internet
Low-level; close to HW High-level; insulated from
and OS hardware and OS
Good for system-level Good for application-level
programming programming
Support structured Support object-oriented
programming programming
Unsafe: don’t get in the Safe: can’t step
programmer’s way “outside the sandbox”
Look like C!
11
Agenda
12
Building Java Programs
$ javac MyProg.java Java compiler
(machine lang code)
HW (ArmLab)
OS (Linux)
MyProg.java MyProg.class
javac
(Java code) (bytecode)
13
Running Java Programs
$ java MyProg Java interpreter /
“virtual machine”
(machine lang code)
HW (ArmLab)
OS (Linux)
MyProg.class
(bytecode)
14
Building C Programs
$ gcc217 myprog.c –o myprog C “Compiler driver”
(machine lang code)
HW (ArmLab)
OS (Linux)
myprog.c myprog
gcc217
(C code) (machine lang code)
15
Running C Programs
$ ./myprog myprog
(machine lang code)
HW (ArmLab)
OS (Linux)
16
Agenda
17
Java vs. C: Portability
Program Code Type Portable?
MyProg.java Java source code Yes
myprog.c C source code Mostly
(For example, COS 217 has used many architectures over the years,
and every time we've switched, all our programs have had to be recompiled!)
18
Java vs. C: Safety & Efficiency
Java
•null reference checking
•Automatic array-bounds checking
•Automatic memory management (garbage collection)
•Other safety features
C
•NULL pointer checking,
•Manual bounds checking
•Manual memory management
A.
B.
C.
Java vs. C: Details
22
Java vs. C: Details
Java C
Hello.java: hello.c:
public class Hello #include <stdio.h>
Overall { public static void main
(String[] args) int main(void)
Program
{ System.out.println( { printf("hello, world\n");
Structure return 0;
"hello, world");
} }
}
23
Java vs. C: Details
Java C
Character type char // 16-bit Unicode char /* 8 bits */
byte // 8 bits (unsigned, signed) char
short // 16 bits (unsigned, signed) short
Integral types
int // 32 bits (unsigned, signed) int
long // 64 bits (unsigned, signed) long
float
Floating point float // 32 bits
double
types double // 64 bits
long double
/* no equivalent */
Logical type boolean
/* use 0 and non-0 */
Generic pointer Object void*
type
#define MAX 1000
Constants final int MAX = 1000; const int MAX = 1000;
enum {MAX = 1000};
24
Java vs. C: Details
Java C
int [] a = new int [10]; int a[10];
Arrays float [][] b = float b[5][20];
new float [5][20];
Array bound // run-time check /* no run-time check */
checking
// Object reference is an
Pointer type int *p;
// implicit pointer
class Mine struct Mine
{ int x; { int x;
Record type
float y; float y;
} };
25
Java vs. C: Details
Java C
String s1 = "Hello"; char *s1 = "Hello";
Strings String s2 = new char s2[6];
String("hello"); strcpy(s2, "hello");
String s1 + s2 #include <string.h>
concatenation s1 += s2 strcat(s1, s2);
Logical ops * &&, ||, ! &&, ||, !
Relational ops * ==, !=, <, >, <=, >= ==, !=, <, >, <=, >=
=, +=, -=, *=, /=, %=, =, +=, -=, *=, /=, %=,
Assignment ops <<=, >>=, >>>=, &=, ^=, |= <<=, >>=, &=, ^=, |=
26
* Essentially the same in the two languages
Java vs. C: Details
Java C
if (i < 0) if (i < 0)
statement1; statement1;
if stmt *
else else
statement2; statement2;
switch (i) switch (i)
{ case 1: { case 1:
... ...
break; break;
case 2: case 2:
switch stmt * ... ...
break; break;
default: default:
... ...
} }
27
* Essentially the same in the two languages
Java vs. C: Details
Java C
int i;
for (int i=0; i<10; i++)
for stmt for (i=0; i<10; i++)
statement;
statement;
while (i < 0) while (i < 0)
while stmt * statement; statement;
do do
do-while stmt * statement; statement;
while (i < 0) while (i < 0);
continue stmt * continue; continue;
labeled continue continue someLabel; /* no equivalent */
stmt
break stmt * break; break;
labeled break break someLabel; /* no equivalent */
stmt
28
* Essentially the same in the two languages
Java vs. C: Details
Java C
return 5; return 5;
return stmt * return; return;
{ {
Compound stmt statement1; statement1;
(alias block) * statement2; statement2;
} }
/* comment */ /* comment */
Comments // another kind
f(x, y, z);
Method / function someObject.f(x, y, z); f(x, y, z);
call
SomeClass.f(x, y, z);
31
The charcount Program
Functionality:
•Read all characters from standard input stream
•Write to standard output stream the number of characters read
stdin stdout
Line 1
charcount ??
Line 2
32
The charcount Program
The program: charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */
int main(void) {
int c;
int charCount = 0;
c = getchar();
while (c != EOF) {
charCount++;
c = getchar();
}
printf("%d\n", charCount);
return 0;
33 }
charcount Building and Running
$ gcc217 charcount.c
$ ls
. .. a.out
$ gcc217 charcount.c -o charcount
$ ls
. .. a.out
charcount
$
34
charcount Building and Running
What is this?
What is the effect?
What is printed?
35
charcount Building and Running
Includes visible
characters plus
two newlines
36
charcount Building and Running
$ cat somefile
Line 1
Line 2
$ ./charcount < somefile
14
$
What is this?
What is the effect?
37
charcount Building and Running
What is this?
What is the effect?
38
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */ Execution begins at
int main(void) main() function
{ int c;
int charCount = 0; • No classes in the C
c = getchar();
while (c != EOF)
language.
{ charCount++;
c = getchar();
}
printf("%d\n", charCount);
return 0;
}
39
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h> We allocate space for
/* Write to stdout the number of
chars in stdin. Return 0. */ c and charCount
int main(void) in the stack section of
{ int c;
int charCount = 0; memory
c = getchar();
while (c != EOF)
{ charCount++; Why int
c = getchar(); instead of char?
}
printf("%d\n", charCount);
return 0;
}
40
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */ getchar() tries to read char
int main(void)
{ int c;
from stdin
int charCount = 0; • Success ⇒ returns that
c = getchar();
while (c != EOF)
char value (within an int)
{ charCount++; • Failure ⇒ returns EOF
c = getchar();
}
printf("%d\n", charCount);
return 0;
}
41
EOF is a special value,
distinct from all possible chars
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */
int main(void)
{ int c; Assuming c ≠ EOF,
int charCount = 0; we increment
c = getchar();
while (c != EOF) charCount
{ charCount++;
c = getchar();
}
printf("%d\n", charCount);
return 0;
}
42
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */
int main(void)
{ int c;
int charCount = 0; We call getchar()
c = getchar(); again and recheck
while (c != EOF)
{ charCount++; loop condition
c = getchar();
}
printf("%d\n", charCount);
return 0;
}
43
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */
int main(void) • Eventually getchar()
{ int c;
int charCount = 0;
returns EOF
c = getchar(); • Loop condition fails
while (c != EOF) • We call printf()
{ charCount++;
c = getchar(); to write final
} charCount
printf("%d\n", charCount);
return 0;
}
44
Running charcount
Run-time trace, referencing the original C code…
charcount.c
#include <stdio.h>
/* Write to stdout the number of
chars in stdin. Return 0. */ • return statement returns
int main(void)
{ int c; to calling function
int charCount = 0; • return from main()
c = getchar();
while (c != EOF) returns to _start,
{ charCount++; terminates program
c = getchar();
}
printf("%d\n", charCount);
return 0;
}
#include <stdlib.h>
45
Normal execution ⇒ 0 or EXIT_SUCCESS ß to use these constants
Abnormal execution ⇒ EXIT_FAILURE
Coming up next …
@christianlue