0

Why doesn't string main() works as char main() and int main() do?

I was replacing int main() with char main() to see how the code will look like: #include <stdio.h> int main() { printf("Hello, World!\n"); 5 return 0; } *********** #include <stdio.h> char main() { printf("Hello, World!\n"); return 0; } They both gives the same output except if I run it with string: #include <stdio.h> string main() { printf("Hello, World!\n"); return 0; } Why is that?

c

4/8/2019 3:08:15 AM

user45368

2 Answers

New Answer

+3

The proper signature for main is actually int main() int main(int argc, char *argv[]) // to take arguments during launch when you do char main() the compiler detects that main returns char but allows it, because the returned character is interpreted as its integer ASCII counterpart. http://www.asciitable.com/ std::string, however, cannot be implicitly converted to int without casting. string main() { return 0; } makes no sense, since you are declaring that main returns string but you are returning a numerical value instead of a string.

+1

Fermi In C char is not interpreted as int, rather it is an int user45368 Where does string came from in C? In C if you are going to use C++ compiler will obviously complain. Anyways main return type must be int in C/C++. string type is a class and can't be converted to int.