Search code examples
cwhile-loopidentifierundeclared-identifier

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


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 <stdio.h>

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

};


/// @brief 
/// @param  
/// @return 
int main(void)
{


};

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


Solution

  • 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:

    #include <stdio.h>
    
    int main () {
       // output the values 1 through 10 using a while loop
       int count = 1;
       while (count < 11) {
          printf ("%d\n", 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.