cout

is cout means for printf? because i am a new learner for C++ and having touch on this 'cout'.

i seen one like
cout << end1;


what is that about?
a new paragraph? pls help me.

cout and printf write to the same stream called stdout. So they do the same thing, although in C++ we prefer to use cout.

printf is function from the C standard library, whereas cout is an object from the C++ standaard library.

http://www.cplusplus.com/reference/iostream/cout/
ok, what about the 'end1' and '<<'?
it's endl and stands for endline (l as in line, not as in one ;).

And the << is an operator similar to + and =. In C, this operator is for bit-shifting, a rather low level operation. C++ uses this in a complete different way here and just means "streaming the endline into the cout - stream".

You could write operator<<(cout, endl) instead if you want, but that would look very weird :-D


Ciao, Imi.
Here's a tutorial, I'm sure there are better ones around. I'd recomend reading a C++ book though.
http://www.learncpp.com/cpp-tutorial/132-input-with-istream/
in addition to kbw i recommend you to read the whole documentation of the cpp-tutorial...

it is easy to understand for beginners and you can learn it within one day if you are encouraged enough... its fun:)...
cout and printf is same function but have different format,
prinf("HelloWorld"); cout<<"HelloWorld";

"<<" you can say this symbol means 'out'...so when you want to write something you use cout<<"sometxthere";

when you want to put variable in printf you write like this right
1
2
numb=10;
printf("your Number is:%d",numb);


different with cout...cout dont have format like %d %f or what...
you just write
1
2
3

numb=10;
cout<<"Your Number is: "<<numb


how about endl --> thats end line; same with \n in printf

okay bro?

@qabil,
Except std::cout is a stream, not a function. In one of the iostream classes they have overloaded operator<< to write on the stream given as an argument.

It's not a function at all. And endl is not the same as '\n' because std::endl also flushes the buffer.
Okok, thanks for all. I get it. :) Is that mean for beginner will use printf instead of cout? because when i look into my text book 'how to c program' , the content does not mentioned 'cout' at all.
Last edited on
how to c program


cout is only available in C++, but it is generally easier to understand and less problematic if you do things wrong.

Everything that is in C is also in C++ (roughly - there are some minor exceptions). But a lot of stuff in C++ is very easy to use but not in C.

So if you learn C, you have to stick to printf. If you learn C++ you have th choice and choosing "cout" over printf is recommended here ;)

Ciao, Imi.
Topic archived. No new replies allowed.