Miscellaneous > Programming & Networking

A simple C query.

<< < (2/2)

DC:

quote:Originally posted by -=f00bar=-:
More noseyness:

What if you do return 1, or 2, or even 74836 ??
--- End quote ---


There are some commands to show the exit code of a program, and they'd show that other number then.

Plus, I assume that the '&&' (and friend) options in bash and other shells depend on the exit code to see if the command exited normally ('x && y' executes x, then y IF x didn't return an error). So returning 1 on normal execution would cause other programs to assume an abnormal termination.

I *think* that the return value goes to 255, but I'm not sure about that (or what happens if it's higher). Try it  ;) .

choasforages:
actally, i think that you can return anything

like

float crapper;
oddfucntion = crapper;
return crapper

then agian, im not sure

voidmain:
Normally if you want to use your command within a script you will return an integer.  If a "0" is returned from your program that tells the shell that your program indicated it had completed with no errors.  Say you wrote a program called "plentyofspace" that checked for at least 1GB of free disk space.  If it has at least 1GB of free disk space then it returns a "0", less than 1GB returns "1" (or any non-zero number).

You could use this program in a file download bash script to download your Mandrake CD automatically. However, it would only attempt the download if there was 1GB of free disk space or more:


--- Code: ---
--- End code ---

Now this is not the most useful example because there are already system commands that can perform the same functions.  But the point is, the system uses the return codes of programs for various things and you can use them too.  When you write a function in C the function is designed to return something. Your entire program is really nothing more than a large function "int main()".

You might also write a program that exits with many different return codes depending on the conditions.  You can trap the return code in a script and branch the logic in the script accordingly.

[ August 17, 2002: Message edited by: VoidMain ]

choasforages:
can't you return pointers?

voidmain:
What good would returning a pointer be?  A pointer is only good within your program for the time it is running.  No matter what you return it will be interpereted by the system as a number (or exit code).

Navigation

[0] Message Index

[*] Previous page

Go to full version