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" ); ?
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.
+ 1
no problem, use
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
printf("hello world");
return 0;
}
+ 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..
+ 1
Yeah I read it on My aunt's book about C. There was #include<stdio.h>. But It was on C language.
- 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.