C++ version

I am currently taking the C++ courses and I am wondering which version of C++ I’am dealing with? I have seen some videos in youtube from like 2013 and when they need to print a phrase they just write cout, but in the version I am taking I need to write std::cout. How old is the version I am dealing with?

Hi @course2786670512

I don’t know specifically which version of C++ the Codecademy course uses, though I think it’s a reasonably modern variant.

In any case, I think I can explain why you’re seeing cout on it’s own in the YouTube video you’re watching but std::cout here on Codecademy.

C++ contains a standard library, but it isn’t available to your program without importing the relevant bits of it. This is why, at the start of your program, you have the #include <iostream> and similar.

<iostream> itself contains some includes, but one of the objects which it contains is the std::cout object. The reason we need to include the std:: part is because cout is an object in the std namespace. Your code will exist in a different namespace, so you need to tell C++ which namespace and object you want - hence, std::cout.

One potential reason that the YouTube tutorial you’ve been looking at can just type cout is possibly because they’ve added a line like this somewhere in their code:

using namespace std.

This brings every name from the standard namespace into the global namespace, meaning you can do stuff like cout << "Hello, world!" << endl; but this is not considered best practice.

:slight_smile:

1 Like

To add to what @thepitycoder said, you can include individual parts of the standard library rather than the entire library. Consider:

#include <iostream>
using std::cout;
using std::endl;

int main() {
  cout << "Hello, World!" << endl;
  cout << "How are you?" << endl;
  return 0;
}

This is kind of like a friend asking you to come over to help change a tire. No need to rent a truck, and bring every tool in your garage when all you need are a jack, and a 4-way. :slightly_smiling_face:

1 Like

that’s very helpful, thank you but is there any difference between using s::dd cout<<"\n"; and cout""<<endl; ?

1 Like

Here’s a good explanation: https://www.geeksforgeeks.org/avoid-using-stdendl/
Thanks for asking! I didn’t realize the difference could have so much potential impact prior to reading the linked article.

1 Like

cstdio is kept in sync with iostream by default so you’d want to detach that as well
…I don’t see a significant difference when doing that here though, maybe I’m missing something, definitely don’t understand everything involved… maybe only for stdout/stderr/stdin? Didn’t see a difference there either though.