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

aarthivg

aarthivg

@aarthivg-HH344f Oct 26, 2024
what is the function of 'main' in 'c' programming?

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

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 12, 2012

    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

    @aarthivg-HH344f Jun 12, 2012

    I remember using as such: void main()
    whats the difference between these statement.
    void main()

    int main(void)
  • aarthivg

    aarthivg

    @aarthivg-HH344f Jun 12, 2012

    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

    @anoop-kumar-GDGRCn Jun 12, 2012

    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

    @aarthivg-HH344f Jun 12, 2012

    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

    @anoop-kumar-GDGRCn Jun 12, 2012

    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

    @aarthivg-HH344f Jun 12, 2012

    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

    @neeraj-iAaNcG Jun 12, 2012

    aarthivg
    Is arguments and parameter, both mean the same
    Yeah they are same...!
  • Dancer_Engineer

    Dancer_Engineer

    @dancer-engineer-EJ8rGI Jun 13, 2012

    aarthivg
    ya I have too. I don't have the software😔
    Take a look at this video:
  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ Jun 13, 2012

    The function definition in C is like this..

    <return-type> function-name(<list-of-arguments>)
    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

    @aarthivg-HH344f Jun 13, 2012

    return statement means?
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 13, 2012

    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

    @herrey-bokSq9 Jun 13, 2012

    Well how differ main from array?
    I know in C++ array are so important so can you explain its difference?
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 13, 2012

    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

    @aarthivg-HH344f Jun 13, 2012

    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

    @neeraj-iAaNcG Jun 13, 2012

    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

    @aarthivg-HH344f Jun 13, 2012

    then getch() is used for?
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 14, 2012

    aarthivg
    then getch() is used for?
    getch() is used to read a character from keyboard...
  • aarthivg

    aarthivg

    @aarthivg-HH344f Jun 14, 2012

    #include<stdio.h>
    #include<stdlib.h>
    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

    @neeraj-iAaNcG Jun 14, 2012

    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

    @aarthivg-HH344f Jun 14, 2012

    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

    @aarthivg-HH344f Jun 14, 2012

    what is the use of SYSTEM("PAUSE") command
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 14, 2012

    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

    @aarthivg-HH344f Jun 14, 2012

    Is there a header file defined Boolean constant as stdbool.h?
    #include<stdio.h>
    #include<stdbool.h>
    int main(void)
    {
    bool x=true;
    bool y=false;
    printf(" the boolean values are %d %d\n",x,y);
    return 0;
    }
  • aarthivg

    aarthivg

    @aarthivg-HH344f Jun 17, 2012

    Is there a header file defined Boolean constant as stdbool.h?
    #include<stdio.h>
    #include<stdbool.h>

    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

    @anoop-kumar-GDGRCn Jun 17, 2012

    I have no idea about it... 😐
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jun 18, 2012

    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

    @abhishek-fg9tRh Feb 16, 2013

    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

    @sulochana-anand-OrGmKr Mar 4, 2013

    the execution of program starts from main function.
  • bithamang

    bithamang

    @bithamang-9pwN1D Mar 14, 2013

    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

    @sanyam-Nl7Zqc Apr 4, 2013

    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

    @rahul69-97fAOs Apr 4, 2013

    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

    @lit-857-hXUEGJ Apr 4, 2013

    int main() return value to whom??
  • lit-857

    lit-857

    @lit-857-hXUEGJ Apr 4, 2013

    last line should be printf("%d",sub);
  • lit-857

    lit-857

    @lit-857-hXUEGJ Apr 4, 2013

    aarthivg
    #include<stdio.h>
    #include<stdlib.h>
    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

    @avii-TGGs8o Apr 4, 2013

    lit-857
    int main() return value to whom??
    to the system. Or to the caller.
  • Sanyam Khurana

    Sanyam Khurana

    @sanyam-Nl7Zqc Apr 5, 2013

    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

    @lit-857-hXUEGJ Apr 5, 2013

    who call the main function
  • Sanyam Khurana

    Sanyam Khurana

    @sanyam-Nl7Zqc Apr 6, 2013

    lit-857
    who call the main function
    What do you mean?
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Apr 6, 2013

    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

    @vishal-pysGmK Apr 6, 2013

    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

    @abhishek-fg9tRh Apr 6, 2013

    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

    @lit-857-hXUEGJ Apr 6, 2013

    wts RTE and main () return value to whome???
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Apr 6, 2013

    RTE stands for run-time environment..
    main() returns value to RTE itself, indicating its status of execution
  • lit-857

    lit-857

    @lit-857-hXUEGJ Apr 6, 2013

    plz explain it i can't understend
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Apr 7, 2013

    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

    @anand-tamariya-DnfjEX Apr 7, 2013

    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

    @vishal-pysGmK Apr 7, 2013

    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

    @lit-857-hXUEGJ Apr 11, 2013

    wts d/f b/w celloc() and melloc() fn
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Apr 11, 2013

    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

    @jeffrey-xA7lUP Apr 12, 2013

    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