Use of printf instead of cout | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Use of printf instead of cout

According with the course we use cout to represent, for example the "Hello World" sentence in screen, but, is not the same to use printf( "Hello World" ); ?

5th Nov 2016, 3:37 PM
Alejandro Fernández Trigo
Alejandro Fernández Trigo - avatar
5 Answers
+ 6
You can use them both, but for cout you need to use "<<" manipulator, and for printf you need to use formatter string. It looks like that: printf("Hello %s\n%c", "World", '!'); /*outputs "Hello World !" */ %s and %c - formatters, at first program outputs all before '%' symbol, %s says that you need to output string next (in params), when string is outputed then program returns to format string and outputs all until '%' symbol, and %c says that you need to output character (also in params). You can read more about formatters in the Internet, just google it. Formatters also can be composite, like %lc (long character) or %.8f (outputs up to 8 nums after floating point in float or double nums). Hope, I helped you. And sorry for bad english.
6th Nov 2016, 9:32 AM
Bivian
Bivian - avatar
+ 1
no problem, use #include <iostream> #include <stdio.h> using namespace std; int main() { printf("hello world"); return 0; }
5th Nov 2016, 3:47 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
Yeah, they just use a different library. <iostream> is standard library for C++ and handles input and output so cout, cin, etc. <stdio.h> is standard C library and also handles input and output as you get printf, scanf, etc..
5th Nov 2016, 7:09 PM
Jaque
Jaque - avatar
+ 1
Yeah I read it on My aunt's book about C. There was #include<stdio.h>. But It was on C language.
6th Nov 2016, 6:45 AM
Mashrafe
Mashrafe - avatar
- 1
Thanks for the answers, I know is not from C++, buy from C. The thing is, I'm actually learning C+/- which is a subset of C; so that's probably why I recognise orders like printf but I didn't know anything about cout. Anyway, thank you all.
6th Nov 2016, 8:23 AM
Alejandro Fernández Trigo
Alejandro Fernández Trigo - avatar