![]() |
|
A Question About C - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: A Question About C (/Thread-A-Question-About-C) Pages:
1
2
|
A Question About C - CPU - 10-15-2015 I am learning the C programming language, and have a question to ask experienced C programmers on this forum: Does putting "void" in a function declaration's parameter list have a difference between an empty parameter list? Example: Code: my_function(void);Versus Code: my_function();Do these prototypes have any difference in any way at all? Also, I was reading on how the "main" function's return type could be "void". Is it true that the main function's return type can be void? If so, why, and is it different from the integer return type? Thank-you. RE: A Question About C - pizzaguy541 - 10-16-2015 (10-15-2015, 04:26 AM)CPU Wrote: I am learning the C programming language, and have a question to ask experienced C programmers on this forum: Does putting "void" in a function declaration's parameter list have a difference between an empty parameter list?There is no difference between the two. Quote: Also, I was reading on how the "main" function's return type could be "void". Is it true that the main function's return type can be void? If so, why, and is it different from the integer return type? Thank-you.Yes, the main function can have a return type of void. Declaring an int was used to return a status value back to the DOS batch file that called the executable. For example, this could be the code for SomeFile.exe: Code: int main()
{
int x = 5;
if x == 5 return 1;
}In your batch file you could have something like this: Code: SomeFile.exe
IF %ERRORLEVEL% NEQ 0
(
ECHO error - SomeFile.exe returned an error code!!!
)RE: A Question About C - CPU - 10-17-2015 (10-16-2015, 10:40 AM)pizzaguy541 Wrote: There is no difference between the two.Yes, the main function can have a return type of void. Declaring an int was used to return a status value back to the DOS batch file that called the executable. Thank-you for your answer. So, if you declare the main method as a void, it works exactly the same except you cannot return any exit code? RE: A Question About C - lord of the flies - 10-17-2015 (10-17-2015, 12:44 AM)CPU Wrote: Thank-you for your answer. So, if you declare the main method as a void, it works exactly the same except you cannot return any exit code? If you give something a "void" return type, that means it shouldn't be returning any result values. If you give something an "int" return type it will return an integer result value. RE: A Question About C - CPU - 10-17-2015 (10-17-2015, 03:01 AM)lord of the flies Wrote: If you give something a "void" return type, that means it shouldn't be returning any result values. If you give something an "int" return type it will return an integer result value. I know how return types work, I'm just wondering with how main works. RE: A Question About C - 0xDEAD10CC - 10-17-2015 (10-16-2015, 10:40 AM)pizzaguy541 Wrote: There is no difference between the two.Yes, the main function can have a return type of void. Declaring an int was used to return a status value back to the DOS batch file that called the executable. This has nothing to do with return types so I have no idea what you're talking about. function() vs function(void) does make a difference though as the latter means that no arguments can be passed to it. Either way you should never omit the return type from any function, and you should never declare the main() function as returning void. RE: A Question About C - pizzaguy541 - 10-17-2015 (10-17-2015, 05:49 AM)0xDEAD10CC Wrote: This has nothing to do with return types so I have no idea what you're talking about. function() vs function(void) does make a difference though as the latter means that no arguments can be passed to it.He was actually asking two questions. The first question was asking about the difference between using "void" or nothing for the functions argument list. In my experience, I've never seen any code function written using a blank argument list that would have values being sent to it. However, I did just read this over at stackoverflow: In C, in general, (void) means no arguments required in function call, while () means unspecified number of arguments. I've always taken it for granted that they both, (void) and (), meant the same and have used either one when writing code. Quote:Either way you should never omit the return type from any function, and you should never declare the main() function as returning void.I've never seen or written any function definition without including the return type. I'll use void when I'm not expecting the function to return anything. As for using void as a return type for the main function, my compiler had never complained about the usage and I've never had a program crash when it exited. Perhaps using the int return type would be required for a windows program, but I've never had to use that for any of my DOS programs. RE: A Question About C - CPU - 10-17-2015 (10-17-2015, 05:49 AM)0xDEAD10CC Wrote: This has nothing to do with return types so I have no idea what you're talking about. function() vs function(void) does make a difference though as the latter means that no arguments can be passed to it. Thank-you. Anyways, omitting the return type is completely opinion-based. For me, personally, I like to omit the return type whenever I'm returning an integer. In my opinion, I agree with you to never make the "main" function void, but do not agree with you on never omitting the return type. RE: A Question About C - 0xDEAD10CC - 10-18-2015 It's not opinion based. The standard does not require that a compiler implicitly add a proper return type for you, even though some compilers will do it regardless of what the standard says, but then you are relying on implementation specific behavior to define what really happens which is horrible. You should never be relying on anything that is non-standard. There's nothing opinion based about properness. In C99 the mention about not having a return type specifier is removed, which would make this undefined behavior... Of which, could have easily been avoided if you did the appropriate thing and explicitly write 'int' before the main() function signature. This way you aren't relying on an old C89 dialect to be consistent with all newer standards of the language. ALWAYS explicitly specify the return type. (10-17-2015, 12:44 AM)CPU Wrote: Thank-you for your answer. So, if you declare the main method as a void, it works exactly the same except you cannot return any exit code? False, you can, but a return type of void means that anything which calls the function won't care what was placed into the eax register. This doesn't mean you can't populate the data which that register holds yourself, nor does it mean that you can't obtain the value after a function call. RE: A Question About C - CPU - 10-18-2015 (10-18-2015, 04:54 AM)0xDEAD10CC Wrote: It's not opinion based. The standard does not require that a compiler implicitly add a proper return type for you, even though some compilers will do it regardless of what the standard says, but then you are relying on implementation specific behavior to define what really happens which is horrible. You should never be relying on anything that is non-standard. There's nothing opinion based about properness. In C99 the mention about not having a return type specifier is removed, which would make this undefined behavior... Of which, could have easily been avoided if you did the appropriate thing and explicitly write 'int' before the main() function signature. This way you aren't relying on an old C89 dialect to be consistent with all newer standards of the language. My C compiler makes a function's return type default to "int", and I'll always be using that compiler, but you do somewhat have a point. |