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

    aarthivg

    Member

    Updated: Oct 26, 2024
    Views: 1.2K
    what is the function of 'main' in 'c' programming?

    int main (void)
    In the above statement, void represent??
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Neeraj Sharma

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 12, 2012

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

    int main(void)
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 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.
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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)
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 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 .
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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😔
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 12, 2012

    aarthivg
    Is arguments and parameter, both mean the same
    Yeah they are same...!
    Are you sure? This action cannot be undone.
    Cancel
  • Dancer_Engineer

    MemberJun 13, 2012

    aarthivg
    ya I have too. I don't have the software😔
    Take a look at this video:
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberJun 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()"
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 13, 2012

    return statement means?
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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..
    Are you sure? This action cannot be undone.
    Cancel
  • herrey

    MemberJun 13, 2012

    Well how differ main from array?
    I know in C++ array are so important so can you explain its difference?
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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..
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 13, 2012

    then getch() is used for?
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 14, 2012

    aarthivg
    then getch() is used for?
    getch() is used to read a character from keyboard...
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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..
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 14, 2012

    what is the use of SYSTEM("PAUSE") command
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • aarthivg

    MemberJun 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-#
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 17, 2012

    I have no idea about it... 😐
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 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...
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberFeb 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
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberMar 4, 2013

    the execution of program starts from main function.
    Are you sure? This action cannot be undone.
    Cancel
  • bithamang

    MemberMar 14, 2013

    in 11th line u should write as
    printf("%d",sub);
    only u put & before sub so it displays the address of sub
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberApr 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 😁
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberApr 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!!😉😁
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 4, 2013

    int main() return value to whom??
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 4, 2013

    last line should be printf("%d",sub);
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 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);
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberApr 4, 2013

    lit-857
    int main() return value to whom??
    to the system. Or to the caller.
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberApr 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 😛
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 5, 2013

    who call the main function
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberApr 6, 2013

    lit-857
    who call the main function
    What do you mean?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 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..
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 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
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 6, 2013

    wts RTE and main () return value to whome???
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 6, 2013

    RTE stands for run-time environment..
    main() returns value to RTE itself, indicating its status of execution
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 6, 2013

    plz explain it i can't understend
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 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..
    Are you sure? This action cannot be undone.
    Cancel
  • Anand Tamariya

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 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.
    Are you sure? This action cannot be undone.
    Cancel
  • lit-857

    MemberApr 11, 2013

    wts d/f b/w celloc() and melloc() fn
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberApr 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
    Are you sure? This action cannot be undone.
    Cancel
  • Jeffrey Arulraj

    MemberApr 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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register