1. ## Prime Checking Algorithm

I'm still new to C. I wrote some code, but I'm sure I did something wrong because it's not giving the correct output.

(compiling and running on linux)

Code:
```void main()
{
unsigned long startingNumber; // THE Number. Is it prime?

printf("Enter Positive Number (0 to 4,294,967,295) to be Tested : ");
scanf(startingNumber);
printf("\nTesting ", startingNumber, "...\n");

unsigned long testNumber = (startingNumber / 2);
// A number can't be evenly divided by a number bigger than half the first number.

unsigned long answer = startingNumber % testNumber; // Get the initial remainder.

while (answer != 0 && testNumber > 1) // Decrement while checking for a modulus of 0.
{
testNumber = testNumber - 1;
}

if ((answer == 0) && ((startingNumber / 2) > 1) || (startingNumber == 2))
{
// Check for prime, with fixes for 2, and testNumbers 1 or smaller.
printf(startingNumber, " is NOT a PRIME Number\n");
}
else
printf(startingNumber, " is a PRIME Number\n");
return 0;
};```

Output:

Code:
```fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c\$ ./a.out
Enter Positive Number (0 to 4,294,967,295) to be Tested : 265952

Testing Hfῼﾛ�fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c\$```
Using GDB, the program exits with code 011.
Can someone help me fix my code?

2. I'm surprised that even compiles.

3. Well thanks, that helps SO much.
What mistakes did I make?
(other than forgetting to convert startingNumber with atoi)

4. Well, I was going to point out a few things, and then I realized the list is kind of big..... Since you insist.... I'll even be somewhat "verbose" about it.

• "void main()" should be "int main(void)".
• After printf(), the text might not show up because you didn't include a '\n', which is fine, but you might want to do a "fflush(stdout);"
• "scanf(startingNumber);" is totally wrong. Consult the man pages or google for info on scanf().
• "printf("\nTesting ", startingNumber, "...\n");" <------- again, totally wrong. Consult the man pages or google for info on printf().
• You are declaring variables in between your code. This is allowed in C++ and C99, but not in C89/C90.
• You are using // as comments, which are generally accepted in C, but are actually not really a part of C89/C90, but are in C++ and C99.
• Be careful with your conditions... Putting parenthesis around conditions help to make the code more readable and protects you from operator precedence that you might not expect.
• You somehow managed to declare main() as void, but return an int anyway. That should have been picked up imo. Returning the int is good, but declare main() as returning it!

Not all of these are actual error messages per se, but just also good programming tips.

5. I tried to follow that, but it's still not working.

New code:
Code:
```int main(void)
{
unsigned long startingNumber; // THE Number. Is it prime?
unsigned long testNumber;

printf("Enter Positive Number (0 to 4,294,967,295) to be Tested : \n");
scanf("%i", &startingNumber);
printf("Testing \n", startingNumber);

testNumber = (startingNumber / 2);
// A number can't be evenly divided by a number bigger than half the first number.

answer = startingNumber % testNumber; // Get the initial remainder.

while (answer != 0 && testNumber > 1) // Decrement while checking for a modulus of 0.
{
testNumber = testNumber - 1;
};

if ((answer == 0) && ((startingNumber / 2) > 1) || (startingNumber == 2))
{
// Check for prime, with fixes for 2, and testNumbers 1 or smaller.
printf(startingNumber, " is NOT a PRIME Number\n");
};
else
printf(startingNumber, " is a PRIME Number\n");
return 0;
};```
I still get several compiler warnings:
Code:
```primechk.c: In function ‘main’:
primechk.c:9: warning: incompatible implicit declaration of built-in function ‘printf’
primechk.c:10: warning: incompatible implicit declaration of built-in function ‘scanf’
primechk.c:10: warning: passing argument 1 of ‘scanf’ makes pointer from integer without a cast
primechk.c:27: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
primechk.c:30: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
primechk.c:31: warning: ‘return’ with a value, in function returning void
primechk.c:6: warning: return type of ‘main’ is not ‘int’```

6. Code:
```unsigned long startingNumber;
scanf("&#37;i", &startingNumber);```
unsigned longs should be read with the format specifier %lu.
Code:
`printf("Testing \n", startingNumber);`
There's no format specifier for startingNumber. That should be
Code:
`printf("Testing %lu\n", startingNumber);`
This
Code:
`printf(startingNumber, " is NOT a PRIME Number\n");`
should, again, be
Code:
`printf("%lu is NOT a PRIME Number\n", startingNumber);`
Semicolons are unnessesary after (most) closing braces: '}'.

primechk.c:31: warning: ‘return’ with a value, in function returning void
primechk.c:6: warning: return type of ‘main’ is not ‘int’
Apparently your main function is still returning void. Try saving your source file. [/edit]

7. BTW, don't put a ; at the end of a function definition.

8. I said that. At the ends of ifs and whiles too. At any closing brace ('}') except for structures and initializer lists.

Well, actually, there's nothing wrong with doing so. It's just spurious and annoying for other programmers to read -- they think they're looking at the end of a struct or something.

9. LOL, I read where you said that about blocks, and didn't realize you were talking about main(). Thought you were just referrring to ones around any given if or while statement or something.

10. It's cool that fuzzypig also uses ubuntu !
It's rather ideal for gcc.

I've written prime programs before but I really better not post them.

11. Methinks someone needs to use the -Werror flag and stop ignoring warnings!

12. Originally Posted by honorable_sir
It's cool that fuzzypig also uses ubuntu !
It's rather ideal for gcc.
Strange thing to say. I'd say ANY Linux, or for that matter any platform where gcc works, is "ideal for gcc"

13. You can stop checking whether the number is prime or not after testing up to, and including, the square root of the number. If it's still prime at that point, then it will stay prime all the way.

Big speed up if you're trying to find lots of prime numbers.