1. ## Another beginning function

Hi:

Ran into another problem in studying my programing. This program is designed to compute a number of primes. Instead of computing any primes, however, it throws a segmentation block. I suspect that it may because I am returning 1 and 0 instead of true and false, but I did that because when I return true and false I get these errors.

ThousandthPrime.c:10:10: error: ‘false’ undeclared (first use in this function)
ThousandthPrime.c:10:10: note: each undeclared identifier is reported only once for each function it appears in
ThousandthPrime.c:20:9: error: ‘true’ undeclared (first use in this functio

Code:
```//ThousandthPrime:  Calculate the thousandthprime.
#include <stdio.h>
//primality function which will determine if a number is prime.
long Primality(long n)
{
//check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2
if(n % 0 && n != 2)
{
return 0;  //if the number is even or equal to 2, return false and do not count these numbers
}//end if
//once we determine that a number is not even or equal to 2, we can check the odds.
for(long i = 3; i * i <= n; i += 2)
{
if(n % i == 0)
{
return 0;
}//end if
}//end for
return 1;
}//end function primality

int main()
{
long primes = 0;// counts the number of primes for us.
long n = 1; //variable that we will run through primality function
long y = 0; //variable input by the user to tell how many primes to find.

printf("Please enter a number(n), and the program will return with the \"nth\" number of primes");
scanf("%ld", y);

//while loop calls the primality functions until "y"  primes are found
while(primes < y)
{
n++;
Primality(n);
if(Primality(n))
{
printf("%ld", n);
primes++;
}//end if
}//end while

printf("I found %ld primes %ld is the \"nth\" prime", primes, n);

}//end main```
Code:
```Please enter a number(n), and the program will return with the "nth" number of primes:  3
Segmentation fault (core dumped)```
What am I doing wrong?

2. You may want to try to increase your compiler warning level and then fix these warnings.
||=== c_homework, Debug ===|
main.c|4|warning: no previous declaration for ‘Primality’ [-Wmissing-declarations]|
main.c||In function ‘Primality’:|
main.c|7|warning: division by zero [-Wdiv-by-zero]|
main.c||In function ‘main’:|
main.c|31|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|
||=== Build finished: 0 errors, 3 warnings ===|
Jim

3. Originally Posted by jimblumberg
You may want to try to increase your compiler warning level and then fix these warnings.

Jim

And how would I do these things?

4. Also, I don't get:

main.c|31|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|

5. scanf() requires a memory location (address) to store the value of what it reads/converts. When you use the name of a variable that is an integer or float type, the name is evaluated for the value it holds. So you are passing y to scanf which is 0 and scanf expects an a valid address. So, you need to use the address operator '&' when a function expects an address, namely &y.

Code:
`if( n % 0 ... )`
Mod is derived from doing division and you cannot of course divide by zero.

6. But that statement says
Code:
`if (n % i == 0)`

7. How can I return true/false without getting the error?

8. Look at line 7.

edit:

You are not getting the error from the return value, however, you should probably define your function so that it returns 'int' instead of 'long'. If you insist on long, then you should append your constants with 'L', because constant integers are by default of type 'int'. I would change the return value to int though.

9. Line 8 on my editor, but I mistyped the first function. Thanks.

Code:
```ThousandthPrime.c: In function ‘Primality’:
ThousandthPrime.c:10:10: error: ‘false’ undeclared (first use in this function)
ThousandthPrime.c:10:10: note: each undeclared identifier is reported only once for each function it appears in
ThousandthPrime.c:17:11: error: ‘true’ undeclared (first use in this function)
ThousandthPrime.c: In function ‘main’:
ThousandthPrime.c:30:2: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]
ThousandthPrime.c:33:15: warning: comparison between pointer and integer [enabled by default]```

10. It's perfectly fine to return 0 or 1 if your compiler doesn't support bool.
That just leaves you with looking up how to use scanf properly. I'm sure there are millions of examples on the internet.

Once you've done that, post what should hopefully be your working code.

11. Originally Posted by jwall
How can I return true/false without getting the error?
To use true and false, you need to declare them with

Code:
`#include <stdbool.h>`

12. I suspect the OP is confused because the strings true and false are within single-line comments, not within code (statements or declarations).

The cause of the error therefore depends on the age of the compiler.

Older C compilers do not support single line (starting with // ) style comments, and will interpret the comments as code. The solution to that is to use the traditional multi-line comment (starting with /* and ending with */ ).

A rarer problem is that the editor (or IDE) being used to create the source files has embedded a newline or other character within the comment (presumably before the word "false" occurs) which causes the compiler to interpret subsequent text as code. The solution in that case is to remove the embedded character. The techniques to view such an embedded character and to remove it depend on the editor though.

The scanf() call on line 31 also needs to be changed to "scanf("%ld", &y)". Adding that ampersand (which I have highlighted in red) will address the segmentation fault.

13. Originally Posted by grumpy
Older C compilers do not support single line (starting with // ) style comments, and will interpret the comments as code.
Hmm... but in that case, even line 1 should contain an error.

14. Revised code
Code:
```//ThousandthPrime:  Calculate the thousandthprime.
#include <stdio.h>
#include <stdbool.h>

//primality function which will determine if a number is prime.
long Primality(long n)
{
//check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2
if(n % 2 == 0 && n != 2)
{
return false;  //if the number is even or equal to 2, return false and do not count these numbers
}//end if
//once we determine that a number is not even or equal to 2, we can check the odds.
for(long i = 3; i * i <= n; i += 2)
{
if(n % i == 0)
{
return true;
}//end if
}//end for
return false;
}//end function primality

int main()
{
long primes = 0;// counts the number of primes for us.
long n = 1; //variable that we will run through primality function
long y = 0; //variable input by the user to tell how many primes to find.

printf("Please enter a number(n), and the program will return with the \"nth\" number of primes:  ");
scanf("%ld", y);

//while loop calls the primality functions until "y"  primes are found
while(primes < &y)
{
n++;
Primality(n);
if(Primality(n))
{
printf("%ld", n);
primes++;
}//end if
}//end while

printf("I found %ld primes %ld is the \"nth\" prime", primes, n);

}//end main```
Warnings I get--how do I increase my compiler warning level? I am using gcc on linux if it matters
Code:
```ThousandthPrime.c: In function ‘main’:ThousandthPrime.c:32:2: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]
ThousandthPrime.c:35:15: warning: comparison between pointer and integer [enabled by default]```
This is what happens when I run my code.

Code:
```Please enter a number(n), and the program will return with the "nth" number of primes:  9
Segmentation fault (core dumped)```

15. You need to study the documentation for scanf(), and realize scanf() requires the address of the variable. Read thru the previous posts, several tell you how to fix this issue. This is probably one of the causes of your segmentation fault. Although I show one more error:
||=== c_homework, Debug ===|
main.c|7|warning: no previous declaration for ‘Primality’ [-Wmissing-declarations]|
main.c||In function ‘main’:|
main.c|34|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|
main.c|38|error: comparison between pointer and integer|
||=== Build finished: 1 errors, 2 warnings ===|

Warnings I get--how do I increase my compiler warning level? I am using gcc on linux if it matters
How are you compiling your code now? Are you using an IDE or are you compiling from the command line? If you compile from the command line show your compile line. Also this link may be useful in finding the correct command line compile options: gcc Warning options. If you look at the error/warning messages I posted you can some of the types of errors/warning switches I used, -Wformat, -Wmissing-declarations.

Jim