printf
can be accessed if you #include <stdio.h>
. I like using printf
because of the format specifiers, and it just feels nicer than doing std::cout << "something\n";
.
#include <stdio.h>
#include <cstdlib> // to use rand()
int main() {
int randNum = rand()%10 + 1;
printf("Hey, we got %d!\n", randNum);
return 0;
}
vs
#include <iostream>
#include <cstdlib> // to use rand()
using std::cout;
int main() {
int randNum = rand()%10 + 1;
cout << "Hey, we got " << randNum << "!\n";
return 0;
}
But a user in a discord server that I am in said it was bad practice because "printf is unsafe, use std::cout".
A second user said it was bad because it's not type safe (the first user did mention type safety but not in depth).
The second user said,
Typesafety is enforced by the compiler. However by using a variadic function, the compiler cannot tell the type of the arguments at runtime; it can't know them in advance. The function needs some way to tell what type of arguments to expect, and the
printf
family of C functions did this through the format string specifiers.
So I'm looking for other alternatives.
If there are none I guess I'll just stick to std::cout