Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can you return a const char* in main?

Tags:

c++

Can anyone explain how this code doesn't give errors when compiled?

int main()
{
  // why doesn't the following line give a type mismatch error??/
  return "success!";
}
like image 507
user3398519 Avatar asked Dec 13 '25 11:12

user3398519


1 Answers

Because of the trigraph ??/, which is replaced by a \, meaning the return is commented out. Your code is equivalent to

int main()
{
  // why doesn't the following line give a type mismatch error\
  return "success!";
}

which is the same as

int main()
{
  // why doesn't the following line give a type mismatch error return "success!";
}

Also note that, in the absence of a return statement in main, there is an implicit return 0;, so the code is well defined.

like image 122
juanchopanza Avatar answered Dec 15 '25 03:12

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!