Error Handling.

Discussion in 'Mac Programming' started by larswik, Dec 27, 2010.

  1. larswik macrumors 68000

    Sep 8, 2006
    SO I am humming right a long in my Learning C book and then my brain freezes on Chapter 9 Error handling. This line of code stopped me in my tracks, it's only part of the over all code.
        int     error;
        error = printf( "The title array takes up %ld bytes of memory.\n\n",  sizeof( title ) );
        if ( error < 0 )
            return kError_printf;
    Now my brain is telling me that printf is the argument that is begin passed to error. But error is of type int and it is begin passed a char string? When I run this it is printing the printf line but also storing some int value in error to be evaluated by IF? Huh?

  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    What does the printf function return? Hint: Typing man 3 <function name> gives you excellent documentation for standard library functions.

    So for example:

    man 3 printf
  3. larswik thread starter macrumors 68000

    Sep 8, 2006
    Very cool. I did not know you could use terminal to get detailed explanations of the functions, Thanks!

    I guess this line sums it up then from printf- " These functions return the number of characters printed (not including the trailing `\0' ) " I understand this now.

    So without having to look at every function to check this I will ask. So printf does 2 things when it executes, it writes the string and returns an int value. Do all functions do to things for the most part, one being the the intended use of the function and the second being an error that is returned?

    printf 1) writes the char string 2) returns a int value
    fgets 1) reads the char stream 2) returns NULL if there was an error.

  4. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Basically you'll just need to read the documentation for each function you are using. After awhile you'll see how certain families of functions deal with errors and how you should handle them.

    I will point out that you should read the man page for errno as that is the standard method for indicating which error has occurred.
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Often the return value of a function is for error handling, and pointer arguments are used to return values, etc. This is often because only one value can be returned and often a function's "result" is multiple values (or a large structure that would be expensive to return). There are times where a return value is a "switch hitter" and returns the result OR a designated return value that indicates errors.

    A way (maybe not the best) is to think of the types of things resulting from a function call:
    1) a value returned
    2) alteration of arguments passed via pointer
    3) side effects

    The first two are pretty straight-forward, the last can be obtuse. In the case of printf 2) doesn't come onto play, 1) is what you just discovered, and 3) is some text being written to stdout. Other possible side effects:
    Changing global program state
    Writing data to a file
    Storing data to a database
    Communicating via a network
    Resource management (claiming or freeing memory, a semaphore, etc.)
    Termination of a thread/the program
    Spawning a thread or process
    Faxing your resignation to your boss without your knowledge

    Often the "side effect" is all we really wanted, and the return value is there to let us know how things went.

  6. larswik thread starter macrumors 68000

    Sep 8, 2006
    Hummm.... This ads a new dimension of thinking. Pass one argument in and get multiple results back, unless it is the 'switch hitter'.

    Cromulent - Thanks for the tip on the 'man 3 printf' I can easily research functions now. I will read up on the errno.

    Lee210 - I understand that multiple results exist but will move forward in the book and not dig to deeply into that now. This simple code I wrote explained a lot to me in understanding what is happening.

    #include <stdio.h>
    int main () {
    	int error;
    	error = printf("Hello World");
    	printf("error int is %d: ",error);
        return 0;
    I was confused when I first saw error = printf(""); I have never seen anything in front of printf like that before.

    So... when I see 'error =' in the future, or 'variable = function' I know the 'Variable =' is being passed the RETURN value of what ever return type it is supposed to return, and the MAIN call to the function is performing it's normal task as in printf writing out to the stdout. If I need error correction for code I need to read each one to see what it RETURNS for error correction, if it returns something that is not s 'Switch Hitter'.

    Now if you will excuse me I am going to let my brain explode for the evening :)
  7. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    Of course the problem with that is that if the first printf gives you an error, what do you expect the second one will do? ;)


Share This Page