Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:05:29+00:00 2026-06-03T23:05:29+00:00

While working on relatively straightforward C code at work today, I began day-dreaming a

  • 0

While working on relatively straightforward C code at work today, I began day-dreaming a little and wrote up the following library for effectively producing error messages. The jury is still out – in my mind, at least – whether this is a more effective approach than simply adapting perror with errno to your purposes… It doesn’t even need to be a “library” at all, rather just a set of macros.

For now, I’m calling the library elib:

elib.h

#ifndef _ELIB_H_
#define _ELIB_H_

struct ErrorMap {
    void (*fp);
    int errorCode;
    char * message;
};

#define eperror(...) fprintf(stderr,  ##__VA_ARGS__)
char * getErrorMsg(struct ErrorMap *, void (*fp), int);

#endif /* _ELIB_H_ */

elib.c

#include <stdlib.h>
#include "elib.h"

char * getErrorMsg(struct ErrorMap * errorMap, void (*fp), int code)
{
    int i;
    // TODO: Replace naive search
    for (i=0; errorMap[i].fp != NULL; i++)
    {
        if (errorMap[i].fp == fp && errorMap[i].errorCode == code)
        {
            return errorMap[i].message;
        }
    }
    return NULL;
}

test.c (sample app “xyzlib”)

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

#include "elib.h"

int xyzlib_openFile(int);
int xyzlib_putStuff(char *);

static struct ErrorMap xyzlib_ErrorMap [] = {
    {xyzlib_openFile, -1, "Argument is less than 3"},
    {xyzlib_putStuff, -1, "Argument is NULL"},
    {xyzlib_putStuff, -2, "Length of argument is 0"},
    {xyzlib_putStuff, -3, "Stuff is too long"},
    {NULL, 0, NULL}
};


int xyzlib_openFile(int fd)
{
    if (fd > 3)
    {
        return (-1);
    }

    // open a file.. or something.

    return 0;
}

int xyzlib_putStuff(char * stuff)
{
    if (stuff == NULL)
    {
        return (-1);
    }

    if (strlen(stuff) == 0)
    {
        return (-2);
    }

    if (strlen(stuff) > 3)
    {
        return (-3);
    }

    // do something ...

    return (0);
}


int main(int argc, char ** argv)
{
    int code;

    if (argc != 2)
    {
        printf("Usage: %s <arg>\n", argv[0]);
        return 1;
    }

    code = xyzlib_putStuff(argv[1]);

    if (code < 0)
    {
        eperror("Error: %s\n", getErrorMsg(xyzlib_ErrorMap, xyzlib_openFile, code));    
    }
}

Basically, you define/register the return codes in the ErrorMap table, and if you ever receive an error indication (response value < 0), then the getErrorMsg will look through the registered table for the corresponding code and function to get the proper message. I think the value is that for a given library taking this approach to error reporting, error codes can be defined per function (rather than globally) – simplifying management of all the codes.

However, in addition to a small overhead looking up the appropriate message, all C code taking this approach would require every function (that doesn’t return non-negative integers) to return ints, and then use the first argument to be a pointer to the return value — Slightly non-idiomatic but commonly used.

Let’s say the target market is software for high-reliability embedded devices (but not terribly resource-constrained).

Any thoughts? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:05:31+00:00Added an answer on June 3, 2026 at 11:05 pm

    I don’t really see a point in this, sorry. Maybe that’s because of how i work with error codes in C. Basically i define error values as a part of the interface of a single module. The error value between modules do collide, i.e. i don’t care if two modules have the same numerical value for their different error codes. To get strong type-checking for different enums i usually wrap them into a struct. But i never really care about making error codes unique for each function which is what your approach seems to be about.

    Again, this is my specific, but i never really felt the need to do otherwise. Error codes defined for the whole module usually fit to be passed between procedures that implement this module. Now what if your approach could actually be mapped on a module boundary instead of the function? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on a web application with struts, I came across the following situation.
I've got a relatively small project written in ASP.NET MVC3. After working a while,
I have been working with android for a little while now and feel pretty
While working on an auditing project I came across different problems regarding software and
While working in a project written by some one else ,there's a Function which
While working on the Clojure Koans, I had to calculate the factorial of a
While working in IntelliJ IDEA, the keyboard occasionally locks in a way that delete
While working with PHP ,handling connection's with Database (MySQL) $result = mysql_query('select * from
While working my way through Cay S. Horstmann's Scala for the Impatient, I noticed
While working on my project I've found out that \n in html source is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.