99 ways to program a hex, Part 7: C89, const correctness

Standardization to C brought with it a way to annotate variables other than its type: how it is to be accessed. volatile [1] informs the compiler that the value cannot be cached and must always be read from when referenced, because some outside agent (hardware, another process or thread) could have changed the contents since the last read, and const [2], which marks a variable as “read-only,” which means the value can be heavily cached as it won't change what-so-ever.

So today's code is the base version [3] (which is C89 [4]), but with “const correctness.”

/*************************************************************************
*************************************************************************/
/* Style: C89, const correctness */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define LINESIZE 16
static void do_dump (FILE *const,FILE *const);
/****************************************************************/
int main(const int argc,char *const argv[])
{
if (argc == 1)
do_dump(stdin,stdout);
else
{
int i;
for (i = 1 ; i < argc ; i++)
{
FILE *fp;
fp = fopen(argv[i],"rb");
if (fp == NULL)
{
perror(argv[i]);
continue;
}
printf("-----%s-----\n",argv[i]);
do_dump(fp,stdout);
fclose(fp);
}
}
return EXIT_SUCCESS;
}
/******************************************************************/
static void do_dump(FILE *const fpin,FILE *const fpout)
{
unsigned char buffer[BUFSIZ];
unsigned char *pbyte;
size_t offset;
size_t bread;
size_t j;
char ascii[LINESIZE + 1];
offset = 0;
while((bread = fread(buffer,1,BUFSIZ,fpin)) > 0)
{
pbyte = buffer;
while (bread > 0)
{
fprintf(fpout,"%08lX: ",(unsigned long)offset);
j = 0;
do
{
fprintf(fpout,"%02X ",*pbyte);
if (isprint(*pbyte))
ascii [j] = *pbyte;
else
ascii [j] = '.';
pbyte ++;
offset ++;
j ++;
bread --;
} while ((j < LINESIZE) && (bread > 0));
ascii [j] = '\0';
if (j < LINESIZE)
{
size_t i;
for (i = j ; i < LINESIZE ; i++) fprintf(fpout," ");
}
fprintf(fpout,"%s\n",ascii);
}
if (fflush(fpout) == EOF)
{
perror("output");
exit(EXIT_FAILURE);
}
}
}
/***************************************************************/

There're no real volatile variables, so there's no use of volatile, but the use of const ensures that I don't change variables inadvertently. One thing to note: The following:

const int *pi;

creates a pointer that can change, which points to memory (interpreted as an integer) that can't change, while:

int *const pi;

creates a pointer that can't change, which points to memory (interpreted as an integer) that can change, while:

const int *const pi;

creates a pointer that can't change, which points to memory (interpreted as an integer) that can't change.

Yes, there are some subtle differences there, and it took me a while to get it down, but you can pin down what can and can't change.

=> [1] http://en.wikipedia.org/wiki/Volatile_variable | [2] http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html | [3] /boston/2012/01/09.1 | [4] http://en.wikipedia.org/wiki/ANSI_C | [5] /boston/2012/01/14.1 | [6] /boston/2012/01/16.1

=> Gemini Mention this post | Contact the author

Proxy Information
Original URL
gemini://gemini.conman.org/boston/2012/01/15.1
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
559.869428 milliseconds
Gemini-to-HTML Time
4.720694 milliseconds

This content has been proxied by September (ba2dc).