Samuel
Samuel

Reputation: 23

expected identifier or '(' in C

I have recently started following the CS50 course on Edx. I'm stuck on the second problem set as when I try to compile my code, this message comes up:

expected identifier or '(' in C

If you need my code, here it is:

#include <stdio.h>
#include <cs50.h>

int main (int argc, string argv[])

int key;

{

    if (argc != 2)
    {
        printf("error\n");
    }
    else
    {
        key = atoi(argv[1]);

        if (key == 0 || key < 0)
        {
            printf("error");
        }
        else
        {
            printf("%i\n", key);
        }
    }
}

If there is anything else wrong with my code, could you please hint it for me?

Upvotes: 0

Views: 1403

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522254

This is what your code should look like:

int main (int argc, string argv[])
{            // <-- every function definition begins with {
    int key;

    if (argc != 2)
    {
        printf("error\n");
    }
    else
    {
        key = atoi(argv[1]);

        if (key == 0 || key < 0)
        {
            printf("error");
        }
        else
        {
            printf("%i\n", key);
        }
    }
}

Your question is a simple typigraphical error, and hence it should probably be closed. I posted the full code because I could not show this in a single comment.

Upvotes: 0

Peter K
Peter K

Reputation: 1382

Move the declaration int key inside the brackets of main.

Then change the string argv[] argument of main to char* argv[]. string argv[] is simply not correct for a main signature.

You also need to include stdio.h and stdlib.h.

Your code would look like this:

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

int main (int argc, char* argv[])
{
    int key;

    if (argc != 2)
    {
        printf("error\n");
    }
    else
    {
        key = atoi(argv[1]);

        if (key == 0 || key < 0)
        {
            printf("error");
        }
        else
        {
            printf("%i\n", key);
        }
    }
}

Verified with Coliru

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44306

int main (int argc, string argv[])
{
  int key;

needs to be inside the bracket

Upvotes: 3

Related Questions