You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:判断质数的C语言函数程序输入后无输出问题排查

Hey there! Let's break down why your program isn't working as expected and fix it step by step.

Key Issues in Your Code


1. You never called the check_prime function in main

This is the biggest reason your program does nothing after input. In your main function, you only wrote int check_prime(int n); (a redundant declaration, since the function is already defined above), but you never actually run check_prime(n) to execute the prime-check logic. The program just reads the input and exits immediately.

2. Incorrect loop condition in check_prime

Your loop uses i < n/2, which misses edge cases. For example, if n=4, n/2=2, so the loop condition i < 2 never runs. The code will incorrectly mark 4 as a prime because count stays 0. The correct range should be i <= n/2 (or even better, i*i <= n for efficiency, since factors larger than the square root of n will have corresponding factors smaller than the square root).

3. Unhandled edge cases for numbers ≤1

Prime numbers are defined as natural numbers greater than 1. Your code will incorrectly label 0, 1, or negative numbers as prime since count remains 0 for these values.

4. Function type mismatch

You declared check_prime as an int function but never return a value. This causes undefined behavior in C—either change it to a void function (since you're printing directly inside it) or have it return a boolean-like value (1 for prime, 0 for non-prime) and handle printing in main.

Fixed Code Example


Here's a revised version that addresses all these issues, with optimizations for efficiency:

#include <stdio.h>

// Function to check if a number is prime, returns 1 for prime, 0 otherwise
int check_prime(int n) {
    // Handle numbers ≤1 immediately
    if (n <= 1) {
        return 0;
    }
    // 2 is the only even prime number
    if (n == 2) {
        return 1;
    }
    // Even numbers >2 can't be prime
    if (n % 2 == 0) {
        return 0;
    }
    // Check only odd divisors up to sqrt(n)
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0) {
            return 0; // Found a divisor, not prime
        }
    }
    return 1; // No divisors found, prime
}

int main() {
    int n;
    printf("enter the number:\n");
    scanf("%d", &n);
    
    // Call the check function and print the result
    int is_prime = check_prime(n);
    if (is_prime) {
        printf("the number is prime\n");
    } else {
        printf("the number is not prime\n");
    }
    return 0;
}

What Changed?

  • Added a proper call to check_prime(n) in main to trigger the logic after input.
  • Added checks for edge cases (≤1, 2, even numbers) to avoid incorrect results.
  • Optimized the loop to only check odd divisors up to the square root of n, making the code faster for large numbers.
  • Restructured the function to return a status code instead of printing directly, which follows better code organization (separating logic and output).

内容的提问来源于stack exchange,提问作者Rojet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:17:55