Using perror in C to convert error numbers to a string

8. February 2012 08:00

 

A short example on how to convert error number to a human readable string in C.

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
	int err = 0;

	if (argc < 2) {
		printf("Usage: %s <error number>\n", argv[0]);
		exit(-1);
	}

	err = atoi(argv[1]);

	printf("Error: %s\n", strerror(err));	


	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink