Author Topic: A simple C query.  (Read 1775 times)

Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
A simple C query.
« 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
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

choasforages

  • VIP
  • Member
  • ***
  • Posts: 1,729
  • Kudos: 7
    • http://it died
A simple C query.
« Reply #1 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
x86: a hack on a hack of a hackway
alpha, hewlett packed it A-way
ppc: the fruity way
mips: the graphical way
sparc: the sunny way
4:20.....forget the DMCA for a while!!!

Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
A simple C query.
« Reply #2 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..
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

DC

  • Member
  • **
  • Posts: 211
  • Kudos: 0
A simple C query.
« Reply #3 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  ;)
GS/CS d- s-: a--- C++ UL+ P+ L++>+++ E W++ N>+ o K- w-- O- M V? PS+>++ PE- Y+ PGP t+ 5+ X R tv+ b+++ DI+ D+ G++ e>++++ h! r- y
A quantummechanical wavefunction describing an unknown amount of bottles of beer on the wall
A quantummechanical wavefunction describing an unknown amount of bottles of beer on the wall
We take a measurement, the wavefunction will collapse, and one of the bottles of beer will fall

foobar

  • Member
  • **
  • Posts: 308
  • Kudos: 0
    • http://www.fuckmicrosoft.com
A simple C query.
« Reply #4 on: 18 August 2002, 01:24 »
More noseyness:

What if you do return 1, or 2, or even 74836 ??
Linux user #283039

Gosh, I love Linux Quake.


DC

  • Member
  • **
  • Posts: 211
  • Kudos: 0
A simple C query.
« Reply #5 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  ;) .
GS/CS d- s-: a--- C++ UL+ P+ L++>+++ E W++ N>+ o K- w-- O- M V? PS+>++ PE- Y+ PGP t+ 5+ X R tv+ b+++ DI+ D+ G++ e>++++ h! r- y
A quantummechanical wavefunction describing an unknown amount of bottles of beer on the wall
A quantummechanical wavefunction describing an unknown amount of bottles of beer on the wall
We take a measurement, the wavefunction will collapse, and one of the bottles of beer will fall

choasforages

  • VIP
  • Member
  • ***
  • Posts: 1,729
  • Kudos: 7
    • http://it died
A simple C query.
« Reply #6 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
x86: a hack on a hack of a hackway
alpha, hewlett packed it A-way
ppc: the fruity way
mips: the graphical way
sparc: the sunny way
4:20.....forget the DMCA for a while!!!

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
A simple C query.
« Reply #7 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 ]

Someone please remove this account. Thanks...

choasforages

  • VIP
  • Member
  • ***
  • Posts: 1,729
  • Kudos: 7
    • http://it died
A simple C query.
« Reply #8 on: 18 August 2002, 21:03 »
can't you return pointers?
x86: a hack on a hack of a hackway
alpha, hewlett packed it A-way
ppc: the fruity way
mips: the graphical way
sparc: the sunny way
4:20.....forget the DMCA for a while!!!

voidmain

  • VIP
  • Member
  • ***
  • Posts: 5,605
  • Kudos: 184
    • http://voidmain.is-a-geek.net/
A simple C query.
« Reply #9 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).
Someone please remove this account. Thanks...