4 ms·
The hello world C+ example is literally hello world in C #include <stdio.h> int main() { printf("hello, world\n"); return 0;
by antegamisou 2y ago
The hello world C+ example is literally hello world in C
#include <stdio.h>
int main()
{
printf("hello, world\n");
return 0;
}
¯\_(ツ)_/¯
- _kst_ 2y agoExcept that in C (at least prior to C23), it should be "int main(void)". I'm fairly sure that all existing C compilers will quietly accept "int main()", but there's an argument that "int main()" causes the program to have undefined behavior. "int main(void)" is the form documented in the standard. And the "return 0;" is unnecessary both in C++ and in all versions of C starting with C99. (Some will argue that it's better style to be explicit.) These are admittedly minor points.
- dmvdoug 2y agoMeh, the program execution language about main() has included from the beginning an escape hatch: it allows main to be defined “in some other implementation-defined manner.” “int main(void)” is one of four forms documented in the standard (or one of two specific shapes of code; the other two are “or equivalent” and “or in some other implementation-defined manner.”
- e12e 2y agoEd: ah, I see it's from TFA: "Hello World in Orthodox C++" Is that really the hello world of c++ though? It's a bit long in the tooth perhaps, bit I think still highlights some differences between C and C++: "Learning Standard C++ as a New Language": https://www.stroustrup.com/new_learning.pdf https://www.stroustrup.com/new_learning.pdf Ed: I suppose if you want to throw out a lot of c++, then the article might not apply...
- zzo38computer 2y agoSome programs will work in C as well as in C++. It does not mean that it is not a C++ program.
- antegamisou 2y ago3 out of 3 people replying haven't been able to differentiate C+ (orthodox C++) from Bjarne Stroustrup's C++. I posted the hello world code to show it's a poor example as it displays similitude between C+ and C.
- pjmlp 2y agoNot any longer, this is the modern C++ hello word. import std; int main() { std::println("hello, world"); } Although your point stands, that is also valid C++.
- nineteen999 2y agoHe is LITERALLY referring to the the example on the page in question.
- pjmlp 2y agoAnd I am literally showing that the example is outdated in C++23.