🌐
cppreference.com
en.cppreference.com › c › io › fprintf
printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com
#include <inttypes.h> #include <stdint.h> #include <stdio.h> int main(void) { const char* s = "Hello"; printf("Strings:\n"); // same as puts("Strings"); printf(" padding:\n"); printf("\t[s]\n", s); printf("\t[%-10s]\n", s); printf("\t[%*s]\n", 10, s); printf(" truncating:\n"); printf("\t%.4s\n", s); printf("\t%.*s\n", 3, s); printf("Characters:\t%c %%\n", 'A'); printf("Integers:\n"); printf("\tDecimal:\t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4,-4); printf("\tHexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); printf("\tOctal:\t\t%o %#o %#o\n", 10, 10, 4); printf("Floating-point:\n"); printf("
🌐
cppreference.com
en.cppreference.com › cpp › io › c › fprintf
std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
#include <cinttypes> #include <cstdint> #include <cstdio> #include <limits> int main() { const char* s = "Hello"; std::printf("Strings:\n"); // same as std::puts("Strings:"); std::printf("\t[s]\n", s); std::printf("\t[%-10s]\n", s); std::printf("\t[%*s]\n", 10, s); std::printf("\t[%-10.*s]\n", 4, s); std::printf("\t[%-*.*s]\n", 10, 4, s); std::printf("Characters:\t%c %%\n", 'A'); std::printf("Integers:\n"); std::printf("\tDecimal: \t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4,-4); std::printf("\tHexadecimal:\t%x %x %X %#x\n", 5,10,10, 6); std::printf("\tOctal: \t%o %#o %#o\n", 10, 10, 4);
🌐
7-Zip Documentation
documentation.help › C-Cpp-Reference › printf.html
printf - C/C++ Reference - Documentation & Help
cppreference.com > Standard C I/O > printf · printf · Syntax: #include <cstdio> int printf( const char *format, ... ); The printf() function prints output to stdout, according to format and other arguments passed to printf(). The string format consists of two types of items - characters that ...
🌐
7-Zip Documentation
documentation.help › CppReference › printf.html
printf - C++ Reference - Documentation & Help
The printf() function prints output to STDOUT, according to format and other arguments passed to printf(). The string format consists of two types of items - characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed.
🌐
Lsu
ld2016.scusa.lsu.edu › cppreference › en › c › io › fprintf.html
printf, fprintf, sprintf, snprintf, printf_s, fprintf_s - cppreference.com
#include <stdio.h> int main(void) { printf("Strings:\n"); const char* s = "Hello"; printf("\t.s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c %%\n", 65); printf("Integers\n"); printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); printf("Octal:\t%o %#o %#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E %e\n", 1.5, 1.5); printf("Hexadecimal:\t%a %A\n", 1.5, 1.5); }
🌐
University of Chicago
naipc.uchicago.edu › 2014 › ref › cppreference › en › c › io › fprintf.html
printf, fprintf, sprintf, snprintf - cppreference.com
#include <stdio.h> int main() { printf("Strings:\n"); const char* s = "Hello"; printf("\t.s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c %%\n", 65); printf("Integers\n"); printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); printf("Octal:\t%o %#o %#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E %e\n", 1.5, 1.5); printf("Hexadecimal:\t%a %A\n", 1.5, 1.5); } Output: Strings: . H
🌐
Liu
cppreference-45864d.gitlab-pages.liu.se › en › cpp › io › c › printf.html
std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
September 10, 2023 - #include <cinttypes> #include <cstdint> #include <cstdio> #include <limits> int main() { const char* s = "Hello"; std::printf("Strings:\n"); // same as std::puts("Strings:"); std::printf("\t[s]\n", s); std::printf("\t[%-10s]\n", s); std::printf("\t[%*s]\n", 10, s); std::printf("\t[%-10.*s]\n", 4, s); std::printf("\t[%-*.*s]\n", 10, 4, s); std::printf("Characters:\t%c %%\n", 'A'); std::printf("Integers:\n"); std::printf("\tDecimal: \t%i %d %.6i %i %.0i %+i %i\n", 1, 2, 3, 0, 0, 4,-4); std::printf("\tHexadecimal:\t%x %x %X %#x\n", 5,10,10, 6); std::printf("\tOctal: \t%o %#o %#o\n", 10, 10, 4);
🌐
Lsu
ld2016.scusa.lsu.edu › cppreference › en › cpp › io › c › printf.html
std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
#include <cstdio> #include <limits> #include <cstdint> #include <cinttypes> int main() { std::printf("Strings:\n"); const char* s = "Hello"; std::printf("\t.s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); std::printf("Characters:\t%c %%\n", 65); std::printf("Integers\n"); std::printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); std::printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); std::printf("Octal:\t%o %#o %#o\n", 10, 10, 4); std::printf("Floating point\n"); std::printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); std::printf("Padding:\t.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
🌐
Cplusplus
cplusplus.com › reference › cstdio › printf
Printf
printf · function · <cstdio> int printf ( const char * format, ... ); Print formatted data to stdout · Writes the C string pointed by format to the standard output (stdout).
Find elsewhere
🌐
Lsu
ld2014.scusa.lsu.edu › cppreference › en › c › io › fprintf.html
printf, fprintf, sprintf, snprintf - cppreference.com
#include <stdio.h> int main(void) { printf("Strings:\n"); const char* s = "Hello"; printf("\t.s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); printf("Characters:\t%c %%\n", 65); printf("Integers\n"); printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); printf("Octal:\t%o %#o %#o\n", 10, 10, 4); printf("Floating point\n"); printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); printf("Padding:\t.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); printf("Scientific:\t%E %e\n", 1.5, 1.5); printf("Hexadecimal:\t%a %A\n", 1.5, 1.5); } Output: Strings:
🌐
Cppreference
cppreference.com › User:Ybab321 › printf_format
User:Ybab321/printf format - cppreference.com
The memory-writing conversion specifier %n is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked printf_s family of functions.
🌐
University of Chicago
naipc.uchicago.edu › 2015 › ref › cppreference › en › cpp › io › c › fprintf.html
std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
#include <cstdio> int main() { std::printf("Strings:\n"); const char* s = "Hello"; std::printf("\t.s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); std::printf("Characters:\t%c %%\n", 65); std::printf("Integers\n"); std::printf("Decimal:\t%i %d %.6i %i %.0i %+i %u\n", 1, 2, 3, 0, 0, 4, -1); std::printf("Hexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); std::printf("Octal:\t%o %#o %#o\n", 10, 10, 4); std::printf("Floating point\n"); std::printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); std::printf("Padding:\t.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); std::printf("Scientific:\t%E %e\n", 1.5, 1.5); std::printf
🌐
Lsu
ld2010.scusa.lsu.edu › cppreference › wiki › c › io › printf
c:io:printf
char name[20] = "Bob"; int age = 21; printf( "Hello %s, you are %d years old\n", name, age );
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › printf-printf-l-wprintf-wprintf-l
printf, _printf_l, wprintf, _wprintf_l | Microsoft Learn
October 26, 2022 - The printf function formats and prints a series of characters and values to the standard output stream, stdout. If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments.
Top answer
1 of 9
342

