first commit!
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
*
|
||||
!*.c
|
||||
!*.h
|
21
arrays.c
Normal file
21
arrays.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int ch, n_whitesp, n_other;
|
||||
int n_digit[10] = {0}; /* initialize array with zeroes */
|
||||
|
||||
n_whitesp = n_other = 0;
|
||||
|
||||
while ((ch = getchar()) != EOF)
|
||||
if (ch >= '0' && ch <= '9')
|
||||
++n_digit[ch - '0'];
|
||||
else if (ch == ' ' || ch == '\n' || ch == '\t')
|
||||
++n_whitesp;
|
||||
else
|
||||
++n_other;
|
||||
|
||||
printf("digits = ");
|
||||
for (int i = 0; i < 10; ++i)
|
||||
printf(" %d", n_digit[i]);
|
||||
printf(", white space = %d, other = %d\n", n_whitesp, n_other);
|
||||
}
|
48
atof.c
Normal file
48
atof.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAXLINE 100
|
||||
|
||||
int getlime(char s[], int lim)
|
||||
{
|
||||
int ch, i;
|
||||
for (i = 0; i < lim - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i)
|
||||
s[i] = ch;
|
||||
if (ch == '\n') {
|
||||
s[i] = ch;
|
||||
++i;
|
||||
}
|
||||
s[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
double atof(char s[]) {
|
||||
double val, power;
|
||||
int ind, sign;
|
||||
|
||||
for (ind = 0; isspace(s[ind]); ++ind) /* skip whitespace */
|
||||
;
|
||||
sign = (s[ind] == '-') ? -1 : 1;
|
||||
if (s[ind] == '+' || s[ind] == '-')
|
||||
++ind;
|
||||
|
||||
for (val = 0.0; isdigit(s[ind]); ++ind)
|
||||
val = 10 * val + (s[ind] - '0');
|
||||
if(s[ind] == '.')
|
||||
++ind;
|
||||
for (power = 1.0; isdigit(s[ind]); ++ind) {
|
||||
val = 10.0 * val + (s[ind] - '0');
|
||||
power *= 10;
|
||||
}
|
||||
return sign * val / power;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double sum = 0;
|
||||
char line[MAXLINE];
|
||||
|
||||
while (getlime(line, MAXLINE) > 0)
|
||||
printf("\t%g\n", sum += atof(line));
|
||||
|
||||
return 0;
|
||||
}
|
27
atoi_v2.c
Normal file
27
atoi_v2.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int atoi(char string[]) {
|
||||
int i, sign_ext, res;
|
||||
|
||||
for (i = 0; isspace(string[i]); ++i)
|
||||
;
|
||||
|
||||
sign_ext = string[i] == '-' ? -1 : 1;
|
||||
if (string[i] == '-')
|
||||
++i;
|
||||
|
||||
for (res = 0; isdigit(string[i]); ++i) {
|
||||
res = res * 10 + (string[i] - '0');
|
||||
}
|
||||
|
||||
return sign_ext * res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%d\n", atoi("123"));
|
||||
printf("%d\n", atoi("-123"));
|
||||
printf("%d\n", atoi("+123"));
|
||||
printf("%d\n", atoi("0123"));
|
||||
printf("%d\n", atoi("0123.1"));
|
||||
}
|
13
fahrcelcdefine.c
Normal file
13
fahrcelcdefine.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define LOWER 0
|
||||
#define UPPER 300
|
||||
#define STEP 20
|
||||
|
||||
int main() {
|
||||
int fahr;
|
||||
|
||||
for (fahr = LOWER; fahr <= UPPER; fahr += STEP) {
|
||||
printf("%3d %6.1f\n", fahr, (5.0/9) * (fahr - 32));
|
||||
}
|
||||
}
|
9
fahrcelcforloop.c
Normal file
9
fahrcelcforloop.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int fahr;
|
||||
|
||||
for (fahr = 0; fahr <= 300; fahr = fahr + 20) {
|
||||
printf("%3d %6.1f\n", fahr, (5.0/9) * (fahr - 32));
|
||||
}
|
||||
}
|
18
fahrenheittable.c
Normal file
18
fahrenheittable.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
double fahr, celcius;
|
||||
double lower, upper, step;
|
||||
|
||||
lower = 0;
|
||||
upper = 300;
|
||||
step = 20;
|
||||
|
||||
fahr = lower;
|
||||
printf("%6c %8c\n", 'F', 'C');
|
||||
while (fahr <= upper) {
|
||||
celcius = 5.0/9 * (fahr - 32);
|
||||
printf("%6.2f %8.2f\n", fahr, celcius);
|
||||
fahr = fahr + step;
|
||||
}
|
||||
}
|
17
filecopy.c
Normal file
17
filecopy.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int character;
|
||||
|
||||
/*
|
||||
character = getchar();
|
||||
while (character != EOF) {
|
||||
putchar(character);
|
||||
character = getchar();
|
||||
}
|
||||
* which can be rewritten as...
|
||||
*/
|
||||
|
||||
while ((character = getchar()) != EOF)
|
||||
putchar(character);
|
||||
}
|
7
helloworld.c
Normal file
7
helloworld.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("hello, ");
|
||||
printf("world");
|
||||
printf("\n");
|
||||
}
|
10
line_count.c
Normal file
10
line_count.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int chars, lines, character;
|
||||
|
||||
for (chars = 0, lines = 0; (character = getchar()) != EOF; ++chars)
|
||||
if (character == '\n')
|
||||
++lines;
|
||||
printf("\n%d lines were inputted\n", lines);
|
||||
}
|
38
longest_line.c
Normal file
38
longest_line.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#define MAXLINE 1000
|
||||
|
||||
int getlime(char line[], int maxline); /* tasty lime! */
|
||||
void copy(char to[], char from[]);
|
||||
|
||||
int main() {
|
||||
int current_len, max_len = 0;
|
||||
char current_line[MAXLINE];
|
||||
char longest[MAXLINE];
|
||||
|
||||
while ((current_len = getlime(current_line, MAXLINE)) > 0)
|
||||
if(current_len > max_len) {
|
||||
max_len = current_len;
|
||||
copy(longest, current_line);
|
||||
}
|
||||
if (max_len > 0) /* there was some line */
|
||||
printf("%s", longest);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getlime(char s[], int lim) {
|
||||
int ch, i;
|
||||
|
||||
for (i = 0; i < lim - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i)
|
||||
s[i] = ch;
|
||||
if (ch == '\n') {
|
||||
s[i] = ch;
|
||||
++i;
|
||||
}
|
||||
s[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
void copy(char to[], char from[]) {
|
||||
for (int i = 0; (to[i] = from[i]) != '\0'; ++i)
|
||||
;
|
||||
}
|
47
longest_line_extern.c
Normal file
47
longest_line_extern.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#define MAXLINE 1000
|
||||
|
||||
int max_len;
|
||||
char current_line[MAXLINE];
|
||||
char longest[MAXLINE];
|
||||
|
||||
int getlime();
|
||||
void copy();
|
||||
|
||||
int main() {
|
||||
int current_len;
|
||||
extern int max_len;
|
||||
extern char longest[];
|
||||
|
||||
max_len = 0;
|
||||
while ((current_len = getlime()) > 0)
|
||||
if (current_len > max_len) {
|
||||
max_len = current_len;
|
||||
copy();
|
||||
}
|
||||
if (max_len > 0) /* there was some line */
|
||||
printf("%s", longest);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getlime() {
|
||||
int ch, i;
|
||||
extern char current_line[];
|
||||
|
||||
for (i = 0; i < MAXLINE - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i)
|
||||
current_line[i] = ch;
|
||||
if (ch == '\n') {
|
||||
current_line[i] = ch;
|
||||
++i;
|
||||
}
|
||||
current_line[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
void copy() {
|
||||
extern char current_line[], longest[];
|
||||
|
||||
for (int i = 0; (longest[i] = current_line[i]) != '\0'; ++i)
|
||||
;
|
||||
}
|
15
power.c
Normal file
15
power.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int power(int m, int n);
|
||||
|
||||
int main() {
|
||||
for (int i = 0; i < 10; ++i)
|
||||
printf("%d %d %d\n", i, power(2, i), power(-3, i));
|
||||
}
|
||||
|
||||
int power(int base, int n) {
|
||||
int p = 1; /* 0th power of any number */
|
||||
for (int i = 1; i <= n; ++i)
|
||||
p *= base;
|
||||
return p;
|
||||
}
|
22
register.c
Normal file
22
register.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main() {
|
||||
struct timespec t0, t1;
|
||||
long int i = 0;
|
||||
register long int x = 0;
|
||||
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
|
||||
for (; i < 1000; ++i)
|
||||
printf("%ld ", i + 1);
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
|
||||
printf("\n%ldns took usual var", (t1.tv_sec - t0.tv_sec) * (long)1e9 + (t1.tv_nsec - t0.tv_nsec));
|
||||
printf("\n\n");
|
||||
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
|
||||
for (; x < 1000; ++x)
|
||||
printf("%ld ", x + 1);
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
|
||||
|
||||
printf("\n%ldns took register var", (t1.tv_sec - t0.tv_sec) * (long)1e9 + (t1.tv_nsec - t0.tv_nsec));
|
||||
}
|
3
segfault.c
Normal file
3
segfault.c
Normal file
@ -0,0 +1,3 @@
|
||||
int main() {
|
||||
*(int*)0 = 0;
|
||||
}
|
38
shellsort.c
Normal file
38
shellsort.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NUMBERS 1000000
|
||||
|
||||
void print_arr(int[]);
|
||||
void shellsort(int[], int);
|
||||
|
||||
void shellsort(int arr[], int n) {
|
||||
int temp;
|
||||
|
||||
for (int gap = n / 2; gap > 0; gap /= 2)
|
||||
for (int i = gap; i < n; ++i)
|
||||
for (int j = i - gap; j >= 0 && arr[j] > arr[j + gap]; j -= gap) {
|
||||
temp = arr[j];
|
||||
arr[j] = arr[j + gap];
|
||||
arr[j + gap] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int ind, numbers[NUMBERS];
|
||||
for (ind = NUMBERS; ind > 0; --ind) {
|
||||
numbers[ind] = rand();
|
||||
}
|
||||
|
||||
shellsort(numbers, NUMBERS);
|
||||
// print_arr(numbers);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_arr(int numbers[]) {
|
||||
for (int ind = 0; ind < NUMBERS; ++ind) {
|
||||
if (ind >= 10 && ind % 10 == 0)
|
||||
printf("\n");
|
||||
printf("%d ", numbers[ind]);
|
||||
}
|
||||
}
|
170
sqlite_ex.c
Normal file
170
sqlite_ex.c
Normal file
@ -0,0 +1,170 @@
|
||||
#include "sqlite_ex.h"
|
||||
|
||||
static char date_string[STR_SIZE];
|
||||
static meas *measurements;
|
||||
static int meas_count;
|
||||
static Datetime twnth_day;
|
||||
static Cost costs;
|
||||
static char *zErrMsg = NULL;
|
||||
static sqlite3 *db;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int respCode;
|
||||
if ((respCode = sqlite3_open("communal.db", &db))) {
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (argc == 2 && !strcmp(argv[1], "-t")) {
|
||||
sqlite_list_tables(db);
|
||||
} else {
|
||||
sqlite_exec_query(SELECT_COUNT_MEAS, count_measurements);
|
||||
measurements = malloc(sizeof(meas) * meas_count);
|
||||
|
||||
sqlite_exec_query(SELECT_MEASUREMENTS, parse_measurements);
|
||||
measurements -= meas_count; // bring it back to the start of array
|
||||
|
||||
sqlite_exec_query(SELECT_TWNTH_DAY, select_twnth_day);
|
||||
|
||||
sqlite3_stmt *st;
|
||||
respCode = sqlite3_prepare_v2(db, SELECT_CURRENT_COSTS, -1, &st, NULL);
|
||||
if (respCode != SQLITE_OK) {
|
||||
fprintf(stderr, "Prepared statement error: %d\n", respCode);
|
||||
}
|
||||
sqlite3_bind_int(st, 1, measurements[meas_count - 1].cost_id);
|
||||
respCode = sqlite3_step(st);
|
||||
if (respCode == SQLITE_ROW) {
|
||||
int n_cols = sqlite3_column_count(st);
|
||||
for (int col_ind = 0; col_ind < n_cols; ++col_ind) {
|
||||
const char *col_name = sqlite3_column_name(st, col_ind);
|
||||
if (!strcmp(col_name, "gas_cost")) {
|
||||
costs.gas_c = sqlite3_column_double(st, col_ind);
|
||||
} else if (!strcmp(col_name, "water_cost")) {
|
||||
costs.water_c = sqlite3_column_double(st, col_ind);
|
||||
} else if (!strcmp(col_name, "electricity_cost")) {
|
||||
costs.electricity_c = sqlite3_column_double(st, col_ind);
|
||||
} else if (!strcmp(col_name, "other_cost")) {
|
||||
costs.housing_c = sqlite3_column_double(st, col_ind);
|
||||
}
|
||||
}
|
||||
}
|
||||
respCode = sqlite3_finalize(st);
|
||||
if (respCode != SQLITE_OK) {
|
||||
fprintf(stderr, "Prepared statement finalization error: %d\n", respCode);
|
||||
sqlite3_free(zErrMsg);
|
||||
}
|
||||
|
||||
print_measurements();
|
||||
|
||||
for (int i = meas_count - 1; i >= 0; --i) {
|
||||
Datetime *dt = measurements[i].datetime;
|
||||
if (dates_same(dt, &twnth_day)) {
|
||||
if (i == meas_count - 1) {
|
||||
printf("No measurements were made since last time!\n");
|
||||
goto free;
|
||||
}
|
||||
double total = communal_difference(&measurements[meas_count - 1],
|
||||
&measurements[i]);
|
||||
strftime(date_string, STR_SIZE, "%x",
|
||||
measurements[meas_count - 1].datetime);
|
||||
printf("date of last measurement: %s\n", date_string);
|
||||
printf("total: %f\n", total);
|
||||
}
|
||||
}
|
||||
|
||||
free:
|
||||
free_measurements();
|
||||
}
|
||||
|
||||
sqlite3_close(db);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sqlite_exec_query(char *query, void *callback) {
|
||||
int respCode = sqlite3_exec(db, query, callback, 0, &zErrMsg);
|
||||
if (respCode != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
||||
sqlite3_free(zErrMsg);
|
||||
}
|
||||
}
|
||||
|
||||
int count_measurements(void *_, int n_cols, char **row_vals, char **cols) {
|
||||
meas_count = atoi(row_vals[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_measurements(void *_, int n_cols, char **row_vals, char **cols) {
|
||||
meas measurement;
|
||||
|
||||
for (int col = 0; col < n_cols; ++col) {
|
||||
if (!strcmp(cols[col], "gas")) {
|
||||
measurement.gas = atof(row_vals[col]);
|
||||
} else if (!strcmp(cols[col], "water")) {
|
||||
measurement.water = atof(row_vals[col]);
|
||||
} else if (!strcmp(cols[col], "electricity")) {
|
||||
measurement.electricity = atof(row_vals[col]);
|
||||
} else if (!strcmp(cols[col], "date")) {
|
||||
Datetime *dt = malloc(sizeof(Datetime));
|
||||
strptime(row_vals[col], "%F", dt);
|
||||
measurement.datetime = dt;
|
||||
} else if (!strcmp(cols[col], "costs")) {
|
||||
measurement.cost_id = atoi(row_vals[col]);
|
||||
}
|
||||
}
|
||||
*measurements++ = measurement;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sqlite_list_tables(sqlite3 *db) {
|
||||
char *errMsg = NULL;
|
||||
int respCode = sqlite3_exec(db, SELECT_ALL_TABLES, print_tables, 0, &errMsg);
|
||||
if (respCode != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", errMsg);
|
||||
sqlite3_free(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
int print_tables(void *_, int rows_n, char **rows, char **cols) {
|
||||
for (int i = 0; i < rows_n; ++i) {
|
||||
printf("%s = %s\n", cols[i], rows[i] ? rows[i] : "NULL");
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int select_twnth_day(void *_, int n_cols, char **row_vals, char **cols) {
|
||||
strptime(row_vals[0], "%F", &twnth_day);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dates_same(Datetime *dt1, Datetime *dt2) {
|
||||
return dt1->tm_year == dt2->tm_year && dt1->tm_mon == dt2->tm_mon &&
|
||||
dt1->tm_mday == dt2->tm_mday;
|
||||
}
|
||||
|
||||
double communal_difference(meas *m_now, meas *m_twnth) {
|
||||
double gas_diff = m_now->gas - m_twnth->gas;
|
||||
double water_diff = m_now->water - m_twnth->water;
|
||||
double electricity_diff = m_now->electricity - m_twnth->electricity;
|
||||
|
||||
return gas_diff * costs.gas_c + water_diff * costs.water_c +
|
||||
electricity_diff * costs.electricity_c + costs.housing_c;
|
||||
}
|
||||
|
||||
void print_measurements() {
|
||||
for (int i = 0; i < meas_count; ++i) {
|
||||
meas m = measurements[i];
|
||||
strftime(date_string, STR_SIZE, "%x", m.datetime);
|
||||
printf("%-15s%11s\n%-15s%11f\n%-15s%11f\n%-15s%11f\n\n",
|
||||
"date:", date_string, "gas:", m.gas, "water:", m.water,
|
||||
"electricity:", m.electricity);
|
||||
}
|
||||
}
|
||||
|
||||
void free_measurements() {
|
||||
for (int i = 0; i < meas_count; ++i)
|
||||
free(measurements[i].datetime);
|
||||
free(measurements);
|
||||
}
|
45
sqlite_ex.h
Normal file
45
sqlite_ex.h
Normal file
@ -0,0 +1,45 @@
|
||||
#define _GNU_SOURCE 1
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define SELECT_MEASUREMENTS "select * from measurements order by date;"
|
||||
#define SELECT_COUNT_MEAS "select count(*) from measurements;"
|
||||
#define SELECT_ALL_TABLES "select * from sqlite_schema where type = 'table';"
|
||||
#define SELECT_TWNTH_DAY "select * from twnth_day;"
|
||||
#define SELECT_CURRENT_COSTS "select * from costs where id = ?;"
|
||||
#define STR_SIZE 11
|
||||
|
||||
typedef struct tm Datetime;
|
||||
|
||||
typedef struct {
|
||||
Datetime *datetime;
|
||||
/* NOTE: probably should change to union type?
|
||||
* (to hold both double and int representation, to count the integer cost)
|
||||
*/
|
||||
double gas;
|
||||
double water;
|
||||
double electricity;
|
||||
int cost_id;
|
||||
} meas;
|
||||
|
||||
typedef struct {
|
||||
double gas_c;
|
||||
double water_c;
|
||||
double electricity_c;
|
||||
double housing_c;
|
||||
} Cost;
|
||||
|
||||
int print_tables(void *, int, char **, char **);
|
||||
void print_measurements();
|
||||
void free_measurements();
|
||||
int dates_same(Datetime *, Datetime *);
|
||||
double communal_difference(meas *, meas *);
|
||||
int parse_measurements(void *, int, char **, char **);
|
||||
int count_measurements(void *, int, char **, char **);
|
||||
int select_twnth_day(void *, int, char **, char **);
|
||||
void sqlite_list_tables(sqlite3 *db);
|
||||
void sqlite_exec_query(char *query, void *callback);
|
55
struct_beginning.c
Normal file
55
struct_beginning.c
Normal file
@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
struct point {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct {
|
||||
int x;
|
||||
int y;
|
||||
} point2;
|
||||
|
||||
struct point3 pt;
|
||||
|
||||
struct point3 {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct rect {
|
||||
struct point pt1;
|
||||
struct point pt2;
|
||||
};
|
||||
|
||||
struct point makepoint(int, int);
|
||||
struct point *makepoint_p(int, int);
|
||||
|
||||
int main() {
|
||||
struct point p = {2, 3};
|
||||
printf("x: %d, y: %d\n", p.x, p.y);
|
||||
|
||||
struct rect r1 = {{2, 3}, {3, 4}};
|
||||
printf("pt1.x: %d, pt1.y: %d, pt2.x: %d, pt2.y: %d\n", r1.pt1.x, r1.pt1.y,
|
||||
r1.pt2.x, r1.pt2.y);
|
||||
|
||||
struct point p2 = makepoint(3, 4);
|
||||
printf("x: %d, y: %d\n", p2.x, p2.y);
|
||||
|
||||
struct point *p3 = makepoint_p(3, 4);
|
||||
printf("x: %d, y: %d\n", p3->x, p3->y);
|
||||
free(p3);
|
||||
}
|
||||
|
||||
struct point makepoint(int x, int y) {
|
||||
struct point p = {x, y};
|
||||
return p;
|
||||
}
|
||||
|
||||
struct point *makepoint_p(int x, int y) {
|
||||
struct point *p = malloc(sizeof(struct point));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
|
||||
return p;
|
||||
}
|
21
typeconversions.c
Normal file
21
typeconversions.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int atoi(char s[]);
|
||||
|
||||
int main() {
|
||||
printf("%d\n", atoi("234"));
|
||||
}
|
||||
|
||||
int atoi(char string[]) {
|
||||
int n = 0;
|
||||
/*
|
||||
* v1
|
||||
for (int i = 0; string[i] >= '0' && string[i] <= '9'; ++i)
|
||||
n = 10 * n + (string[i] - '0');
|
||||
*/
|
||||
for (int i = 0; isdigit(string[i]); ++i)
|
||||
n = 10 * n + (string[i] - '0');
|
||||
|
||||
return n;
|
||||
}
|
25
typeslength.c
Normal file
25
typeslength.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum boolean { NO, YES };
|
||||
|
||||
int main() {
|
||||
printf("\nINTEGER TYPES:\n");
|
||||
printf("%-20s%ld bytes wide\n", "char: ", sizeof(char));
|
||||
printf("%-20s%ld bytes wide\n", "int: ", sizeof(int));
|
||||
printf("%-20s%ld bytes wide\n", "short int: ", sizeof(short int));
|
||||
printf("%-20s%ld bytes wide\n", "long: ", sizeof(long));
|
||||
printf("%-20s%ld bytes wide\n", "long int: ", sizeof(long int));
|
||||
printf("%-20s%ld bytes wide\n", "long long int: ", sizeof(long long int));
|
||||
|
||||
printf("\nFLOATING POINT TYPES:\n");
|
||||
printf("%-20s%ld bytes wide\n", "float: ", sizeof(float));
|
||||
printf("%-20s%ld bytes wide\n", "double: ", sizeof(double));
|
||||
printf("%-20s%ld bytes wide\n", "long double: ", sizeof(long double));
|
||||
|
||||
#if defined(__STDC_VERSION__)
|
||||
printf("\n%ld\n", __STDC_VERSION__);
|
||||
#endif
|
||||
printf("\nseveral " "string " "constants.\n");
|
||||
return 0;
|
||||
}
|
15
wc.c
Normal file
15
wc.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
/*
|
||||
long num_chars = 0;
|
||||
|
||||
while (getchar() != EOF)
|
||||
++num_chars;
|
||||
* or..
|
||||
*/
|
||||
long num_chars;
|
||||
|
||||
for (num_chars = 0; getchar() != EOF; ++num_chars);
|
||||
printf("%ld\n", num_chars);
|
||||
}
|
24
word_count.c
Normal file
24
word_count.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define IN 1
|
||||
#define OUT 0
|
||||
|
||||
int main() {
|
||||
int character, lines, words, chars, state;
|
||||
|
||||
state = OUT;
|
||||
lines = words = chars = 0;
|
||||
while ((character = getchar()) != EOF) {
|
||||
++chars;
|
||||
if (character == '\n')
|
||||
++lines;
|
||||
if (character == ' ' || character == '\n' || character == '\t')
|
||||
state = OUT;
|
||||
else if (state == OUT) {
|
||||
state = IN;
|
||||
++words;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n%d %d %d\n", lines, words, chars);
|
||||
}
|
Reference in New Issue
Block a user