0

I am learning OS course. We were taught pipes and this program is not getting compiled in GCC 4.4.7. When we change main to int main, it compiles fine. What is the reason?

Command line: gcc pipedemo.c

#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>

#define BUFFER_SIZE 100
#define READ_END 0
#define WRITE_END 1

main()
{
        char source[]="pipe_program";
        char dest[BUFFER_SIZE];
        char datas[BUFFER_SIZE];
        char datar[BUFFER_SIZE];
        int fd1[2],fd2[2];
        pid_t pid;
        if(pipe(fd1)==-1)
        {
                fprintf(stderr,"Pipe creation failed\n");
                exit(0);
        }

       if(pipe(fd2)==-1)
       {
           //...
       }
       //...
 }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Madhusoodan P
  • 681
  • 9
  • 20
  • Are there any reason you or your friend want to use non-standard syntax of function definition? – MikeCAT Aug 17 '16 at 03:11
  • The reason may be `-Werror` option or something. Would you mind disclosing the command line option? – MikeCAT Aug 17 '16 at 03:13
  • @MikeCAT I have no reason but by default the return type is int so we wrote that. But I want to know what is the error, Not intrested in compilation – Madhusoodan P Aug 17 '16 at 03:13
  • @MikeCAT sure edited – Madhusoodan P Aug 17 '16 at 03:14
  • "is not getting compiled " - you know this *how*? What is the reported error message from the failed compile attempt ? it belongs, verbatim, in your question. – WhozCraig Aug 17 '16 at 03:29
  • Note that the style of defining `main()` without an explicit return type of `int` was made obsolete (invalid) by the C99 standard. You should not be coding to the archaic C89/C90 standard which did allow the notation without an explicit return type. You should be adhering to C11, using `gcc -Wall -Wextra -Werror -std=c11` (or substituting `-std=gnu11` if you prefer). And your code should compile cleanly under those options. – Jonathan Leffler Aug 17 '16 at 04:57

1 Answers1

2

The standard (ISO/IEC 9899-2011) says about main:

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

      int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

      int main(int argc, char *argv[]) { /* ... */ }

or equivalent;

So that ends it all?
No, not that easy. At the end of the quote is a small semicolon and what follows is this little, seemingly innocent part

or in some other implementation-defined manner.

Than there is the difference between "hosted" (section 5.1.2.2 within an OS and its rules) and "freestanding" (in section 5.1.2.1. For example on a small PIC or similar, barely any rules at all and the rest is implementation defined)

The syntax of a function definition is defined in section 6.9.1 where it reads in item 1

The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.

Further detailed in item 2

The return type of a function shall be void or a complete object type other than array type.

So any function definition (including main) must have a return type specified.

But see the rules for the "freestanding environment" above or better, the lack of them.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20