0% found this document useful (0 votes)
23 views44 pages

Spr23 - 02 - Git - C

The document discusses using Git and GitHub for version control of source code in a C programming course. It provides an overview of key concepts in version control systems including local and remote repositories, the difference between the working copy and repository, and pushing and pulling changes. It also discusses using GitHub for distributing assignment code and collaborating.

Uploaded by

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

Spr23 - 02 - Git - C

The document discusses using Git and GitHub for version control of source code in a C programming course. It provides an overview of key concepts in version control systems including local and remote repositories, the difference between the working copy and repository, and pushing and pulling changes. It also discusses using GitHub for distributing assignment code and collaborating.

Uploaded by

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

Git and GitHub … then C

@synkevych

@pawel_czerwinski
1
@afgprogrammer
Agenda

Our computing environment A taste of C


•Lecture 1 and Precepts 1 and 2: •History of C
Linux and Bash •Building and running C programs
•Lecture 2: git and GitHub •Characteristics of C
•Example program: charcount

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?

All of these problems are solved by revision control tools, e.g.:


3 git
Repository vs. Working Copy

WORKING COPY REPOSITORY


git commit
• Represents single version • Contains all checked-in
of the code versions of the code
• Plain files (e.g, .c) • Specialized format, located
• Make a coherent set of in .git directory
modifications, then • Can view commit history
commit this version of code • Can diff any versions
to the repository • Can check out any version,
• Best practice: write a git checkout ‡
by default the most recent
meaningful commit message (known as HEAD)
4 ‡
We'll rarely use checkout (see slide 6)
Relevant xkcd

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

Our computing environment A taste of C


•Lecture 1 and Precepts 1 and 2: •History of C
Linux and Bash •Building and running C programs
•Lecture 2: git •Characteristics of C
•Example program: charcount

8
The C Programming Language

Who? Dennis Ritchie


When? ~1972
Where? Bell Labs
Why? Build the Unix OS

Read more history:


9
https://www.bell-labs.com/usr/dmr/www/chist.html
Java vs. C: History
This is what
we’re using

1960 1970 1972 1978 1989 1999 2011 2018


ANSI C89 ISO/ANSI
BCPL B C K&R C ISO C11 ISO C18
ISO C90 C99

Algol

Simula C++ Java

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

Our computing environment A taste of C


•Lecture 1 and Precepts 1 and 2: •History of C
Linux and Bash •Building and running C programs
•Lecture 2: git •Characteristics of C
•Example program: charcount

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)

data java data

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)

data myprog data

16
Agenda

Our computing environment A taste of C


•Lecture 1 and Precepts 1 and 2: •History of C
Linux and Bash •Building and running C programs
•Lecture 2: git •Characteristics of C
•Example program: charcount

17
Java vs. C: Portability
Program Code Type Portable?
MyProg.java Java source code Yes
myprog.c C source code Mostly

MyProg.class Bytecode Yes


myprog Machine lang code No

Conclusion: Java programs are more portable

(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

Conclusion 1: Java is often safer than C


19 Conclusion 2: Java is often slower than C
C is for … car?
Q: Which corresponds to the C programming language?

A.

B.

C.
Java vs. C: Details

Next 7 slides show C language details by way of Java comparisons.

For now, use as a comparative language overview reference to start the


simple "syntax mapping" stage of learning C, so that you're well
prepared to dive into the less rote aspects in the coming weeks.

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");
} }
}

Building $ javac Hello.java $ gcc217 hello.c –o hello

$ java Hello $ ./hello


Running hello, world 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 * ==, !=, <, >, <=, >= ==, !=, <, >, <=, >=

Arithmetic ops * +, -, *, /, %, unary - +, -, *, /, %, unary -

Bitwise 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:
... ...
} }

goto stmt // no equivalent goto someLabel;

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;
} }

Exceptions throw, try-catch-finally /* no equivalent */

/* 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);

* Essentially the same in the two languages


29
Agenda

Our computing environment A taste of C


•Lecture 1 and Precepts 1 and 2: •History of C
Linux and Bash •Building and running C programs
•Lecture 2: git •Characteristics of C
•Example program: charcount

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

$ gcc217 charcount.c –o charcount


$ ./charcount
Line 1
Line 2
^D

What is this?
What is the effect?
What is printed?
35
charcount Building and Running

$ gcc217 charcount.c –o charcount


$ ./charcount
Line 1
Line 2
^D
14
$

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

$ ./charcount > someotherfile


Line 1
Line 2
^D
$ cat someotherfile
14
$

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 …

More character processing,


structured exactly how we'll
want you to design your
Assignment 1 solution!
Frankie Fouganthin

@christianlue

Read the A1 specs soon: you'll be ready to start after Lecture 3!


46

You might also like