%d prints an integer: it will print the ascii representation of your character. What you need is %c:

printf("%c", ch);

printf("%d", '\0'); prints the ascii representation of '\0', which is 0 (by escaping 0 you tell the compiler to use the ascii value 0.

printf("%d", sizeof('\n')); prints 4 because a character literal is an int, in C, and not a char.

Answer from peoro on Stack Overflow
🌐
University of Surrey
personalpages.surrey.ac.uk › r.bowden › C › printf.html
Format Codes for printf
June 9, 2023 - int printf(const char *format, ...) int fprintf(FILE *stream, const char *format, ...) int sprintf(char *string, const char *format, ...) The functions return the number of characters written, or a negative value if an error occurred. The format string is of the form ·
Discussions

Why does printf ask for a char*?
TBH, this is right time to say RTFM Did you even try to Google and look at some example code? Every function has a signature ie return type, function name and then types of parameters it expects. C is compiled language (though looks like you are using some kind of interpreter) and before calling the function, compiler checks if the parameters passed are of correct data type. Languages like python fail after making the call but compiled languages fail at compile time ie no executable code is generated at all because code is faulty. More on reddit.com
🌐 r/C_Programming
18
0
February 1, 2022
How to correctly printf strings and characters with %s and %c - Stack Overflow
To print a string you need to pass ... to printf (in this case name or &name[0]). ... Sign up to request clarification or add additional context in comments. ... This is a nice explanation. 2022-05-01T06:39:56.793Z+00:00 ... Save this answer. ... Show activity on this post. ... is designed for a single character a char, so ... More on stackoverflow.com
🌐 stackoverflow.com
how do I set a char to this "█" and then use printf to print it
A character literal is defined by a character in single quotes ('a', '_'). To print a character using printf, use the %c format specifier. More on reddit.com
🌐 r/cpp_questions
12
1
February 24, 2021
shell - What characters need to be escaped when using the printf command? - Unix & Linux Stack Exchange
Are there any other instances where I would need to escape a character for it to be interpreted literally? ... In the format argument of printf, only the % and \ characters are special (no, " is not special and \" is unspecified per POSIX). More on unix.stackexchange.com
🌐 unix.stackexchange.com
January 16, 2019
🌐
Cplusplus
cplusplus.com › reference › cstdio › printf
Printf
int printf ( const char * format, ... ); Print formatted data to stdout · Writes the C string pointed by format to the standard output (stdout).
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › format-specification-syntax-printf-and-wprintf-functions
Format Specification Syntax: `printf` and `wprintf` Functions | Microsoft Learn
Conversions for character types char and wchar_t are specified by using c or C, and single-byte and multi-byte or wide character strings are specified by using s or S, depending on which formatting function is being used. Character and string arguments that are specified by using c and s are interpreted as char and char* by printf family functions, or as wchar_t and wchar_t* by wprintf family functions.
🌐
cppreference.com
en.cppreference.com › c › io › fprintf
printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com
As with all bounds-checked functions, printf_s, fprintf_s, sprintf_s, and snprintf_s are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>. ... The format string consists of ordinary byte characters ...
Find elsewhere
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › printf.3.html
printf(3) - Linux manual page
#include <stdio.h> int printf(const char *restrict format, ...); int fprintf(FILE *restrict stream, const char *restrict format, ...); int dprintf(int fd, const char *restrict format, ...); int vprintf(const char *restrict format, va_list ap); int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap); int vdprintf(int fd, const char *restrict format, va_list ap); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): dprintf(), vdprintf(): Since glibc 2.10: _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE
🌐
Sololearn
sololearn.com › en › Discuss › 2165306 › c-printing-a-char
C - Printing a char
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › printf-in-c
printf in C - GeeksforGeeks
The printf() function is defined inside <stdio.h> header file. ... formatted_string: It is a string that specifies the data to be printed. It may also contain a format specifier as a placeholder to print the value of any variable or value. args...: These are the variable/values corresponding to the format specifier. ... Returns the number of characters printed after successful execution.
Published   October 18, 2025
🌐
Embedded Artistry
embeddedartistry.com › home › blog › printf a limited number of characters from a string
printf a Limited Number of Characters from a String - Embedded Artistry
December 15, 2021 - // Only 5 characters printed const char * mystr = "This string is definitely longer than what we want to print."; printf("Here are first 5 chars only: %.5s\n", mystr);
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-print-using-printf
How to print % using printf()? - GeeksforGeeks
April 4, 2025 - Flag character · Field width · Precision · Length modifier · Conversion specifier · The main thing to note in the standard is the below line about conversion specifier. A '%' is written. No argument is converted. The complete conversion specification is'%%'. So, we can print "%" using "%%" c · /* Program to print %*/ #include <stdio.h> int main() { printf("%%"); getchar(); return 0; } Output ·
🌐
cppreference.com
en.cppreference.com › cpp › io › c › printf
std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
Strings: [ Hello] [Hello ] [ Hello] [Hell ] [Hell ] Characters: A % Integers: Decimal: 1 2 000003 0 +4 -4 Hexadecimal: 5 a A 0x6 Octal: 12 012 04 Floating point: Rounding: 1.500000 2 1.30000000000000004440892098500626 Padding: 01.50 1.50 1.50 Scientific: 1.500000E+00 1.500000e+00 Hexadecimal: 0x1.8p+0 0X1.8P+0 Special values: 0/0=-nan 1/0=inf Variable width control: right-justified variable width: ' x' left-justified variable width : 'x ' (the last printf printed 41 characters) Fixed-width types: Largest 32-bit value is 4294967295 or 0xffffffff
🌐
Reddit
reddit.com › r/cpp_questions › how do i set a char to this "█" and then use printf to print it
r/cpp_questions on Reddit: how do I set a char to this "█" and then use printf to print it
February 24, 2021 -
	char variable = "";
	printf ("%d", variable);

I tried this, and it gives me this error

main.cpp: In function ‘int main()’:
main.cpp:14:18: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
   14 |  char variable = "█";
      |                  ^~~~~
      |                  |
      |                  const char*

what should I do to make this work?

🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_printf.htm
C Library - printf() function
Below is the illustration of the C library printf() function. #include <stdio.h> int main() { int num = 10; char str[] = "Hello"; printf("Integer: %d, String: %s\n", num, str); return 0; }
🌐
W3Schools
w3schools.com › c › c_variables_format.php
C Format Specifiers
To print different types in a single printf() function, you can use the following: int myNum = 15; char myLetter = 'D'; printf("My number is %d and my letter is %c", myNum, myLetter); Try it Yourself » · Tip: Each format specifier must match the correct type of value.
🌐
PHP
php.net › manual › en › function.printf.php
PHP: printf - Manual
function printf(string $format, mixed ...$values): int · Produces output according to format. format · The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result and conversion specifications, each of which results in fetching its own parameter.
Top answer
1 of 2
17

In the format argument of printf, only the % and \ characters are special (no, " is not special and \" is unspecified per POSIX).

But, two important notes.

  1. In most printf implementations¹, it's the byte values for \ and % that are special and the POSIX specification could even be interpreted as requiring it as it requires the printf utility to be an interface to the printf(3) C function and not wprintf(3) for instance (like it requires %.3s to truncate to 3 bytes and not 3 characters).

    In some character encodings including BIG5 and GB18030, there are hundreds of characters that contain the encoding of backslash, and to escape those for printf, you'd need to insert a \ before each 0x5c byte within the encoding of those characters!

    For instance in BIG5-HKSCS, as used for instance in the zh_HK.big5hkscs (Hong Kong) locale, all of Ěαжふ㘘㙡䓀䨵䪤么佢俞偅傜兝功吒吭园坼垥塿墦声娉娖娫嫹嬞孀尐岤崤幋廄惝愧揊擺暝枯柦槙檝歿汻沔涂淚滜潿瀙瀵焮燡牾狖獦珢珮琵璞疱癧礒稞穀笋箤糭綅縷罡胐胬脪苒茻莍蓋蔌蕚螏螰許豹贕赨跚踊蹾躡鄃酀酅醆鈾鎪閱鞸餐餤駹騱髏髢髿鱋鱭黠﹏𠗫𠰺𣘀𦖭𦰡𧃸𨜏𩄼𪀔 contain byte 0x5c (which is also the encoding of \).

    With most printf implementations, in that locale, printf 'αb' doesn't output αb but byte 0xa3 (the first byte of the encoding of α) followed by the BS character (the expansion of \b).

    $ LC_ALL=zh_HK.big5hkscs luit
    $ locale charmap
    BIG5-HKSCS
    $ printf 'αb' | LC_ALL=C od -tx1 -tc
    0000000  a3  08
            243  \b
    0000002
    

    Best is to avoid using (and even installing / making available) those locales as they cause all sorts of bugs and vulnerabilities of that sort.

  2. Some printf implementations support options, and even those that don't are required to support -- as the option delimiter. So printf -- won't output -- but likely report an error about a missing format argument. So if you can't guarantee your format won't start with -, you have to use the -- option delimiter:

     printf -- "$escaped_format" x y...
    

In any case, if you want to print arbitrary strings, you'd use:

printf '%s\n' "$data" # with terminating newline
printf %s "$data"     # without

There's no character that is special in the string passed to %s (though note that with the exception of the printf builtin of zsh, you can't pass the NUL character in any of printf arguments).

Note that while the canonical way to enter a literal \ is with \\ and a literal % with %%, on ASCII-based systems, you can also use \134 and \45 and with some printf implementations \x5c, \x25, or \x{5c}, \x{25}, or (even on non-ASCII systems): \u005c, \u0025 or \u{5c}, \u{25}.


¹ yash's printf builtin being the only exception I am aware of.

2 of 2
3

From the manual:

$ man printf
...
   printf FORMAT [ARGUMENT]...
...
   FORMAT controls the output as in C printf.  Interpreted sequences are:

This lists several interpreted sequences. The following are those where the character itself needs to be escaped.

   \"     double quote
   \\     backslash
   %%     a single %

I tested these three in bash, and they behaved as expected. As per man bash, this implementation of printf uses the "standard printf(1) format specifications" as above, in addition to a few more that aren't relevant here.


However, other shells such as zsh implement printf slightly differently. Here, the double quote shouldn't be escaped.

$ printf '"'
"   
$ printf '\"'
\"
🌐
Polytechnique
lix.polytechnique.fr › ~liberti › public › computing › prog › c › C › FUNCTIONS › format.html
printf format identifiers. - LIX (Polytechnique)
The format identifier describes the expected data. The identifier is the character that ends Here is a list of the format identifers as used in 'printf' ,'sprintf' ,'fprintf' and 'scanf'.