Stop Microsoft

Miscellaneous => Programming & Networking => Topic started by: Calum on 17 August 2002, 02:27

Title: A simple C query.
Post by: Calum on 17 August 2002, 02:27
okay, well i've been meaning to ask this for a good few months now, and it's not detrimental, so don't worry about it or anything, well, for a start off, here's this hello world program i wrote. It was the first thing i wrote off the top of my head, not in any way copied out of a book. We all have to start somewhere, i told myself:
Code: [Select]
Now, to me, that looks okay, but when i compile it, look what happens:
Code: [Select]
why is this? what are these two errors? i don't know what they mean, and even more mystifying to me, the program runs exactly as expected, printing Hello, World! and then a newline. So what's the deal?

thanks, but as i say, it's not important, i'm just nosey!  :D
Title: A simple C query.
Post by: choasforages on 17 August 2002, 03:45
ummm, actally the code would go like this


#include <stdio.h>
int main()
{
printf("hello world");
return 0;
}

always have your includes at the top, and not in the body of the prog, you must also to return in everyfunction. somebody with more experiance correct this
Title: A simple C query.
Post by: Calum on 17 August 2002, 15:48
thanks! so, if one were to compile the above, then it would get no errors? i will try this later..
Title: A simple C query.
Post by: DC on 17 August 2002, 17:28
quote:
Originally posted by Calum:

hello.c:2: warning: return type defaults to `int'
hello.c:2: warning: function declaration isn't a prototype
hello.c: In function `main':
hello.c:5: warning: control reaches end of non-void function



Choasforages is right, but for a more detailed note on what it actually says:
warning: return type defaults to `int'
Your 'main' function doesn't have a return type. This must be something. 'void' is for functions that don't have to return anything, int and char are what it says. The standard for the main() function is int, most programs use that (it's standard to use that for error codes, where '0' is normal execution).
GCC defaults to 'int'

warning: control reaches end of non-void function
main() is now a function that returns an integer (it isn't 'void', which means it has to return something), but it reached the end without seeing a 'return'.

warning: function declaration isn't a prototype
hello.c: In function `main':
This probably has to do with #include being in the body of the program. #include copies everything in the given header file in the source (the preprocessor does this), and so all the functions in the include file land *in* the main() function which isn't good.

Hope that helped cure your noseyness  ;)
Title: A simple C query.
Post by: foobar on 18 August 2002, 01:24
More noseyness:

What if you do return 1, or 2, or even 74836 ??
Title: A simple C query.
Post by: DC on 18 August 2002, 02:09
quote:
Originally posted by -=f00bar=-:
More noseyness:

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



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  ;) .
Title: A simple C query.
Post by: choasforages on 18 August 2002, 05:52
actally, i think that you can return anything

like

float crapper;
oddfucntion = crapper;
return crapper

then agian, im not sure
Title: A simple C query.
Post by: voidmain on 18 August 2002, 08:56
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: [Select]

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 ]

Title: A simple C query.
Post by: choasforages on 18 August 2002, 21:03
can't you return pointers?
Title: A simple C query.
Post by: voidmain on 18 August 2002, 21:43
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).