Skip to content

ASCII Control Characters

A header of named constants for every ASCII control character, with a test program that prints their values.

Published Dec 8, 1996

The first thirty-two and the last code point of the ASCII table are all unnamed in most C headers, which makes protocol code full of magic numbers. This snippet defines them once - NUL, SOH, STX, ETX, EOT, ENQ, ACK, BEL, BS, HT, LF, VT, FF, CR, SO, SI, DLE, DC1-DC4, NAK, SYN, ETB, CAN, EM, SUB, ESC, FS, GS, RS, US, DEL - so you can write readable checks like if (ch == ETX). Use it whenever you are parsing raw serial or network protocols by hand.

#include <Windows.H>
#include <WindowsX.H>
#include <Stdio.H>
#include <Stdlib.H>

#include "Headers/ASCII.H"

int main()
{
	printf("%d\n",NUL);
	printf("%d\n",SOH);
	printf("%d\n",STX);
	printf("%d\n",ETX);
	printf("%d\n",EOT);
	printf("%d\n",ENQ);
	printf("%d\n",ACK);
	printf("%d\n",BEL);
	printf("%d\n",BS_);
	printf("%d\n",HT_);
	printf("%d\n",LF_);
	printf("%d\n",VT_);
	printf("%d\n",FF_);
	printf("%d\n",CR_);
	printf("%d\n",SO_);
	printf("%d\n",SI_);
	printf("%d\n",DLE);
	printf("%d\n",DC1);
	printf("%d\n",DC2);
	printf("%d\n",DC3);
	printf("%d\n",DC4);
	printf("%d\n",NAK);
	printf("%d\n",SYN);
	printf("%d\n",ETB);
	printf("%d\n",CAN);
	printf("%d\n",EM_);
	printf("%d\n",SUB);
	printf("%d\n",ESC);
	printf("%d\n",FS_);
	printf("%d\n",GS_);
	printf("%d\n",RS_);
	printf("%d\n",US_);
	printf("%d\n",DEL);

	return 0;
}
An unhandled error has occurred. Reload 🗙