文件使用打印 - Isprime_function.c如何在c中的if函數中使用main函數中的函數返回值。我不想在我的Isprime_function文件
#include <stdio.h>
#include "all_header.h"
int isprime(int input)
{
int i, stop = 0;
if(input >= 1)
{
for(i=2; i<=input/2; i++)
{
if(input%i==0)
{
stop = 1;
break;
}
}
if(stop==0 && input!=1)
{
printf("%d is a prime number\n",input);
}
else
{
printf("%d is not a prime number\n",input);
}
}
else if(input == 1)
{
printf("%d is not prime by definition!\n", input);
}
else if(input == 0)
{
printf("%d is not a valid number.\n", input);
}
else
{
printf("%d Please use positive nonzero integers! Try again!\n", input);
}
return 0;
}
文件 - Isprime_main.c
#include <stdio.h>
#include "all_header.h"
int main()
{
int input;
printf("\nPlease Enter a Positive Integer: ");
scanf("%d", &input);
isprime(input);
return 0;
}
文件 - All_header.h
#ifndef PRESENT_VALUE_FUNCTIONS
#define PRESENT_VALUE_FUNCTIONS
int isprime(int input);//check whether number is prime or not.
#endif
我用以下命令
gcc -Wall -o isprime isprime_main.c isprime_function.c all_header.h
我是編程新手我有一個小小的疑問。 此功能正常工作,但在我的isprime_fuction.c文件我使用「printf」我不想在函數中使用我想返回特定的值和控制行爲從主函數我該如何做,因爲我想用這個函數來獲取數字的主要因素。
您的返回值賦值給一個變量,並使用它像任何其他變量。 –
如果你只是想讓函數返回一個布爾值「是/否」的值,並且沒有打印,那麼在你調用該函數之前進行輸入驗證。 –