LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2003, 04:56 PM   #1
Patchorus
LQ Newbie
 
Registered: Aug 2003
Location: Sweden
Distribution: Fedora core 4
Posts: 25

Rep: Reputation: 15
void?


what is the difference between Void and int?
for example:

void main()
{
blabla;
}

....
int main()
{
blabla;
}


becaus like the "hello world" program (the only one i have made this far) uses int,
but i saw a source i downladed had void...
this is so confusing )
 
Old 10-23-2003, 05:02 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
It means the return value of the main function. When void, the compiler assumes that you don't want to return a value and so returns 0 to the shell (or whoever was the invoker of the program/function). You can read the return value from shell directly by saying 'echo $?' after the command or implicity by && and || operators.

Writing the return value is done by saying something like
'return 27;' in the end of the function.
 
Old 10-23-2003, 05:10 PM   #3
Patchorus
LQ Newbie
 
Registered: Aug 2003
Location: Sweden
Distribution: Fedora core 4
Posts: 25

Original Poster
Rep: Reputation: 15
aha, thanks
that helped me understand it a little.
guess i have to study really hard to
understand it fully
 
Old 10-23-2003, 05:15 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
..and "int" means that the main() function returns a integer (..., -85, 0, 1, 2, ) and "void" means that main() does not return anything. You could say that the "void"-version is less complete. If you run the compiler (gcc) on it with strict syntax-checking (gcc's -Wall -pedantic options), it will output a warning about this.

Also, the "int"-version should end with:

return 0; /* or some error code: 1,2,3... */

Because main is defined to return a integer number, and without a "return <number>" it would not comply to that.

Normally a program should return such an error code to the calling program
 
Old 10-23-2003, 05:20 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
So arguably the nicest hello world program is:
Code:
#include <stdio.h>

int main()
{
     printf("Hello world!\n");
     return 0;
}
To turn on strict syntax checking to see warnings, compile with:

gcc -Wall -pedantic -ansi -o hello hello.c

Last edited by Hko; 10-23-2003 at 05:21 PM.
 
Old 10-23-2003, 09:08 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
void is a C datatype, commonly used for functions that could have all kinds of data for arguments.

For instance, bsearch(), qsort() are standard parts of the C library.
They require that the last argument to the library call be a function pointer. The function prototype they expect is like this:

Code:
int compare(void *, void *);
meaning that you can pass it pointers to anything: an int, a double, a struct, etc. That is what K & R meant when creating the void datatype.

It doesn't just mean 'nothing' it really means 'anything you want'
 
Old 10-23-2003, 11:42 PM   #7
Pres
Member
 
Registered: Jun 2002
Location: Australia
Distribution: Slack 9.1
Posts: 232

Rep: Reputation: 30
void main() is naughty. According to ANSI, main() should return an int.
 
Old 10-24-2003, 12:35 AM   #8
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
jim mcnamara,

What you say is true about void * but not for void. void * is a different type to void, one is a pointer type and the other is not a type, unless you consider 'the lack of' as a type.

Cheers.
 
Old 10-24-2003, 10:06 PM   #9
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Seg - it's still a datatype by definition.
 
Old 10-25-2003, 07:24 PM   #10
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Sure it is by definition, I was trying to highlight the point that it is not an "anything" data type as void * .

I thought it was relevent since the thread starter was querying about the type 'void'..

Cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what are void pointers used for? aditya1 Linux - General 10 03-14-2005 03:06 PM
void pointers suchi_s Programming 9 11-08-2004 03:05 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM
Virtual void Cyth Linux - General 7 01-02-2002 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration