Jump to content
  • Advertisement
Sign in to follow this  
PoisonedV

C++ other way of printf

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
in C++, you have the cout

#include <iostream.h>

void main(void)
{
cout << "Yo big !!! I'm " << 23 << " years old" << endl;
}

something like that.

if you want to read from the keyboard, you can use cin

int i, j;

cout << "enter two integers separated by a space: ";
cin >> i >> j;

For the other operations of IO, check the Ostream, Istream class...

Share this post


Link to post
Share on other sites
Quote:
Original post by PoisonedV
#include <stdio.h>

int main()
{
printf("Hello World!\n");
return 0;
}


hwo would i use it in that context?



#include <iostream>

int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}

Share this post


Link to post
Share on other sites
In C++, you use


#include <iostream>
// without .h, since that is pre-std C++

and then

int main()
{
std::cout << "Hello World!" << std::endl;
}


or


int main()
{
using namespace std;
cout << "Helllo World!" << endl;
}


std::cout is an object (a stream) which overloads the << operator to print to the console.
So when you do

cout << "bla";

it will resolve to

std::cout.operator<<(const char*)

or
std::cout.operator<<(std::string) or something like that.


Look up iostreams and overloading operators somewhere, this will help you understand what's going on.

BTW, the C functions are still availabe in C++, but they reside in headers like
<cstdio> instead of <stdio.h> and belong to the namespace std, so you must prefix them with std:: as well or use "using namespace std".

Share this post


Link to post
Share on other sites
std is the namespace for the standard library in C++. Namespaces are meant to organize your code. You can use them as well:



#include <iostream>

namespace myNamespace
{
void Hello() { std::cout << "Hello World" << std::endl; };
}

int main()
{
myNamespace::Hello();
// or alternatively

using namespace myNamespace;
Hello();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!