0
votes

So for school I got this exercise where I need to make a program that calculates if a number is a prime number or not. This program should make use of parent and child processes, and strtoul should be used to convert the argv to a unsigned long.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>

void checkprime(unsigned long num);

int main(int argc, char *argv, char *env)
{       
strtoul(argv, NULL, 0);

pid_t pid = fork(); 

if(pid == 0)
{
    unsigned long number;

    printf("\nGive number to check: \n");
    scanf("%lu",&number);

    checkprime(number);
}
else if(pid < 0)
{
    perror("Fork Failed!");
}
else
{
    int status = -1, result;
    waitpid(pid, &status, 0);
    result = WEXITSTATUS(status);

    if(result == 1)
    {       
        printf("this is a prime number\n");
    }
    else if(status < 0)
    {
        perror("Something Failed");
    }
    else
    { 
        printf("this is not a prime number\n");
    }
}
return 0;
}

void checkprime(unsigned long num)
{
int i;
for(i = 2; i < num; i++)
{
    if(num % i == 0)
    {
        exit(0);
    }
}
exit(1);
}

So when I try to compile this it says: line 13: identifier not expected. Error code 1.

The code on line 13 says: pid_t pid = fork();

Now my question is: Why do i get that error?

Its fixed, thanks everyone for the help. I appreciate it.

3
What is the purpose of pid_t = pid = fork();? It's simply not valid C syntax. - Some programmer dude
What's worse, you fixed the error in the code you show here! You should always copy-paste the code, so you don't introduce other errors, or like in this case fix the error. Now this question is worthless since there is no problem. - Some programmer dude
im sorry i meant : pid_t pid = fork(); - Jonas
Check out man7.org/linux/man-pages/man3/strtol.3.html and move your call to strtol(...) below the declaration of your local variables. - cleblanc
Also, that should teach you to copy-paste the error as well. In full, complete, without modifications and as text. - Some programmer dude

3 Answers

2
votes

Some old versions of compilers accept C89 (not C99 or C11) as the default C dialect. You want C99 (since you have a declaration after a statement) at least.

If you use some old version of GCC on some old Linux distribution, try compiling with gcc -std=gnu99 -Wall -Wextra -g where -std=gnu99 sets the C dialect (you could also use -std=c99 or better), -Wall -Wextra asks for warnings, -g wants debug information (in DWARF, for the gdb debugger). Better yet, upgrade your GCC to a recent version (like GCC 7 in November 2017) whose default is C11 with GNU extensions.

Or put the declaration

pid_t pid= 0;

(I prefer to systematically initialize variables in C)

before the statements

 strtoul(argv, NULL, 0); //useless call, you need to store the result
 pid= fork(); 
0
votes

The compiler is telling you it didn't expect to encounter an identifier pid_t at that line since the previous line wasn't an identifier. You need to declare your variables at the top of the scope, and put other code below that. You could either do this

int main(int argc, char *argv, char *env)
{       
    long int val=strtoul(argv, NULL, 0);
    pid_t pid = fork(); 
    ...

or

int main(int argc, char *argv, char *env)
{       
    pid_t pid = fork(); 
    strtoul(argv, NULL, 0);
    ...

But you're missing the value returned from strtol

0
votes

You can use make shorter prime finder loop by changing, i < num to i < num/2.

This is one of standards for main() arguments:

(int argc, char **argv)

Turn on gcc flag: -Wall to see all warnings in code.

As @cleblanc said in one of answer, store return value of strtoul() to a varibale long int val = strtoul(argv[1], NULL, 0);.

we pass number for check, from command line arguments, argv[1] refers to number when we run program with

./prime 15

Code

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

void checkprime(unsigned long num);

int main(int argc, char **argv)
{ 

    long int val = strtoul(argv[1], NULL, 0);

    pid_t pid = fork(); 

    if(pid == 0){   
        checkprime(val);
    }
    else if(pid < 0){
        perror("Fork Failed!");
    }
    else{
        int status = -1, result;
        waitpid(pid, &status, 0);
        result = WEXITSTATUS(status);
        if(result == 1){       
            printf("Prime\n");
        }
        else if(status < 0){
            perror("Something Failed");
        }
        else{ 
            printf("Not prime\n");
        }
    }
    return 0;
}

void checkprime(unsigned long num)
{
    int i;
    for(i = 2; i < num/2; i++){
        if(num % i == 0){
            exit(0);
        }
    }
    exit(1);
}

Compile, Run

gcc -Wall prime.c -o prime

./prime 15