CS 330 Lecture 12 – C
Agenda
- what ?s
- array/pointer duality
- scope in C vs. Java
- array allocation in C vs. Java
- call by value in C vs. Java
- new types
- enums in C vs. Java
Think About This
- Is Java an interpreted language or a compiled one?
- Eclipse will compile classes that have compilation errors. When you try to run a bad method, however, it fails. How does Eclipse do this?
TODO
- Read Simon Tatham’s Descent into C: http://www.chiark.greenend.org.uk/~sgtatham/cdescent.
- Why does C not have a
boolean
type? - Why does Java not have
unsigned
types? - 1/4 sheet.
Code
atoi.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
const char s[10000] = "534714";
int s_as_int = 0;
// Solved with array-thinking...
for (int i = 0; i < strlen(s); ++i) {
s_as_int *= 10;
s_as_int += s[i] - '0';
}
// Solved with pointer-thinking...
s_as_int = 0;
for (char *c = s; *c != '\0'; ++c) {
s_as_int *= 10;
s_as_int += *c - '0';
}
printf("s_as_int: %d\n", s_as_int);
return 0;
}
points.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
double vertices[] = {
0.2560344, 0.7008195, 0.309516,
0.9977562, 0.1579919, 0.811734,
0.9093718, 0.1702925, 0.754314,
0.5427342, 0.8875533, 0.595991,
0.8922929, 0.8357852, 0.653600,
0.8543975, 0.1454883, 0.976032,
0.3781706, 0.0057611, 0.754993,
0.4160220, 0.3118013, 0.652116,
0.5450121, 0.8648328, 0.309731,
0.1296349, 0.6227086, 0.794076,
0.7750567, 0.8990120, 0.870277,
0.6404002, 0.7845455, 0.874627,
0.8406608, 0.0757393, 0.408112,
0.4229174, 0.4599141, 0.680888,
0.6372424, 0.3462617, 0.318470,
0.9495815, 0.6654377, 0.563382,
0.1967725, 0.7708067, 0.136886,
0.8724695, 0.9815157, 0.616252,
0.0389801, 0.4078829, 0.542178,
0.7192636, 0.8921647, 0.372246,
};
/* printf("Vertex %d - %f %f %f", 0, vertices[0], vertices[1], vertices[2]); */
/* printf("Vertex %d - %f %f %f", 1, vertices[3], vertices[4], vertices[5]); */
/* printf("%d", i); */
for (int i = 0; i < 20; ++i) {
printf("Vertex %d - %f %f %f\n", i, vertices[3 * i], vertices[3 * i + 1], vertices[3 * i + 2]);
}
double *vertex = vertices;
for (int i = 0; i < 20; ++i) {
printf("Vertex %d - %f %f %f\n", i, vertex[0], vertex[1], vertex[2]);
vertex += 3;
}
int j = 17;
{
int j = 32;
printf("j: %d\n", j);
}
return 0;
}
Scope.java
package lecture08;
public class Scope {
public static void main(String[] args) {
int[] args;
int j = 17;
{
int k = 32;
}
System.out.println(k);
}
}
Haiku
Counter examples
The old taught incrementing
To one up the old
The old taught incrementing
To one up the old