Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is main necessary in a program

Tags:

c

I made a program which is still in development. I did not declared main in my program deliberately.As I am developing a library for making graph and other algorithm which I would use in my programs.The purpose to develop such a library in C is to work upon the problems given in book Introduction to Algorithms Thomas H Coreman Here is the code if some one wants to see.

#include<stdio.h>
#include<stdlib.h>
#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node *graph;

graph cnode(int data);      //cnode is to create a node for graph
void cgraph(void);
struct node {
    int data, color;
    struct node *LEFT, *RIGHT, *TOP, *DOWN;
};

graph root = NULL;

void cgraph(void)
{
    int n, choice, dir, count;

    choice = 1;
    count = 1;
    graph priv, temp;

    printf("Printf we are making a graph the first is root node\n");
    while (choice == 1) {
        count++;
        if (count == 1) {
            printf("This is going to be root node \n");
            scanf("%d", &n);
            root = cnode(n);
            count--;
            priv = root;
        }       //ending if
        else {
            printf
                ("Enter direction you want to go LEFT 1 RIGHT 2 TOP 3 DOWN 4\n");
            scanf("%d", &dir);
            printf("Enter the data  for graph node\n");
            scanf("%d", &n);
            temp = cnode(n);
            if (dir == 1) {
                priv->LEFT = temp;
            }
            if (dir == 2) {
                priv->RIGHT = temp;
            }
            if (dir == 3) {
                priv->TOP = temp;
            }
            if (dir == 4) {
                priv->DOWN = temp;
            }
            priv = temp;
        }       //ending else
        printf
            ("Enter 1 to continue adding nodes to graph any thing else would take you out\n");
        scanf("%d", &choice);
    }           //ending while
}               //ending main

graph cnode(int data)
{
    graph temp = (graph) malloc(sizeof(graph));

    temp->data = data;
    temp->LEFT = NULL;
    temp->RIGHT = NULL;
    temp->TOP = NULL;
    temp->DOWN = NULL;
    temp->color = -1;
    return temp;
}

When I compiled the above program I got following error.

cc graph.c
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

What does this error signify and why should I declare main in my program?

like image 756
reality displays Avatar asked Nov 24 '10 07:11

reality displays


People also ask

Why We Use main in the program?

Importance points of the main() function An operating system always calls the main() function when a programmers or users execute their programming code. It is responsible for starting and ends of the program. It is a universally accepted keyword in programming language and cannot change its meaning and name.

Is main function necessary?

c does not require a main() function. Show activity on this post. main is mandatory to be present if you are building your code into an application as main functions serves as an entry point for the application. But, if your code is being built as lib, then main is not needed.

What is the main of a program?

A sequence of instructions that is used when a program is started and is central to its structure, being able to invoke subprograms or subroutines as required and being itself executed by the operating system.

Is it necessary to have a main () function in AC program?

No, the ISO C standard states that a main function is only required for a hosted environment (such as one with an underlying OS). For a freestanding environment like an embedded system (or an operating system itself), it's implementation defined.


5 Answers

By default, gcc (and most C compilers) compile and link to a standalone executable. The main() function is required so that the startup code knows where execution of your code should start.

To compile your library code without linking, use gcc -c graph.c. In this case, graph.c does not require a main() function.

like image 77
caf Avatar answered Oct 05 '22 13:10

caf


main is mandatory to be present if you are building your code into an application as main functions serves as an entry point for the application.

But, if your code is being built as lib, then main is not needed.

EDIT: Check this for information on static and shared libraries.

like image 28
Jay Avatar answered Oct 05 '22 12:10

Jay


Why? Because the standard says so (mostly).

The main function is required for hosted C environments (freestanding environments are allowed to start up any way they like).

If you're developing a library, you don't need a main for the library itself but you won't be able to turn it into an executable without one (other than by using non-portable trickery). And, at a bare minimum, you should have one for the test suite anyway.

In other words, your library should have a large test suite which is controlled from a main function (most likely in a separate source file or files) so that you can test any new work and regression-test to ensure it hasn't stuffed up the old work.

like image 23
paxdiablo Avatar answered Oct 05 '22 13:10

paxdiablo


A program needs an entry point to clarify where your program starts. Without that, it's impossible for your tools to know which function should be called first.

It is possible to specify another function as entry point, but by using main, everyone that reads your code will know where your program starts.

Usually, when developing libraries, you will put main in a separate program and use that as a starting point while testing out your library. Something like this:

gcc -o library.o -c library.c
gcc -o main.o -c main.c
gcc -o testprogram library.o main.o
like image 33
Robert Avatar answered Oct 05 '22 11:10

Robert


normally main() is start up itself. if you ignore the main() it need any starter to execute program. basically main is an identifier which identified by compiler when a program is executed.

like image 21
user2964054 Avatar answered Oct 05 '22 13:10

user2964054