Getting the error "expected identifier or '(' before '{' token in C [closed]

房东的猫 提交于 2019-12-10 12:24:02

问题


Any help would be great!

#include <stdio.h>
#define pi 3.14159



int main()
{
  float r;
  char PI;

/*Program for circumference. */

    printf(" This is a program that will calculate circumference.\n");
    printf("Please put in your radius.\n");

    scanf("%f", &r);
    printf("Please input PI\n");
    PI = getchar();
 }

   {
    if {(char != PI || 3.14);
    printf("Incorect value\n");
}

    else {
    printf("Thank you, the circumference is now.\n");
    printf("%f", (r) * pi *2);
}


return 0;

}

I'm trying to figure out this error, I have definitely searched around, but nothing really has popped up with my specific problem. If it helps, it is right before the "if" statement begins. Might I be using one too many '{'?


回答1:


I have pointed out the errors with comments.Make it a habit to use caps for the #define macro, not for the variables.And finally, the condition for your if should be if(PI!=pi).Remove the { between if and the (, and also the ; after the )

#include <stdio.h>
#define pi 3.14159



int main()
{
  float r;
  char PI;

/*Program for circumference. */

    printf(" This is a program that will calculate circumference.\n");
    printf("Please put in your radius.\n");

    scanf("%f", &r);
    printf("Please input PI\n");
    PI = getchar();
 } //This is the source of error as `main()` ends after this `}'

   {
    if(PI!=pi)  //You have used a `;` after if's condition & an extra '{' before it
    printf("Incorect value\n");
}

    else {
    printf("Thank you, the circumference is now.\n");
    printf("%f", (r) * pi *2);
}


return 0;

}



回答2:


You are terminating your main function at the first } character. You have an uneven match of open and close braces and that is causing the problem. You also have a problem with your if statement. This

if {(char != PI || 3.14);

should read as

if (char != PI || 3.14)

or more specifically the entire if-else should be

if (char != PI || 3.14)
{
   printf("Incorect value\n");
} else {
   printf("Thank you, the circumference is now.\n");
   printf("%f", (r) * pi *2);
}


来源:https://stackoverflow.com/questions/16257109/getting-the-error-expected-identifier-or-before-token-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!