Last active
January 21, 2025 05:56
-
-
Save zacque0/e626537c0eed399e004e4916377f0533 to your computer and use it in GitHub Desktop.
CL's read-char vs C's getchar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Modified version of: https://github.com/soffes/k-and-r/blob/master/examples/1.9-1.c | |
| // Compile with the command: gcc longest-length.c | |
| #include <stdio.h> | |
| #define MAXLINE 1000 // Maximum input line size | |
| // Note: renamed from `getline` to `gline` since `getline` is already defined | |
| int gline(char line[], int maxline); | |
| void copy(char to[], char from[]); | |
| // Print longest input line | |
| main() { | |
| int len; // Current line length | |
| int max; // Maximum length seen so far | |
| char line[MAXLINE]; // Current input line | |
| max = 0; | |
| while ((len = gline(line, MAXLINE)) > 0) { | |
| if (len > max) { | |
| max = len; | |
| } | |
| } | |
| if (max > 0) { // There was a line | |
| printf("%d", max); | |
| } | |
| return 0; | |
| } | |
| // gline: read a line into `s`, return length | |
| int gline(char s[], int lim) { | |
| int c, i; | |
| for (i= 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { | |
| s[i] = c; | |
| } | |
| if (c == '\n') { | |
| s[i] = c; | |
| ++i; | |
| } | |
| s[i] = '\0'; | |
| printf("###count: %d\n", i); | |
| return i; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; Execution: sbcl --script longest-length.lisp | |
| (in-package "CL-USER") | |
| (defun main () | |
| "Read max line length." | |
| (loop with max = 0 | |
| for read-count = (read-count) | |
| while (> read-count 0) | |
| when (> read-count max) | |
| do (setf max read-count) | |
| finally (when (> max 0) (format t "~d" max)))) | |
| (defun read-count () | |
| (loop for c = (read-char *standard-input* nil 'eof) | |
| and i from 0 | |
| until (or (eq c 'eof) (char= c #\Newline)) | |
| finally (unless (eq c 'eof) | |
| (incf i)) | |
| (format t "###count: ~d~%" i) (return i))) | |
| (main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (in-package "CL-USER") | |
| (let ((seen-eof nil)) | |
| (defun read-char* (&optional (input-stream *standard-input*) eof-error-p | |
| eof-value recursive-p) | |
| (cond | |
| ;; Return cached EOF | |
| ;; Can be before/after `cl:read-char'. But I place it before. | |
| (seen-eof (if eof-error-p | |
| (error 'end-of-file) | |
| eof-value)) | |
| (eof-error-p (handler-case (read-char input-stream eof-error-p eof-value | |
| recursive-p) | |
| (end-of-file () | |
| (setf seen-eof t) | |
| (error 'end-of-file)))) | |
| (t (let ((result (read-char input-stream eof-error-p eof-value | |
| recursive-p))) | |
| (when (eq result eof-value) | |
| (setf seen-eof t)) | |
| result))))) | |
| (defun main () | |
| "Read max line length." | |
| (loop with max = 0 | |
| for read-count = (read-count) | |
| while (> read-count 0) | |
| when (> read-count max) | |
| do (setf max read-count) | |
| finally (when (> max 0) (format t "~d" max)))) | |
| (defun read-count () | |
| (loop for c = (read-char* *standard-input* nil 'eof) | |
| and i from 0 | |
| until (or (eq c 'eof) (char= c #\Newline)) | |
| finally (unless (eq c 'eof) | |
| (incf i)) | |
| (format t "###count: ~d~%" i) (return i))) | |
| (main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment