0

When trying to compile my mario.c and greedy.c programs, Clang compiler error messages indicate that a semicolon is needed after int main(void).

The C book that I am using (i.e., C Step by Step by Mitchell Waite and Stephen Prata ,SAMs 1989) does not indicate that a semicolon is required after main()'s signature. Incidentally, that book examples just start with main ().

If we didn't put a semicolon after main()'s signature though, Clang compiler prints the following error messages:

greedy2.c:4:2: error: type specifier missing, defaults to 'int'
      [-Werror,-Wimplicit-int]
 main() 
 ^~~~
greedy2.c:4:8: error: expected ';' after top level declarator
 main() 
       ^
       ;
greedy2.c:7:4: error: expected identifier or '('
   { 
   ^
greedy2.c:27:14: error: initializer element is not a compile-time constant
     int i = round (change*100) ;
             ^~~~~~~~~~~~~~~~~~
greedy2.c:29:6: error: expected identifier or '('
     {
     ^
5 errors generated.
jharvard@appliance (~/Dropbox/pset1):

uma1966

2 Answers 2

3

No, you don't need to write a semicolon after the main function's signature.

However when you are creating functions other than main() you usually write a function prototype (also known as declaring the function). In that case, you write the function's signature before main() and end it with a semicolon.

In main(), you use this function by calling it.

After the closing brace of main(), you then go on and define the function.

In the following example, notice how I use a custom function (i.e.,square()) to return the square of an int. Pay close attention to the use of semicolons!

#include <stdio.h>

int square(int a); //Declaring a function by writing its prototype

int main(void)
{

    printf("hello, world\n");

    int ans;

    ans = square(10); // using a function by calling it in an assignment statement.

    printf("your answer is %d\n",ans);

    return 0;
}

int square(int a) // defining a function
{
    return a * a;
}

The output is

hello,world
your answer is 100

Hope this helps! Good luck!!

2

Please read this: http://d2o9nyf4hwsci4.cloudfront.net/2013/fall/lectures/1/w/notes1w/notes1w.html#_code_hello_world_code

See the sample hello world?

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
}

int main (void) is the start of your main function. Then, you use curly braces { and } to surround the lines of code that are in your function.

You keep getting errors because you don't have those curly braces. Because you don't have those curly braces, the compiler thinks you are trying to declare a function prototype, which does require a ; at the end. But that's not what you want to do.

Please review the document I linked. It will help you with the syntax.

1
  • Thanks for your Clarification. Part of my Syntax errors were due to my use of an 1989 old C Book . I appear to have missed the notes sent by you. It might be in the Reference section of Lecture Notes1.---Uma1966. initial posting by the author @curiouskiwi
    – uma1966
    Jul 9, 2014 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .