Return Values

This is a discussion on Return Values within the C Programming forums, part of the General Programming Boards category; I'm really sorry if this is a dumb question but ever since I started using C, I never understood the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Return Values

    I'm really sorry if this is a dumb question but ever since I started using C, I never understood the reason why functions are defined with much larger values then required.

    I'm planning to write an commercial API so I wish to get my code written as best as possible, so I won't have to go back and fix things later.

    My questions is that they're are millions of functions out there but many are definied with much higher variables then required.

    An example of this is the int SDL_Flip(SDL_Surface *screen); function in the Simple Direct Layer library. The function will only return two possible values, 0 for success & -1 on an error. So why is this defined as an interger? Wouldn't be better to define this as an char?

    int main(int argc, char *argv[]) is another one. I'll be very surpised if their was a single program that could return 4228250625 different return values with good reason. I'm sure most people just use two. One for a success and another for an error. I'm guessing that main() is an expection however, because it's not like any other function.

    So why are some many programs and functions like this? I'm sure that changing these won't ever gain the slightest speedup, but this wouldn't this make your executables a few bytes smaller?

    Thanks to any replies. Once again sorry if this was a stupid question.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    If you are trying to save space then I guess so. But at a higher level point of view, you know you are checking for a number/integer not an actual character.

    There are some other exceptions like getchar() returns an int because some values are in fact larger than a char.
    Last edited by sand_man; 03-20-2006 at 01:51 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > Wouldn't be better to define this as an char?
    Historically, all C functions returned int by default, unless you went out of your way to indicate that it returned something else.

    Also return values in particular are returned in machine registers, so it makes sense to use a type which is closest to the machine, and traditionally int served this role.

    The char type may be signed or unsigned, so it does not make sense to try and return a negative value since that would break on machines which default to char as being unsigned.

    > I'm planning to write an commercial API so I wish to get my code written as best as possible
    You could consider making all your return types as enumerations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    And something else you might want to consider....

    If it is commercial, at some point it will probably have to be maintained by someone else. While trying to keep it as small as possible is a laudable goal, if you get too far from the "norm", you might pay a price later on if someone other programmer needs to come up to speed and maintain it.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by Drahcir
    I'm sure that changing these won't ever gain the slightest speedup, but this wouldn't this make your executables a few bytes smaller?
    As Salem said, int tends to be the size of a register on a given machine, so using char instead of int for function return values may actually increase the size of the executable, since you need extra instructions to convert.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21