Professor Hong
#include "filename.h"
- includes file in directory of where the source program was found; else follows an implementation defined rule.#include
- follows an implementation-defined rule to find the file#define name replacement text
- can also be used to substitute macros #if !defined(HDR)
#define HDR
/* contents of hdr.h go here */
#endif
#include <stdio.h>
int main()
{
int x=1, y=2, z[10];
int *ip; // ip is a pointer to an int
ip = &x; // ip now points to x
printf("ip=%x\n", ip); // prints address of x
y = *ip; // y is now 1
printf("y=%d\n", y);
*ip = 0; // x is now 0
printf("x=%d\n", x);
ip = &z[0]; // ip now points to z[0]
printf("ip=%x\n", ip);
return 0;
}
#include <stdio.h>
int main()
{
char *ptr = NULL; // Null pointer
printf("%c", *ptr);
return 0;
}
#include <stdio.h>
void printArray(int arr[])
{
for (int i=0; i<10; i++)
printf("%d ", arr[i]);
printf("\n");
}
void printArrayAddress(int arr[])
{
for (int i=0; i<10; i++)
printf("%x ", &arr[i]);
printf("\n");
}
int main()
{
int intArr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printArray(intArr);
printArrayAddress(intArr);
int *intPtr = intArr; // defaults to &intArr[0]
printf("intPtr=%x\n", intPtr);
printf("*intPtr=%d\n", *intPtr);
int *intPtr2 = &intArr[2]; // address of intArr[2]
printf("intPtr2=%x\n", intPtr2);
printf("*intPtr2=%d\n", *intPtr2);
printf("\n");
*intPtr2 = *intPtr2 + 10; // adds 10 to arr[2]
printf("intPtr2=%x\n", intPtr2);
printf("*intPtr=%d\n", *intPtr2);
int y;
y = *intPtr2 + 1; // gets arr[2] and adds 1
printf("y=%d\n", y);
*intPtr2 += 10; // adds 10 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
++*intPtr2; // adds 1 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
(*intPtr2)++; // adds 1 to arr[2]
y = *intPtr2;
printf("y=%d\n", y);
*intPtr2++; // increments pointer to arr[3]! That's it!
y = *intPtr2;
printf("y=%d\n", y);
return 0;
}
#include <stdio.h>
// K&R Pg. 95
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int x = 1;
int y = 2;
printf("before: x=%d, y=%d\n", x, y);
swap(x, y);
printf("after: x=%d, y=%d\n", x, y);
}
#include <stdio.h>
// K&R Pg. 96
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
int main()
{
int x = 1;
int y = 2;
printf("before: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("after: x=%d, y=%d\n", x, y);
}
#include <stdio.h>
#include <ctype.h>
// K&R Pg. 97
int getch();
void ungetch(int);
#define BUFSIZE 100
char buf[BUFSIZE]; // buffer for ungetch
int bufp = 0; // next free position in buf
int getch(void) // get a (possibly pushed back) charater
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) // push character back on input
{
if (bufp >= BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++] = c;
}
// getint: get next integer from input into *pn
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch())) // skip white space
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-')
{
ungetch(c); // it's not a number
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}
int main()
{
int c, pn;
c = getint(&pn);
printf("c=%d pn=%d", c, pn);
return 0;
}
#include <stdio.h>
// K&R Pg. 99
// strlen:return length of string s
int strlen(char *s)
{
int n;
for (n=0; *s != '\0'; s++)
n++;
return n;
}
int main()
{
char str[] = "ABCDEFG";
printf("strlen=%d\n", strlen(str));
return 0;
}
#include <stdio.h>
// K&R Pg. 103
// strlen:return length of string s
int strlen(char *s)
{
char *p = s;
while (*p != '\0')
p++;
return p - s;
}
int main()
{
char str[] = "ABCDEFG";
printf("strlen=%d\n", strlen(str));
return 0;
}
#include <stdio.h>
// K&R Pg. 101
#define ALLOCSIZE 10000 // size of available space
static char allocbuf[ALLOCSIZE]; // storage for alloc
static char *allocp = allocbuf; // next free position
char *alloc(int n) // return pointer to n characters
{
if (allocbuf + ALLOCSIZE - allocp >= n) // it fits
{
allocp += n;
return allocp - n; // old p
}
else // not enough room
return 0;
}
void afree(char *p) // free storage pointed to by p
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
int main()
{
char *spacePtr;
int n = 10;
spacePtr = alloc(n);
printf("spacePtr=%x, allocp=%x\n", spacePtr, allocp);
char *spacePtr2;
spacePtr2 = alloc(n);
printf("spacePtr2=%x, allocp=%x\n", spacePtr2, allocp);
afree(spacePtr2);
printf("spacePtr2=%x, allocp=%x\n", spacePtr2, allocp);
afree(spacePtr);
printf("spacePtr=%x, allocp=%x\n", spacePtr, allocp);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char amessage[] = "now is the time"; // an array
char *pmessage = "now is the time"; // a pointer
printf("amessage=%s\n",amessage);
printf("pmessage=%x\n",pmessage);
printf("pmessage=");
for(; *pmessage!='\0'; ++pmessage)
printf("%c",*pmessage);
}
#include <stdio.h>
// K&R pg. 105
// strcpy: copy t to s; array subscript version
void strcpy(char *s, char *t)
{
int i;
i = 0;
while ((s[i] = t[i]) != '\0')
i++;
}
int main()
{
char str[10] = "My string";
char newStr[10];
strcpy(newStr, str);
printf("str=%s\n", str);
printf("newStr=%s\n", newStr);
return 0;
}
#include <stdio.h
// K&R pg. 105
// strcpy: copy t to s; poitner version 1
void strcpy(char *s, char *t)
{
while ((*s = *t) != '\0') {
s++;
t++;
}
}
int main()
{
char str[10] = "My string";
char newStr[10];
strcpy(newStr, str);
printf("str=%s\n", str);
printf("newStr=%s\n", newStr);
return 0;
}
#include <stdio.h>
// K&R pg. 106
// strcmp: return <0 if s<t, 0 if s==t, >0 if s>t
int strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i] - t[i];
}
int main()
{
char s[] = "ABC";
char t[] = "ABC";
printf("%d", strcmp(s,t));
return 0;
}
#include <stdio.h>
// K&R pg. 106
// strcmp: return <0 if s<t, 0 if s==t, >0 if s>t
int strcmp(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
int main()
{
char s[] = "ABC";
char t[] = "ABC";
printf("%d", strcmp(s,t));
return 0;
}
#include <stdio.h>
// K&R pg. 88
// swap:interchange v[i] and v[j]
void swap(int v[], int i, int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
// K&R pg. 87
// qsort:sort v[left]...v[right] into inreasing order
void qsort(int v[], int left, int right)
{
for (int i=0; i<3; i++)
printf("%d ", v[i]);
printf("; left=%d, right=%d\n",left,right);
int i, last;
if (left >= right) // do nothing if array contains fewer than two elements
return;
swap(v, left, (left + right)/2); // move partition elem to v[0]
last = left;
for (i = left+1; i <= right; i++) // partition
if (v[i] < v[left])
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
int main()
{
int v[] = {3, 1, 2};
int left = 0;
int right = 2;
printf("ORIGINAL: ");
for (int i=0; i<3; i++)
printf("%d ", v[i]);
printf("\n");
qsort(v, left, right);
printf("SORTED: ");
for (int i=0; i<3; i++)
printf("%d ", v[i]);
return 0;
}
#include<stdio.h>
#include <string.h>
// K&R Pg. 108-109
#define MAXLINES 5000
#define ALLOCSIZE 10000 // size of available space
char *lineptr[MAXLINES]; // pointers to text lines
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
static char allocbuf[ALLOCSIZE]; // storage for alloc
static char *allocp = allocbuf; // next free position
void qsort(char *lineptr[], int left, int right);
// sort input lines
int main()
{
int nlines; // number of input lines read
if ((nlines = readlines(lineptr, MAXLINES)) >= 0)
{
printf("INPUT:\n");
writelines(lineptr, nlines);
qsort(lineptr, 0, nlines-1);
printf("\nOUTPUT:\n");
writelines(lineptr, nlines);
return 0;
}
else
{
printf("error:input too big to sort\n");
return 1;
}
}
#define MAXLEN 1000 // max length of any input line
int mygetline(char *, int);
char *alloc(int);
// readlinse: read input lines
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;
while ((len = mygetline(line, MAXLEN)) > 0)
{
if (nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else
{
line[len-1] = '\0'; // delete newlines
strcpy(p, line);
lineptr[nlines++] = p;
}
}
return nlines;
}
// writelines: write output lines
void writelines(char *lineptr[], int nlines)
{
int i;
for (i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
}
char *alloc(int n) // return pointer to n characters
{
if (allocbuf + ALLOCSIZE - allocp >= n) // it fits
{
allocp += n;
return allocp - n; // old p
}
else // not enough room
return 0;
}
void afree(char *p) // free storage pointed to by p
{
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
// mygetline: get line into s, return length
int mygetline(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
// qsort:sort v[left]...v[right] into increasing order
void qsort(char *v[], int left, int right)
{
int i, last;
void swap(char *v[], int i, int j);
// do nothing if array contains fewer than two elements
if (left >= right)
return;
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
if (strcmp(v[i], v[left]) <0)
swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
// swap:interchange v[i] and v[j]
void swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
#include <stdio.h>
// K&R pg. 111
static char daytab[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
// day of year: set day of year from month & day
int day_of_year(int year, int month, int day)
{
int i, leap;
leap = year%4 == year%100 != 0 || year%400 == 0;
for (i = 1; i < month; i++)
day += daytab[leap][i];
return day;
}
// month_day: set month, day from day of year
void month_day(int year, int yearday, int *pmonth, int *pday)
{
int i, leap;
leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
for (i = 1; yearday > daytab[leap][i]; i++)
yearday -= daytab[leap][i];
*pmonth = i;
*pday = yearday;
}
int main()
{
int day;
day = day_of_year(2018, 2, 15);
printf("day=%d\n", day);
int pmonth, pday;
month_day(2018, 46, &pmonth, &pday);
printf("pmonth=%d, pday=%d", pmonth, pday);
return 0;
}
#include <stdio.h>
// K&R pg. 113
// month_name: return name of n-th month
char *month_name(int n)
{
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
int main()
{
char *name = month_name(2);
printf("name=%s", name);
return 0;
}
HW4-5