-2

Can I return a variable in main function? like in

#include <stdio.h>
#include <stdlib.h>

int main(int factorial,int n)
{
    n=1;
    printf("Enter a number to find it's factorial: ");
    scanf("%d",&factorial);
    while(factorial!=0){
        n*=factorial;
        factorial--;
    }
    //printf("%d",n);
    return ("%d",n);   //Here
    return n;          //neither works
} 

It doesn't return the variable n. Is the return 0; statement only for telling that the program worked fine?

Dair
  • 15,910
  • 9
  • 62
  • 107
Ahmad
  • 21
  • 8
  • look here: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – HDJEMAI Nov 06 '15 at 06:10
  • it doesn't have my answer. – Ahmad Nov 06 '15 at 06:16
  • Your last assumption is pretty close: it's telling the environment whether the program finished ok (return 0 usually) or with an error (return non-0 value). Otherwise, where should main() return to anyway? –  Nov 06 '15 at 06:16
  • 1
    How does the linked question not have your answer? The accepted answer starts with "The return value for main should indicate how the program exited.". That is pretty much the answer you're looking for. –  Nov 06 '15 at 06:17
  • 1
    If you want to tell the environment the result of your calculation instead (which is what you seem to be asking), just print the value. –  Nov 06 '15 at 06:19

2 Answers2

2

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!

3442
  • 8,248
  • 2
  • 19
  • 41
1

The return value for main should indicate how the program exited. Normal exit is generally represented by a 0 return value from main. Abnormal termination is usually signaled by a non-zero return but there is no standard for how non-zero codes are interpreted.

Is the return 0; statement only for telling that the program worked fine?

Yes, the return value of main function only shows how program is exited, no other use with this value as you do with return value of non main functions like return a value for other uses.

The returned value is interpreted by environment (like DOS or other operating systems) from which the program is running for it's internal tasks. The program can't use that value.

K.H.A.J.A.S
  • 514
  • 4
  • 19