Skip to content

Instantly share code, notes, and snippets.

@aptavout aptavout/putch.c
Created Sep 22, 2014

Embed
What would you like to do?
A fixed-length putchar()
#include <stdio.h>
#define MAXLINE 81 /* MAXLINE = MAXCARD + 1 */
#define MAXCARD 80
#define NEWLINE '\n'
#define BLANK ' '
static int putch_lastc = -1;
static char putch_buf[MAXLINE];
void putch(char c);
main()
{
char c;
while (getch(&c) != EOF) {
putch(c);
}
}
/* see previous gist for getch()... */
void putch(char c)
{
int i;
extern int putch_lastc;
extern char putch_buf[];
if (putch_lastc >= MAXCARD || c == NEWLINE) {
for (i = putch_lastc + 1; i < MAXCARD; ++i)
putch_buf[i] = BLANK;
for (i = 0; i < MAXCARD; ++i)
putchar(putch_buf[i]);
if (c == NEWLINE)
putchar(NEWLINE);
putch_lastc = -1;
}
if (c != NEWLINE) {
putch_lastc++;
putch_buf[putch_lastc] = c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.