-1

I have come a cross different ways of writing a functions e.g. consider main function in following ways

int main()
{ 
   some thing here;
   return 0;
}

Second and third variant could be like

void main()
{ 
   some thing here;
}

int main(int argv , char *argc[])
{ 
   some thing here;
   return 0;
}

So how can someone possibly write a single function in different ways? Which could cause errors in my opinion ?

How to write such functions ? Is is similar to overloading overloading or overwriting concept in java ? Is there a facility to use a single name for many functions ? Please provide an example of using such functions ?

Mazhar
  • 575
  • 5
  • 20
srikar sana
  • 115
  • 1
  • 9
  • The first two examples are incorrect. You can have `int main(void)` but remember that `main` is not called from a C function. The third example is by convention usually written the other way round: `int main(int argc , char *argv[])` – Weather Vane Oct 10 '16 at 08:56
  • sir the thing i wanted to ask is we are using a main function in this many forms cant we provide the same provision for the functions which we write ? if how to provide that facility – srikar sana Oct 10 '16 at 08:59
  • Not gonna lie, main is a rubbish example, but the point is, even in Java if you try to 'overload' functions with the same return type you should get some sort of ambiguous call exception. – George Oct 10 '16 at 09:01
  • There are not "many forms", there are obsolete definitions. If you use `int main(void)` you are telling the compiler that the arguments provided cannot be accessed. – Weather Vane Oct 10 '16 at 09:02
  • sir i am bad at taking examples sorry for that. I mean i saw my friend using same function name twice to write two functions but they were having different number of input argumnets with different types one with no arguments and one different int and char. then after seeing that program the main program came into my mind that we are also using the main in the same way (forget about the return value pls only consider input arguments) – srikar sana Oct 10 '16 at 09:04
  • Well please show the relevant examples. – Weather Vane Oct 10 '16 at 09:05
  • so i am going to write a function like this int fucn1( int a1, int a2, char a3) and if i call the function like func1(void) in middle of the program will this work? – srikar sana Oct 10 '16 at 09:07
  • No it will not work. You must supply the correct arguments. – Weather Vane Oct 10 '16 at 09:09
  • does the compiler raise any error for writing like that ? and i heard that main has three arguments but mostly we mention only two if and only if we want to use them only . but it contain three arguments but we are mentioning only two arguments but still working how is this happening – srikar sana Oct 10 '16 at 09:11
  • There is no function overloading in C. The two valid forms of `main` have nothing to do with overloading. `main` is allowed to have two forms (but not in the same program) because it's magic. – n. m. could be an AI Oct 10 '16 at 09:11
  • "No it will not work. You must supply the correct arguments" but main is working. main is also a function so if in c if main is having such a feature then why can't we give such things to other functions also – srikar sana Oct 10 '16 at 09:13
  • For third argument to `main` [please see this](http://stackoverflow.com/questions/10321435/is-char-envp-as-a-third-argument-to-main-portable) – Weather Vane Oct 10 '16 at 09:14
  • "No it will not work" means your commented function call will not work. – Weather Vane Oct 10 '16 at 09:15
  • Duplicate of http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336 – Klas Lindbäck Oct 10 '16 at 09:15
  • 1
    "why can't we give such things to other functions also?" Because that is the way C is written. `main` is not "**a** C function" it is the entry point for code execution. It is a special case. – Weather Vane Oct 10 '16 at 09:19
  • its not a duplicate i think so sir klas lindback i am not asking about the return value of main is nor i am asking for the proper signature for the main function but how is the c compiler allowing these many signatures for main function. – srikar sana Oct 10 '16 at 09:24
  • isn't main written in c library like other functions like printf() and all ? – srikar sana Oct 10 '16 at 09:25
  • Of course not - you wrote `main` yourself. – Weather Vane Oct 10 '16 at 09:31
  • thank you sir weather vane and all this question of mine gave me some basic points – srikar sana Oct 10 '16 at 09:34

1 Answers1

0

I am trying to answer your questions here:

First, main is of course a function. In fact, main is the entry point for every c program, and every c program must have a main function.

A C-Program consists of variables and functions, and MUST have at least one function. In the minimal case, this will be the main function.

Ok, so much for the theory and the first part of your question.

What i think is the thing thats bugging your mind is: You think that main() is a C library function and therefore it cannot be "overloaded".

The truth is, its not a library function, its just a specific entry point for your program, and it identifies itself by the name "main", not the return type or arguments it takes.

You can write a C program with just

main(){
 [your code here]
}

or also

int main(int argc, char * argv[]){
 [your code here]
}

So you declare and define the main function yourself and you do it the way it best fits your program's need (i.e. if your program takes command line variables, you will pick the second example).

Remember, main is not a library function, its just a naming convention where a C-program starts.

You can also have a look at The C Programming Language (Second Edition) by Brian Kernighan and Dennis Ritchie, pages 5-7.

Iamafox
  • 28
  • 9
  • sir lamafox thank you and i have been recently searching about the variadic functions can't we work with different arguments types?,in every example given on the internet there is only an example with only multiple int arguments. cant we use some char some int some float do we have such facility ?can we get that by using va_macros – srikar sana Oct 10 '16 at 09:32
  • You can use variadic functions with float or char for example but the datatype gets promoted, as you can read in this post: http://stackoverflow.com/questions/11270588/variadic-function-va-arg-doesnt-work-with-float – Iamafox Oct 10 '16 at 09:39
  • Btw, seeing answer below, it is NOT possible to overload functions in C like that, it will give you a "conflicting types" error. Dont get confused. – Iamafox Oct 10 '16 at 09:45