echo47 said:1. For ANSI C, those are all incorrect. Here are the two correct methods:
int main(void)
{
return 0;
}
int main(int argc, char *argv[])
{
return 0;
}
2. Those are not functions, they are function prototypes. The difference is each one passes a different type of argument to the function.
int main(int argc, char *argv[])
bogdanm said:The reason is quite subtle and is has to do with the way the compiler works. In C, after compiling the source file that has "main" in it, you get an object. But any information about the type of 'main' is gone in this object. That is, the linker (that must be called in order to get the final executable) sees only the symbol 'main' exported in the object, it doesn't have any information about the number and types of parameres and its return value. It also sees that 'main' is referenced from the startup file (this must be linked also in the final executable) and it just resolve the symbol, it doesn't care about its type. Things are a bit different in C++, but NOT for 'main'. In C++, 'main' is declared as "external C" by default, thus exhibiting the same behaviour as in C. The other functions (the ones that are not prefixed with "extern C") use a scheme called "name mangling" than encodes the type information with the function name to get a unique "function signature". This way, the type ambiguities are resolved. As stated before, the standard form for "main" is:
Code:int main(int argc, char *argv[])
Antharax said:It might be possible that void main(void) compiles with your compiler but it is wrong!
The int is used to return a value to the process which starts the program you've just written.
It is just written like this in the ANSI C standard so you should use it like that.
It the same as writing a text. When you make spelling errors, it might be possible that one reader understands what you mean and the other one doesn't. But it just ain't right to make those errors!
Regarding your second question
They are different in the type of parameter you pass to the function.
the first is a signed value (eg. -32767 to 32767)
the second is an unsigned value (eg. 0 to 65535)
the third is a constant unsigned value (meaning it cannot be altered in the function)
some compilers for microcontrollers with Harvard architecture (split program and data bus) use this to specify whether the parameter is a value in RAM or in ROM.
In C you can only have one of these functions in your program at once. In C++ you can have all of them at the same time (it supports overloading)
Antharax
suromenggolo said:int main(int argc, char *argv[])
{
return 0;
}
if you save to file : test.c and result executable file is test.exe
if you run test.exe with parameter like:
test.exe parameter1 parameter2
result int argc is 2, char *argv[0] is parameter1, char *argv[1] is parameter2
echo47 said:Please get youself a good C book and start reading!
"The C Programming Language", second edition, by Kernighan and Ritchie. It's a small efficient book, just like the language. Widely referred to as simply "K&R".
h**p://www.amazon.com/exec/obidos/asin/0131103628/
If you prefer long-winded tutorial books, I've thumbed through this one and it looks good: "C Programming - A Modern Approach" by K. N. King.
h**p://www.amazon.com/exec/obidos/asin/0393969452/
echo47 said:I haven't seen the King book anywhere, but K&R is pretty easy to find on P2P file sharing networks. I'll look around a bit and if I find a link, I'll PM you.
Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.
echo47 said:Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.
silvio said:echo47 said:Although K&R is a great book, it was published in 1988 before the 1999 ANSI standard. You'll see some old things like main() without an explicit int or return value.
Look here:
h**p://
suromenggolo wrote:
int main(int argc, char *argv[])
{
return 0;
}
if you save to file : test.c and result executable file is test.exe
if you run test.exe with parameter like:
test.exe parameter1 parameter2
result int argc is 2, char *argv[0] is parameter1, char *argv[1] is parameter2
Hai....
Can you give simple program for example...?? i still not understand....
Thank you..
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?