Helpful Information
 
 
Category: C Programming
Book To Old To Learn C From?

I have the following (incredibly) simple code:



#include <stdio.h>

void main() {
printf("Hello, world!");
exit();
}


When I feed this (incredibly) simple code to the gcc (4.8 I think) compiler, I get a warning about the return value, and it halts.

Now, this is an exact replica of the first code presented in the C book I had intended to use for learning, but if this won't compile, that would seem to suggest that perhaps there is something that this (somewhat) dated book is not covering. Is this a warning perhaps that I should get a newer C book before I dive into learning C/C++?

I have succesfully compiled this code on an old copy of Borland's C/C++ compiler under DOS. I just need to know if there's some new concept that my book is not incorporating here.

the problem is unix. in unix the return code of your main function will be passed to the calling program.
you donīt supply one.
DOS does not need this value though.

itīs only a warning, not an error... so you should have an "a.out" in your directory...

the best book i found for learning c (if you already know basic or pascal or some other languages) is the "Programming C" by Kernighan and Ritchie - the inventors of C and unix :)

Aha.... thank you! And thank you for the book reccomendation. The reason I really enjoyed Programming Perl is because it is written by someone(s) who clearly know exactly what is going on. I prefer to have all of the icky details laid out in front of me so that I can peruse them and piece things together instead of having some watered down version of the language tossed at me only to find out later that I've been using horrible programming practice in it the whole time.

According to the ANSI standard, main() is supposed to return an int,

and if you specify a function to return a value, and you don't return one, the compiler will yell at you.

Any good C programmer will always treat warnings as errors.

You should compile with -Wall -ansi -pedantic flags, unless you are intentionally using non ANSI code.

if you code for MSDOS, you really donīt need to care.
but in general i agree. keep your code 100% ansi compliant! one day you might want to use it on another OS....

Well, now I'm coding under Red Hat Linux 7.0, not MSDOS. What I need to do first, I've realized, is learn how to use gcc. I'm used to being able to just feed an interpreter my code and see the results right away, but this is more complicated, and I never really got anywhere when I originally started learning C (I've just recently come to understand why one would use pointers instead of just passing the actual variable from function to function). I coded some simple C apps for MSDOS a long time ago, but I've given up on trying to code anything useful on Windoze. So, I'm learning Linux, the various apps for Linux, and C/C++ all at once. :D No sweat!

I never did really big projects in c/linux, but one tip from my experience:

use emacs as editor. itīs really good. you can compile, run and debug your program in it just like in the "Turbo C" ide on dos. even better: use emacs running on xwindows. then you also have dropdowns and popups to work with :cool:

I removed the exit() part, and it worked....how it was supposed to work....I guess.

Yea, I dropped the void and the exit() which made it stop whining about the return value. It just seemed very strange to me that it seemed NOT to want to see any return value.

I left the void part, still worked...what is void() anyway?

"void" is nothing ;)

you use it if your function returns no value or for parameters that have no specific type.

Or when you get mad that it keeps returning everything but what you expect ;)

If you put a newline in your printf statement:



#include <stdio.h>

int main()
{
printf("Hello World!\n");
return 1;
}


then the compiler guarantees a flush of the output buffer. Which is generally a good thing too.

#include <stdio.h>


void main() {

printf("Hello, world!");

exit();

}

the problem is... when the program starts, main() is called. when it ends, it returns 1 or 0 (1 means it closed due to an error, and the OS should make a fuss about it, and 0 means everything went swell)

Just change it to



int main(){

printf("Hello, world!");

exit(0);

}

"exit(0)" is basically the same as saying "return 0";)

Originally posted by Lord MJ
According to the ANSI standard, main() is supposed to return an int,

Just to confuse matters here, ISO specifies that it is illegal to declare void main() in C++, not C: http://homepages.tesco.net/~J.deBoynePollard/FGA/legality-of-void-main.html

However, using void main() (though legal) is a bad practice in C, IMHO :)










privacy (GDPR)