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

  • SEARCH
  • Home
  • 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 569695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:19:22+00:00 2026-05-13T13:19:22+00:00

My program is written in C for Linux, and has many functions with different

  • 0

My program is written in C for Linux, and has many functions with different patterns for return values:

1) one or two return n on success and -1 on failure.
2) some return 0 on success and -1 on failure.
3) some return 1 on success and 0 on failure (I generally spurn using a boolean type).
4) pointers return 0 on failure (I generally spurn using NULL).

My confusion arises over the first three — functions that return pointers always return 0 on failure, that’s easy.

The first option usually involves functions which return a length which may only be positive.

The second option, is usually involved with command line processing functions, but I’m unsure it has correctness, perhaps better values would be EXIT_SUCCESS and EXIT_FAILURE?

The third option is intended for functions which are convenient and natural to be called within conditions, and I usually emulate a boolean type here, by using int values 1 and 0.

Despite this all seeming reasonably sensible, I still find areas where this is not so clear or obvious as to which style to use when I create the function, or which style is in use when I wish to use it.

So how can I add clarity to my approach when deciding upon return types here?

  • 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-05-13T13:19:22+00:00Added an answer on May 13, 2026 at 1:19 pm

    So how can I add clarity to my approach when deciding upon return types here?

    Pick one pattern per return type and stick with it, or you’ll drive yourself crazy. Model your pattern on the conventions that have long been established for the platform:

    • If you are making lots of system calls, than any integer-returning function should return -1 on failure.

    • If you are not making system calls, you are free to follow the convention of the C control structures that nonzero means success and zero means failure. (I don’t know why you dislike bool.)

    • If a function returns a pointer, failure should be indicated by returning NULL.

    • If a function returns a floating-point number, failure should be indicated by returning a NaN.

    • If a function returns a full range of signed and unsigned integers, you probably should not be coding success or failure in the return value.

    Testing of return values is a bane to C programmers. If failure is rare and you can write a central handler, consider using an exception macro package that can indicate failures using longjmp.

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

Sidebar

Related Questions

I have a data acquisition program written in C++ (Visual Studio 6.0). Some clients
I have a console program written in C# that I am using to send
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have a fairly large program written in C. It spans several files, and
I am making changes to an old program written in VC++6. the project resources
I have a classic client/server (fat client and database) program written in Delphi 2006.
In our project the Java webservice communicate with the backend program written in C
I have a bash script that runs a simulation program written in Fortran 90,
I'm looking for a lightweight way to make my program (written in C) be
While it's my understanding that there's no fundamental reason a program written for 32-bit

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.