Search code examples
cfunctioncall

Function not getting called from the main function


I am seriously worried that I don't understand the very fundamentals of C programming. I am reading numbers from a text file, and I am going to check if they are prime numbers or not. However, the problem is that the IsPrime function is never being called from the main function. I have tried making the number variable a global and a local variable, but it does not work. Is my basic understand that bad?

Reading from the text file is no problem. It looks like this:

73771782    81296771    79982326    75332246    10128193
81643413    76259734    94432076    50063976    91748657
42311916    -1920042    90747362    53851612    43498487
73193311    96685173    39019033    8630045     17

Because the main function never calls the IsPrime function, the consequence is that all numbers are printed out as is a prime number.

Here is the code:

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

int isPrime(int number);

int main(int argc, char **argv) {
    int number = 0;
    int number2 = 0;
    FILE *fp = NULL;
    
    char file_name[] = "C:\\Users\\Erlen\\Desktop\\numbers.txt";
    fp = fopen(file_name, "r"); 
    
    if (fp == NULL) {
        printf("Error opening the file: \n");
        exit(0);
    }
    
    fscanf(fp, "%d", &number);
    
    while (number != number2) {
        if (isPrime(&number) == 1) {
            printf("Number: %d is a prime number \n", number);
        } else {
            printf("The number %d is a prime number \n", number);
        }
        number2 = number;
        fscanf(fp, "%d", &number);
    }
    printf("\n");
    
    return 0;
}

int isPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0) {
            return 0;
        }
    }
    return 1;
}

Solution

  • There are multiple problems in your code:

    • you pass the address of number instead of its value to isPrime.
    • you should iterate while (fscanf(fp, "%d", &number) == 1) to handle all numbers in the file. Your current loop logic is cumbersome and would fail if the first number if 0 or if the file contains identical consecutive numbers.
    • it is idiomatic in C to only test if the return value of isPrime is non zero.
    • the message for composite numbers is incorrect.
    • the convention for the exit status is non zero for failure.
    • you should take the absolute value of negative numbers.
    • you should return 0 for 0 and 1 which are not considered prime numbers.
    • you can improve the performance of isPrime for large numbers by testing if i * i <= number instead of i < number, reducing the complexity to O(sqrt(N)).

    Here is a modified version:

    #include <stdio.h>
    
    int isPrime(int number);
    
    int main(int argc, char *argv[]) {
        int number;
        FILE *fp;
        char file_name[] = "C:\\Users\\Erlen\\Desktop\\numbers.txt";
    
        fp = fopen(file_name, "r"); 
        if (fp == NULL) {
            printf("Error opening the file: \n");
            return 1;
        }
        
        while (fscanf(fp, "%d", &number) == 1) {
            if (isPrime(number)) {
                printf("Number %d is a prime number\n", number);
            } else {
                printf("Number %d is a not a prime number\n", number);
            }
        }
        return 0;
    }
    
    int isPrime(int number) {
        if (number < 0)
            number = -number;
        if (number < 2)
            return 0;
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                return 0;
            }
        }
        return 1;
    }