kandr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 3a14f3cf7a1bd094ae73a4d2ca151464a9eda224
Author: aabacchus <ben@bvnf.space>
Date:   Wed, 19 May 2021 00:11:26 +0100

might as well make commits out of these

Diffstat:
A.gitignore | 2++
A1.2.c | 23+++++++++++++++++++++++
A1.5.c | 17+++++++++++++++++
A1.6.c | 20++++++++++++++++++++
Acat.c | 10++++++++++
Amax.c | 11+++++++++++
Awc.c | 19+++++++++++++++++++
7 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +a.out +*.o diff --git a/1.2.c b/1.2.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdlib.h> + +#define LOWER 0 +#define UPPER 300 +#define STEP 20 + +int main() { + float fahr; + size_t fahrs; + void *fahrp; + fahrs = sizeof(float); + fahrp = malloc(fahrs); + fahr = 0; + printf("value:%f size:%d pointer: %p\n", fahr, fahrs, fahrp); + + printf("%3s\t%6s\n", "F", "C"); + for (fahr=UPPER; fahr >= LOWER; fahr -= STEP) { + printf("%3.0f\t%6.2f\n", fahr, 5. * (fahr - 32.) / 9.); + } + free(fahrp); + return 0; +} diff --git a/1.5.c b/1.5.c @@ -0,0 +1,17 @@ +#include <stdio.h> + +int main() { + int c, nb; + char inWord = 0; + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + inWord = 0; + } + else if (inWord == 0) { + putchar('\n'); + inWord = 1; + } + putchar(c); + } + return 0; +} diff --git a/1.6.c b/1.6.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +int main () { + int c, i, nwhite, nother; + int ndigit[10] = {0}; + nwhite = nother = 0; + + while ((c = getchar()) != EOF) + if (c >= '0' && c <= '9') + ++ndigit[c-'0']; + else if (c == ' ' || c == '\n' || c == '\t') + ++nwhite; + else + ++nother; + printf("digits ="); + for (i = 0; i < 10; ++i) + printf(" %d", ndigit[i]); + printf(". white space = %d, other = %d\n", + nwhite, nother); +} diff --git a/cat.c b/cat.c @@ -0,0 +1,10 @@ +// from section 1.5 of K&R +#include <stdio.h> + +int main() { + int c; + while ((c = getchar()) != EOF) { + putchar(c); + } + return 0; +} diff --git a/max.c b/max.c @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <time.h> +int main() { + char i = 0; + while (1) { + printf("%d\n", i); + i++; + sleep(1); + } + return 0; +} diff --git a/wc.c b/wc.c @@ -0,0 +1,19 @@ +#include <stdio.h> + +int main() { + long nc, nw, nl; + int c; + char inWord; + nc = nw = nl = 0; + while ((c = getchar()) != EOF) { + if (c == '\n') ++nl; + if (c == ' ' || c == '\t' || c == '\n') inWord=0; + else if (inWord == 0) { + inWord = 1; + ++nw; + } + ++nc; + } + printf(" %ld %ld %ld\n", nl, nw, nc); + return 0; +}