Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
3 basic questions about C programming?
I had a few basic questions about C.
1)Why and when is 'int main(int argc, char *argv[])' instead of 'int main'
2)When creating a function, when should you use 'void 'function name'' and what is the different between using int ''function name'? Basically I don't know why some functions are void and some are int
3)Could you explain 'return' to me. I've seen you can have return 0 or return 1 or return void, etc. What is the difference
3 Answers
- 7 years agoFavorite Answer
1.The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameter int main(void) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */} or equivalent (note 9); or in some other implementation-defined manner. Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.....
2. in programing language for creating function
syntax is: return type function name.
int and void is return type thats why when creating function we wright "int function name" or
"void function name"
if function does not return any value that time void return type is use...
if function return some value that time use int return time.....
3.return 0 for a successful completion and some other code for errors..
return1 means "file not found....
return void means does not return any value,
- ?Lv 78 years ago
1)Why and when is 'int main(int argc, char *argv[])' instead of 'int main'
you use int main(void){} when you do not need the command line arguments
use int main(int argc, char *argv[]){} when you need to use command line args
as in
c: myprog arg1 arg1
2)When creating a function, when should you use 'void 'function name'' and what is the different between using int ''function name'? Basically I don't know why some functions are void and some are int
You can also return a float or char or struct, char * , void * ....
The return value is what comes back to your programme when you call a function
as in
int add(int a, int b){
return a+b;
}
int c;
c=add(1,2); // c now has the return value from add
you use void when you do not need a return value from the function
as in
void prt(int n){
printf("%d\n",n);
return; // just a plain return with no value....
}
3)Could you explain 'return' to me. I've seen you can have return 0 or return 1 or return void, etc. What is the difference
return means return control to the calling function
you do not have return void;
it is either
return value;
or
return; // with no value specified...
When you return from main() you are returning to the operating system and telling it if all went well
(or not)
- 8 years ago
Glad to answer! As for number one, i don't think you need to but i know this only from c++, the difference between void and it is the variable type. Int is an integer so it will return an integer. Void doesn't return anything. Return basically means what the function returns so lets give an example. if i have a function called int func() and it contained only return 0; than i called this command in main: {output command} << func(); it would output the return value, 0. Hope this helped
Source(s): C++ For Dummies '09 Edition Personal Experience