What is the function of 'main' in 'c' programming?

what is the function of 'main' in 'c' programming?

int main (void)
In the above statement, void represent??

Replies

  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    what is the function of 'main' in 'c' programming?

    int main (void)
    In the above statement, void represent??
    void represents that main function is not taking any argument while its execution. You can even skip that void and write main(). main takes two arguments like main(int argc, char *argv[])
    where argc is the counter that counts the number of arguments passed to main and argv means argument vector which stores all the arguments passed in the form of a character array
  • aarthivg
    aarthivg
    I remember using as such: void main()
    whats the difference between these statement.
    void main()

    int main(void)
  • aarthivg
    aarthivg
    Nick_Sharma
    void represents that main function is not taking any argument while its execution. You can even skip that void and write main(). main takes two arguments like main(int argc, char *argv[])
    where argc is the counter that counts the number of arguments passed to main and argv means argument vector which stores all the arguments passed in the form of a character array
    Is arguments and parameter, both mean the same
  • Anoop Kumar
    Anoop Kumar
    void main() means... this function is not returning any value.
    int void main() is a illegal declaration .
    either it could be int main() , which must have a integer type return statement as last line of function
    or another type of return.
  • aarthivg
    aarthivg
    ianoop
    void main() means... this function is not returning any value.
    int void main() is a illegal declaration .
    either it could be int main() , which must have a integer type return statement as last line of function
    or another type of return.
    sorry, its int main(void)
  • Anoop Kumar
    Anoop Kumar
    aarthivg
    sorry, its int main(void)
    that means main function is not taking any argument and will return a integer type value.
    why should not you make some programs about your doubts.compiler itself will tell you .
  • aarthivg
    aarthivg
    ianoop
    that means main function is not taking any argument and will return a integer type value.
    why should not you make some programs about your doubts.compiler itself will tell you .
    ya I have too. I don't have the softwareπŸ˜”
  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    Is arguments and parameter, both mean the same
    Yeah they are same...!
  • Dancer_Engineer
    Dancer_Engineer
    aarthivg
    ya I have too. I don't have the softwareπŸ˜”
    Take a look at this video:
  • silverscorpion
    silverscorpion
    The function definition in C is like this..

     function-name()
    Here, return-type can be of any data-type, like int or char etc.. It can also be "void" which means that function doesn't return anything.

    In the list of arguments, you can give one or more arguments (or) 'parameters' to the function. If the function doesn't take any arguments, you can specify that by writing 'void' in place of the arguments. Hence 'function-name(void)'. But usually, if a function doesn't take any arguments, it's name is simply followed by a set of parentheses, like so.. "function-name()"
  • aarthivg
    aarthivg
    return statement means?
  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    return statement means?
    return type depicts the type of value that your function returns. return statement returns a value to the caller function. Consider following program

    main()
    {
    int c;
    c=sum();
    printf("%d",c);
    return 0;
    }
    int sum()
    {
    int a=1,b=2;
    return(a+b);
    }
    In the above program, return(a+b) is called return statement as it returns an integer value to the calling function that is main. Now as an integer value is returned, there must be a variable to hold the returned data and even that must be of the same type as the type of value returned i.e. int in this case..
  • herrey
    herrey
    Well how differ main from array?
    I know in C++ array are so important so can you explain its difference?
  • Neeraj Sharma
    Neeraj Sharma
    herrey
    Well how differ main from array?
    I know in C++ array are so important so can you explain its difference?
    There is no match between main and array. They are two different things all together. main() marks the beginning point of execution of a program whereas array is simply a collection of objects of same type which are referred under a same name whose memory is allocated statically. main() is a function whereas array is a collection of elements of the same data type
  • aarthivg
    aarthivg
    Nick_Sharma
    return type depicts the type of value that your function returns. return statement returns a value to the caller function. Consider following program

    main()
    {
    int c;
    c=sum();
    printf("%d",c);
    return 0;
    }
    int sum()
    {
    int a=1,b=2;
    return(a+b);
    }
    In the above program, return(a+b) is called return statement as it returns an integer value to the calling function that is main. Now as an integer value is returned, there must be a variable to hold the returned data and even that must be of the same type as the type of value returned i.e. int in this case..
    return 0 implies ?
  • Neeraj Sharma
    Neeraj Sharma
    As I have not specified any return type for main, by default it will be int. So as the system calls the main function, you are just returning a dummy integer value. It doesn't do anything..
  • aarthivg
    aarthivg
    then getch() is used for?
  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    then getch() is used for?
    getch() is used to read a character from keyboard...
  • aarthivg
    aarthivg
    #include
    #include
    int main()
    {
    int a,b,sub;
    printf(" enter the value of a");
    scanf("%d",&a);
    printf("enter the value of b");
    scanf("%d",&b);
    sub=a-b;
    printf(" %d",&sub);
    return 0;
    }

    just tried out a simple program.... but cant able to get the output... whats wrong in it

    for 3-2 ... getting output as 9016
  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    printf(" %d",&sub);
    Here is the mistake. & is called address operator. So when you are printing &sub, you are printing the address at which the result is stored and not the result. replace the above line with

    printf("%d",sub);

    This will make your program work perfectly..
  • aarthivg
    aarthivg
    Nick_Sharma
    Here is the mistake. & is called address operator. So when you are printing &sub, you are printing the address at which the result is stored and not the result. replace the above line with

    printf("%d",sub);

    This will make your program work perfectly..
    ya...thanks... got it
  • aarthivg
    aarthivg
    what is the use of SYSTEM("PAUSE") command
  • Neeraj Sharma
    Neeraj Sharma
    aarthivg
    what is the use of SYSTEM("PAUSE") command
    System() is used to call OS functions. System("pause") causes the DOS output screen to be held. It does the same function as getch() when used as the last line in program. If you don't write getch() or System("pause") in the earlier versions of C then the black screen just flashes and then disappears. Then you must press alt+f5 to view the output. System("pause") and getch() prevents the flashing of screen and holds it till a character is entered
  • aarthivg
    aarthivg
    Is there a header file defined Boolean constant as stdbool.h?
    #include
    #include
    int main(void)
    {
    bool x=true;
    bool y=false;
    printf(" the boolean values are %d %d\n",x,y);
    return 0;
    }
  • aarthivg
    aarthivg
    Is there a header file defined Boolean constant as stdbool.h?
    #include
    #include

    int main(void)
    {
    bool x=true;
    bool y=false;
    printf(" the boolean values are %d %d\n",x,y);
    return 0;
    }

    help me out
    Tagging #-Link-Snipped-# #-Link-Snipped-# #-Link-Snipped-#
  • Anoop Kumar
    Anoop Kumar
    I have no idea about it... 😐
  • Neeraj Sharma
    Neeraj Sharma
    Same here. No idea. Tried searching it but with no significant answer.. Anyway the above program is not working in Turbo C++ 4.5. I downloaded stdbool.h and tried but then also there are errors...
  • Abhishek Rawal
    Abhishek Rawal
    Sorry for bumping in old thread (Killing time);

    Yes, there's a header file defined boolean constant as stdbool.h

    1 4

    Using emacs editor & GCC 4.7
  • sulochana anand
    sulochana anand
    the execution of program starts from main function.
  • bithamang
    bithamang
    in 11th line u should write as
    printf("%d",sub);
    only u put & before sub so it displays the address of sub
  • Sanyam Khurana
    Sanyam Khurana
    aarthivg
    what is the function of 'main' in 'c' programming?

    int main (void)
    In the above statement, void represent??

    The main function has two basic functions :-

    1) It allocates the memory for program.

    2) It guides the compiler, that compilation will start from the main function.

    In the statement

    int main (void); (always terminate a statement with a semicolon) πŸ˜›

    void represents that the function do not takes any arguments.

    The statement is same as

    int main(); πŸ˜€

    If you still have any doubts, feel free to ask 😁
  • rahul69
    rahul69
    Sanyam Khurana
    int main (void); (always terminate a statement with a semicolon) πŸ˜›
    Not always!!😎 Actually avoid semicolon in case of loops and conditional statements(ie if blocks)
    eg:
    int i=0;
    while (i<10);
    i++;
    . Big Blunder!!πŸ˜‰πŸ˜
  • lit-857
    lit-857
    int main() return value to whom??
  • lit-857
    lit-857
    last line should be printf("%d",sub);
  • lit-857
    lit-857
    aarthivg
    #include
    #include
    int main()
    {
    int a,b,sub;
    printf(" enter the value of a");
    scanf("%d",&a);
    printf("enter the value of b");
    scanf("%d",&b);
    sub=a-b;
    printf(" %d",&sub); //error//
    return 0;
    }

    just tried out a simple program.... but cant able to get the output... whats wrong in it

    for 3-2 ... getting output as 9016


    line should be printf("%d",sub);
  • avii
    avii
    lit-857
    int main() return value to whom??
    to the system. Or to the caller.
  • Sanyam Khurana
    Sanyam Khurana
    rahul69
    Not always!!😎 Actually avoid semicolon in case of loops and conditional statements(ie if blocks)
    eg:
    int i=0;
    while (i<10);
    i++;
    . Big Blunder!!πŸ˜‰πŸ˜
    haha, yes that's true, I was talking about General statements πŸ˜›
  • lit-857
    lit-857
    who call the main function
  • Sanyam Khurana
    Sanyam Khurana
    lit-857
    who call the main function
    What do you mean?
  • Vishal Sharma
    Vishal Sharma
    ianoop
    void main() means... this function is not returning any value.
    int void main() is a illegal declaration .
    either it could be int main() , which must have a integer type return statement as last line of function
    or another type of return.

    actually it returns a value.. but its not specific so we call it void..
  • Vishal Sharma
    Vishal Sharma
    lit-857
    who call the main function
    its the operating system...
    and main() is always "public" so that it is visible to the OS
  • Abhishek Rawal
    Abhishek Rawal
    Vishal0203
    its the operating system...
    and main() is always "public" so that it is visible to the OS
    As per my knowledge, RTE calls the main function & not just OS.
  • lit-857
    lit-857
    wts RTE and main () return value to whome???
  • Vishal Sharma
    Vishal Sharma
    RTE stands for run-time environment..
    main() returns value to RTE itself, indicating its status of execution
  • lit-857
    lit-857
    plz explain it i can't understend
  • Vishal Sharma
    Vishal Sharma
    Means, if main returns a null value, then the program is executed successfully.. 0 indicates EXIT_ON_SUCCESS
    any value other than zero indicates unusual execution..
  • Anand Tamariya
    Anand Tamariya
    lit-857
    plz explain it i can't understend
    A c program is compiled into assembly code(and finally into binary). The main() function enables the compiler to put the code present in this method as first line in the compiled version - the entry point of the program when you execute it.
  • Vishal Sharma
    Vishal Sharma
    main is a function and array is a collection of elements of same data type defined under same name..
    no point of getting confused here.
  • lit-857
    lit-857
    wts d/f b/w celloc() and melloc() fn
  • Vishal Sharma
    Vishal Sharma
    lit-857
    wts d/f b/w celloc() and melloc() fn
    ummm... do you have a text Book to learn C??
    I think you must refer that.. text books are the first companions. after reading that, still you face problems in understanding, we'll help you
  • Jeffrey Arulraj
    Jeffrey Arulraj
    lit-857
    wts d/f b/w celloc() and melloc() fn
    At the same time please avoid SMS language cause it can lead to misconception among us

    We are willing to help you more so please be more clear to us

You are reading an archived discussion.

Related Posts

MCA

I AM A MCA STUDENT.
hai friends m navin. frm kerala,india... I am doing my engineering in CS...
Name: Syed Ahmed Kamal *Engineering Field: Electronics Location: Karachi,Pakistan *Occupation: (Student of third year Electronics Engineering at Sir Syed University) *I joined CrazyEngineers because: To let to know about new...
Entrance exam is a part of every engineer's life. Here's a comic take by Absolute Zero on entrance exams.
hello Engineers ! I am a student of electronics and communication engineering! i will graduate hopefully , i year or 2 from now. (hopes) Im here to look better ideas...