Why doesn't string main() works as char main() and int main() do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

8th Apr 2019, 3:08 AM
user45368
1 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.
8th Apr 2019, 3:28 AM
Fermi
Fermi - avatar