C++23 Update

We now finally have std::print as a way to use std::format for output directly:

#include <print>
#include <string>

int main() {
    // ...
    std::print("Follow this command: {}", myString);
    // ...
}

This combines the best of both approaches.

Original Answer

It's compiling because printf isn't type safe, since it uses variable arguments in the C sense1. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won't give you the results you want. It's actually undefined behaviour, so anything at all could happen.

The easiest way to fix this, since you're using C++, is printing it normally with std::cout, since std::string supports that through operator overloading:

std::cout << "Follow this command: " << myString;

If, for some reason, you need to extract the C-style string, you can use the c_str() method of std::string to get a const char * that is null-terminated. Using your example:

#include <iostream>
#include <string>
#include <stdio.h>

int main()
{
    using namespace std;

    string myString = "Press ENTER to quit program!";
    cout << "Come up and C++ me some time." << endl;
    printf("Follow this command: %s", myString.c_str()); //note the use of c_str
    cin.get();

    return 0;
}

If you want a function that is like printf, but type safe, look into variadic templates (C++11, supported on all major compilers as of MSVC12). You can find an example of one here. There's nothing I know of implemented like that in the standard library, but there might be in Boost, specifically boost::format.


[1]: This means that you can pass any number of arguments, but the function relies on you to tell it the number and types of those arguments. In the case of printf, that means a string with encoded type information like %d meaning int. If you lie about the type or number, the function has no standard way of knowing, although some compilers have the ability to check and give warnings when you lie.

2 of 9
48

Use myString.c_str() if you want a C-like string (const char*) to use with printf.