#C++, my first program
27 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
the image shows a C program, you are writing C++
Yes but I was thought to coding like this
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!";
return 0;
}
yes , this is how you write a simple hello world program in C++ , you are correct
So do I need to learn both methods ?
no, in the image you posted , they are using C , they are not using C++
you dont need to learn how to write C code
Even though almost* all C code is valid in C++, C and C++ are both different languages and you do stuff differently in both languages.
you dont need to worry about stdio.h or printf
those are C language things , not C++
they are valid in C++ only because C++ is supposed to be superset of C
for now you don’t
Oh, thanks for the clarification
almost *
I don't recommend using using namespace std; fwiw
just make the lecturer happy and not use it anywhere else
In C++23 (modern C++) you can use this instead even:
#include <print>
int main()
{
std::print("Hi!");
}```
It's simpler, modern and more intuitive
C++ the modern way can feel very nice to get started with
If you don't have access to modern C++ (or your lecturer just sucks and doesn't know any better) you can keep using the old and confusing method
#include <iostream>
int main()
{
std::cout << "Hi!";
}``` of course
Formatting arguments is also much more intuitive in C++23 than it was in previous standardisations, so I recommend using std::print so you don't get too confused :D
Modern: std::println("MyInt = {}, something in hex = {:x}, aligned 3rd arg with fixed width of the 4th arg: {: >{}}", number1, number2, number3, width);
Old: std::cout << number1 << std::hex << number2 << std::dec << std::setfill(' ') << std::setw(width) << number3 << '\n';
C: cursed
or give up doing iostreams and use printf 🙂
C is not cursed, it's just not C++
Besides, why don't we use the import since we're using modern C++ now?
Okay then write the equivalent in C
@red sorrel Has your question been resolved? If so, type !solved :)
Solved
bro just use ai already