Printing int, float and string values with printf in C++
This code defines a main function where the int and float variables are constants and the text variable is not. Prints the values number, realNumber, and text and then returns 0.

Categories:
less than a minute
This code defines a main function where the int and float variables are constants and the text variable is not. Prints the values number, realNumber, and text and then returns 0.
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
constexpr int number = 123;
constexpr float realNumber = 3.146;
string text = "Hello World";
printf("Number: %d ", number);
printf("Pi value: %.2f ", realNumber);
printf("Text: %s ", text.c_str());
return 0;
}
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.