#C++, my first program

27 messages · Page 1 of 1 (latest)

red sorrel
#

In my class I was thought to use

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, world!";
    return 0;
}

To write my first program

And when I went on YouTube for more tutorials I saw to use the method in the picture below and I’m confused??

loud siloBOT
#

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.

hazy gazelle
red sorrel
hazy gazelle
red sorrel
hazy gazelle
# red sorrel 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

fiery granite
#

for now you don’t

red sorrel
terse ocean
fiery granite
#

just make the lecturer happy and not use it anywhere else

terse ocean
#

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
fiery granite
#

C++ the modern way can feel very nice to get started with

terse ocean
#

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

fiery granite
#

or give up doing iostreams and use printf 🙂

olive thicket
#

Besides, why don't we use the import since we're using modern C++ now?

terse ocean
loud siloBOT
#

@red sorrel Has your question been resolved? If so, type !solved :)

exotic ore
#

bro just use ai already