Let's analyze your code step by step...
int main(int factorial,int n)
Function main
takes two parameters: factorial
of type int
, and n
of type int
, and returns an object of type int
. Error: main
is a special function, and the only valid signatures for it are int main()
and int main(int, char**)
, or any compatible type thereafter, such as int main(int, const char *const[])
.
{
Begin function main
.
n = 1;
Assign value 1
to variable n
of type int
.
printf("Enter a number to find it's factorial: ");
Call function printf
with argument "Enter a number to find it's factorial: "
of type const char*
. The variable-length argument list is empty. The function's return value (of type int
) is discarded.
scanf("%d", &factorial);
Call function scanf
with argument "%d"
of type const char*
. The variable-length argument list contains one argument: &factorial
, of type int*
. The function's return value (of type int
) is discarded.
while(factorial != 0) {
While factorial
is not equal to 0
, do.
n *= factorial;
Assign n
the result of multiplying n
by factorial
.
factorial--;
Decrement factorial
after the expression, and discard the expression's value (of type int
).
}
End while block.
//printf("%d",n);
Comment line. Has no effect on the program.
return ("%d",n); //Here
The expression ("%d", n)
uses the comma operator to evaluate the "%d"
expression, then the n
expression, and evaluating to the latter, effectively ignoring the former. Thus, this is equivalent to return n;
.
return n; //neither works
Since the previous return statement has already been ran by this moment, this is dead code that will never be executed. Anyway, it's equivalent to the previous return statement.
}
End function main
.
Now, with the obvious exception of the wrong main
signature, and the dead code at the end, you're completely right. And yes, if the return value of main
is zero, the program is said to have executed successfully. Otherwise, it executed unsuccessfully, and the purpose of the return value is to provide (broad) information about what went wrong.
I hope this has led a light on you!