Home > Enterprise >  Why there is no return value?
Why there is no return value?

Time:10-28

This is my code

#include <stdio.h>

int isPrime(int n) 
{
    if (n == 1 || n == 0) { return 0; }
    for (int i=2; i <= n/2; i  ) {
        if (n%i == 0) {
            return 0;
        }
    }
    return 1;
}

int main()
{
    int n = 2;
    isPrime(n);
    return 0;
}

I try to run my program, but there is none of return value. Why and how can i fix this?

CodePudding user response:

C is not a REPL. It will only print what you explicitly tell it to print:

printf("%d\n", isPrime(n));

In the context of main() you could also return1 that and check in the shell:

return isPrime(n);

Where then you can do:

./isprime && echo "Is prime!"

1 Keep in mind this works only for int return values, and that 0 means success or in C parlance: "No error".

  • Related