Joseph Haugh
University of New Mexico
struct Point {int x; int y;};
struct Point incPoint(struct Point p) {
p.x++;
p.y++;
return p;
}
void main(void) {
struct Point p1 = {12, 3};
struct Point p2 = incPoint(p1);
printf("p1=(%d, %d) p2=(%d, %d)\n",
p1.x, p1.y, p2.x, p2.y);
}
struct Point {int x; int y;};
struct Point incPoint(struct Point p) {
p.x++;
p.y++;
return p;
}
void main(void) {
struct Point p1 = {12, 3};
struct Point p2 = incPoint(p1);
printf("p1=(%d, %d) p2=(%d, %d)\n",
p1.x, p1.y, p2.x, p2.y);
}
struct Point {int x; int y;};
void incrementPoint(struct Point *p) {
(*p).x += 2;
p->y += 2;
}
void main(void) {
struct Point p1 = {7, 7};
incrementPoint(&p1);
printf("p1=(%d, %d)\n", p1.x, p1.y);
}
struct Point {int x; int y;};
void incrementPoint(struct Point *p) {
(*p).x += 2;
p->y += 2;
}
void main(void) {
struct Point p1 = {7, 7};
incrementPoint(&p1);
printf("p1=(%d, %d)\n", p1.x, p1.y);
}
#include
#include
struct Point {double x; double y;};
void foo(struct Point *p) {
double d = sqrt((p->x)*(p->x)
+ (p->y)*(p->y));
p->x /= d;
p->y /= d;
}
void main(void) {
struct Point p1 = {3, 4};
foo(&p1);
printf("p1=(%5.2f, %5.2f)\n", p1.x, p1.y);
}
#include
#include
struct Point {double x; double y;};
void foo(struct Point *p) {
double d = sqrt((p->x)*(p->x)
+ (p->y)*(p->y));
p->x /= d;
p->y /= d;
}
void main(void) {
struct Point p1 = {3, 4};
foo(&p1);
printf("p1=(%5.2f, %5.2f)\n", p1.x, p1.y);
}
void main(void) {
char data[] = "Warcraft";
data[7] = '+';
char *linePt = &data[4];
*linePt = '*';
printf("[%s], [%s]\n", data, linePt);
}
void main(void) {
char data[] = "Warcraft";
data[7] = '+';
char *linePt = &data[4];
*linePt = '*';
printf("[%s], [%s]\n", data, linePt);
}
Functions defined in stdio.h include:
Constants defined in stdio.h include:
void filecopy(FILE* in, FILE* out) {
int c;
while ((c = getc(in)) != EOF) {
putc(c, out);
}}
int main(int argc, char** argv) {
FILE* fp; int i;
if(argc == 1) filecopy(stdin, stdout);
else {
for(i = 1; i < argc; ++i) {
fp = fopen(argv[i], "r");
if(fp == NULL) return 1;
else {
filecopy(fp, stdout);
fclose(fp);
}}}
return 0;
}
Include the library’s .h file in your source code
Compile/Link: gcc -llibrary options
Your source code will compile with references to functions and variables declared extern.
After your source code compiles, the linker needs to attach to the executable code for each library function you referenced.
On Unix-like systems, the rule for naming libraries is libx.a, where x is some string.
Link library libx.a with the gcc option: -lx.
Example:
#include <time.h>
Not all libraries require this.
#include
#include
void main(void) {
time_t clock = time(NULL);
long sec = (long)clock;
printf("Seconds since Unix Epoch: %ld\n",sec);
printf("Current time: %s\n", ctime(&clock));
}
Seconds since Unix Epoch: 1711651683
Current time: Thu Mar 28 12:48:03 2024
On moons.cs.unm.edu, time_t is a long.
On moons, gcc -ltime is not needed.
The example below shows just a few of the constants defined
#include
#include /* no linker lib option needed. */
void main(void) {
printf("%d\n", INT_MIN); /* -2147483648 */
printf("%d\n", INT_MAX); /* 2147483647 */
printf("%d\n", CHAR_MIN); /* -128 */
printf("%d\n", CHAR_MAX); /* 127 */
printf("%d\n", UCHAR_MAX); /* 255 */
}
gcc -l library NOT needed
int rand(void)
void srand(unsigned long seed)
Generally, it is not very useful to get a pseudo-random number between 0 and RAND_MAX.
This utility function returns a uniformly distributed pseudorandom number between 0 and n-1.
int randomInt(int n) {
int r = rand(); /* r = [0,RAND_MAX] */
/* x = [0, 1) */
double x = (double)r / ((double)RAND_MAX + 1.0);
/* Without +1.0, there is a 1 in RAND_MAX */
/* chance of returning n. */
/* return: [0, n-1] */
return (int)(x*n);
}
void main(void) {
int i; int bins[7];
long seed = (long)time(NULL);
printf("seed = %ld\n", seed);
srand(seed);
for (i = 0; i < 7; i++) {
bins[i] = 0;
}
for (i = 0; i < 10000; i++) {
int r = randomInt(6);
bins[r]++;
}
for (i = 0; i < 7; i++) {
printf("bins[%d] = %d\n", i, bins[i]);
}
}
Use of this seed on moons will exactly reproduce these results.
seed = 1332289063
bins[0] = 1638
bins[1] = 1669
bins[2] = 1604
bins[3] = 1645
bins[4] = 1690
bins[5] = 1754
bins[6] = 0
void main(void) {
int i;
int bins[12];
srand((long)time(NULL));
for (i=0; i<7; i++) {
bins[i] = 0;
}
for (i=0; i<70000; i++) {
int r = randomInt(6) + randomInt(6);
bins[r]++;
}
for (i=0; i<12; i++) {
printf("bins[%d] = %d\n", i, bins[i]);
}
}
bins[0] = 2016
bins[1] = 3826
bins[2] = 5879
bins[3] = 7904
bins[4] = 9558
bins[5] = 11733
bins[6] = 9684
bins[7] = 7749
bins[8] = 5779
bins[9] = 3951
bins[10] = 1921
bins[11] = 0
char *strcpy(char dest, const char src)
\0
character is copied.char *strncpy(char dest, const char src, size_t n)
\0
.int strlen(const char* str)
#include
int strlen(const char str[]) {
int i = 0;
while (str[i]) i++;
return i;
}
void main(void) {
char word[] = "Hello";
printf("%d\n", strlen(word));
}
Including math.h will tell the compiler that the math functions like sqrt(x) exist.
The math library file name is: libm.a
gcc -lm foo.c
#include
#include
void main(void) {
double x1 = 3.1;
double x2 = 3.6;
printf("%f\n", pow(x2 - x1, 2.0)); /* 0.250000 */
printf("%f\n", pow(x1 - x2, 2.0)); /* 0.250000 */
printf("%f\n", pow(x2 - x1, 2.5)); /* 0.176777 */
printf("%f\n", pow(x1 - x2, 2.5)); /* -nan ???? */
printf("%f\n", pow(x2 - x1, 2.0) * sqrt(x2 - x1));
/* 0.176777 */
}
$x^{2.5}=x^{2}x^{0.5}=x^{2}\sqrt{x}$
$\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}$
Could us pow, but both slower AND less accurate.
#include
double dist(double x1, double y1,
double x2, double y2) {
return sqrt(pow(x1-x2, 2.0) + pow(y1-y2, 2.0));
}
Better Approach:
double dx = x1 - x2;
double dy = y1 - y2;
return sqrt(dx*dx + dy*dy);
Often only need relative distance:
double dx = x1 - x2;
double dy = y1 - y2;
return dx*dx + dy*dy;
Distance is a very important concept in computer science: