Peter Louis Peter Louis -4 years ago 82
C Question

compiling my simple C code

Whenever I try to compile the following code, I can enter the initial value I ask for but then nothing happens. Also, there is no build error that being picked up so i don't know why this is happening. Please help and thank you in advance.

#include <stdio.h>
#include <stdbool.h>
int n;
int allDaPrimes[1000];
int counter = 0;
bool isPrime(int number);
int i;

int main()
printf("Please enter a numeric value now: ");
scanf("%d", &n);
for (i = 2; i <n; i++){
if(isPrime(i)) {
allDaPrimes[counter] = i;

for(i= 0; i==counter; i++){
printf("%d", allDaPrimes[i]);
return 0;


bool isPrime(int number) {
for (i= 2; i <= number; i++){
if(number % i == 0 && number != i){
return false;

return true;

Answer Source

All your variables are declared globally rather than locally in the functions where they are used. This means that you're getting conflicts between the different functions.

Take i for example. In the loop in main, you start off with it set to 2. But isPrime() changes it to be n+1 because that is the last value it'll be after the loop in there finishes. But you call that twice so i ends up being n+2, so the main loop only runs once. And if you end up with any values in the array, it'll be n+2 if n+1 was a prime.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download