expected identifier or ‘(‘ before ‘while’gcc, Error to Add Identifier before a While

huangapple go评论58阅读模式
英文:

expected identifier or '(' before 'while'gcc, Error to Add Identifier before a While

问题

  1. 我是编程新手,有3个或更多错误:

  2. 不知道为什么 int main (Void) 显示为 (main 函数的重定义错误)。

  3. 在这个函数下方的 (While 循环) 显示为 (while 前面应该是预期的标识符或 '(') 错误,请帮忙。

点击此处查看图像描述

#include <stdio.h>

int main ()
{
    printf("Hello World");
    return 0;
};


/// @brief 
/// @param  
/// @return 
int main(void)
{
    // 获取起始值。
    int startingLambas = get_int("sLlambas");

    // 获取结束目标 llambas 值。
    int endingLambas = get_int("endingLlambs");

    // 打印所需的年份。

    printf ("");

};


 while (/* 条件 */)
 {
    /* 代码 */
 }

我是从一个关于 While 循环的视频学习的,但是IDE开始显示这些错误,我很困惑。请帮助。

英文:

I am new to Coding, There are 3 Errors or even more:

  1. Don't Know why int main (Void) is showing (redefinition of main error)
  2. below this function (While Loop) is showing (expected identifier or '(' before 'while'gcc) Error please help.

enter image description here

#include &lt;stdio.h&gt;

int main ()
{
    printf(&quot;Hello World&quot;);
    return 0;

};


/// @brief 
/// @param  
/// @return 
int main(void)
{
    //Getting the starting the value.

    int startingLambas = get_int(&quot;sLlambas&quot;);

    //Getting the Ending goal  llambas value.
    int endingLambas = get_int(&quot;endingLlambs&quot;);

    //Printing the required years needed.

printf (&quot;&quot;);

};


 while (/* condition */)
 {
    /* code */
 }

I was learning from a video about to While loop but IDE started to show these errors and I am confused. Please Help.

答案1

得分: 1

编程语言具有重要的作用域和可见性概念。

两个主要函数都在同一文件中定义。它们在文件作用域内定义。在同一作用域内,C编程语言不允许程序员创建重复的标识符。

函数的名称是一个标识符,就像变量或常量的名称是一个标识符一样。当在同一作用域内多次定义标识符时,编译器无法解决由多次使用相同名称命名多个事物引起的歧义。

C编译器寻找名为“main”的函数作为程序的起始点。在同一文件中放置两个“main”函数意味着有两个可以作为程序起始点的函数。编译器只能处理程序中的一个起始点。

while循环必须存在于函数的作用域内。例如:

#include <stdio.h>

int main() {
   // 使用while循环输出1到10的值
   int count = 1;
   while (count < 11) {
      printf("%d\n", count);
      count++;
   }
}

在上面的示例中,“main”标识符在文件作用域内定义。在“int main ()”之后的“{”开始了main函数的函数作用域。while循环存在于main函数的函数作用域内。因此,它存在于声明main的文件作用域内的作用域内。在while循环内部执行两个表达式。第一个表达式是printf函数,用于输出count的值。第二个表达式递增count变量。“count++;”之后的“}”结束了while循环的作用域。“}”结束了main函数的作用域。

C提供了两种类型的语句,简单语句和复合语句。简单语句是单个表达式,如“count++;”。复合语句包含多个简单或复合语句。复合语句以“{”开头,以“}”结尾。在“}”后面不跟分号。

主函数使用了一个复合语句,因为它的作用域包含多个语句。在上面的示例中,主函数包含一个简单语句和一个复合语句(while循环)。程序中的所有简单语句都以“;”结束。所有复合语句都以“{”开始,以“}”结束。

创建仅包含一个简单语句且没有其他内容的复合语句是可以接受的。事实上,这被认为是良好的防御性编程,因为它允许程序员添加额外的语句而不引起混淆。

英文:

Programming languages have important concepts of scope and visibility.

Both your main functions are defined within the same file. They are defined at the file scope. Within the same scope the C programming language does not allow the programmer to create duplicate identifiers.

The name of a function is an identifier, just as the name of a variable or constant is an identifier. When an identifier is defined more than once in the same scope the compiler is unable to resolve the ambiguity caused by naming multiple things with the same name.

The C compiler looks for a function named "main" to be the starting point of a program. Putting two "main" functions in the same file suggests two functions from which to start the program. The compiler can only deal with one starting point in any program.

A while loop must exist within the scope of a function. For example:

<!-- language: lang-c -->
#include <stdio.h>

int main () {
   // output the values 1 through 10 using a while loop
   int count = 1;
   while (count &lt; 11) {
      printf (&quot;%d\n&quot;, count);
      count++;
   }
}

In the example above the identifier "main" is defined at the file scope. The opening "{" after "int main ()" begins the function scope for the main function. The while loop exists within the function scope of the main function. Thus, it exists in a scope within the file scope in which main is declared. Within the while loop two expressions are executed. The first expression is the printf function which outputs the value of count. The second expression increments the count variable. The "}" following "count++;" ends the scope of the while loop. The final "}" ends the scope of the main function.

C provides two kinds of statements, simple statements and compound statements. Simple statements are single expressions such as "count++;". Compound statements contain multiple simple or compound statements. Compound statements begin with "{" and end with "}". No semicolon follows the "}".

The main function uses a compound statement because its scope includes multiple statements. In the example above the main function contains a simple statement and a compound statement (the while loop). All the simple statements in the program end with ";". All the compound statements begin with "{" and end with "}".

It is acceptable to create a compound statement containing only one simple statement and nothing else. In fact this is considered good defensive programming because it allows the programmer to add additional statement without confusion.

huangapple
  • 本文由 发表于 2023年6月18日 21:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定