%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.
%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.
This is supposed to print the ASCII value of the character, as %d is the escape sequence for an integer. So the value given as argument of printf is taken as integer when printed.
char ch = 'a';
printf("%d", ch);
Same holds for printf("%d", '\0');, where the NULL character is interpreted as the 0 integer.
Finally, sizeof('\n') is 4 because in C, this notation for characters stands for the corresponding ASCII integer. So '\n' is the same as 10 as an integer.
It all depends on the interpretation you give to the bytes.
Why does printf ask for a char*?
How to correctly printf strings and characters with %s and %c - Stack Overflow
how do I set a char to this "█" and then use printf to print it
shell - What characters need to be escaped when using the printf command? - Unix & Linux Stack Exchange
Videos
picoc> int p = 4;
picoc> printf(p); ^ :49:9 can't set char* from int in argument 1 of call to printf()
I guess printf does not accept integers directly although it accepts char arrays?
But why is it asking for “char*”, a pointer?
If you try this:
#include<stdio.h>
void main()
{
char name[]="siva";
printf("name = %p\n", name);
printf("&name[0] = %p\n", &name[0]);
printf("name printed as %%s is %s\n",name);
printf("*name = %c\n",*name);
printf("name[0] = %c\n", name[0]);
}
Output is:
name = 0xbff5391b
&name[0] = 0xbff5391b
name printed as %s is siva
*name = s
name[0] = s
So 'name' is actually a pointer to the array of characters in memory. If you try reading the first four bytes at 0xbff5391b, you will see 's', 'i', 'v' and 'a'
Location Data
========= ======
0xbff5391b 0x73 's' ---> name[0]
0xbff5391c 0x69 'i' ---> name[1]
0xbff5391d 0x76 'v' ---> name[2]
0xbff5391e 0x61 'a' ---> name[3]
0xbff5391f 0x00 '\0' ---> This is the NULL termination of the string
To print a character you need to pass the value of the character to printf. The value can be referenced as name[0] or *name (since for an array name = &name[0]).
To print a string you need to pass a pointer to the string to printf (in this case name or &name[0]).
%c
is designed for a single character a char, so it print only one element.Passing the char array as a pointer you are passing the address of the first element of the array(that is a single char) and then will be printed :
s
printf("%c\n",*name++);
will print
i
and so on ...
Pointer is not needed for the %s because it can work directly with String of characters.
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?
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.
In most
printfimplementations¹, it's the byte values for\and%that are special and the POSIX specification could even be interpreted as requiring it as it requires theprintfutility to be an interface to theprintf(3)C function and notwprintf(3)for instance (like it requires%.3sto 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 each0x5cbyte 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
printfimplementations, in that locale,printf 'αb'doesn't outputαbbut byte0xa3(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 0000002Best is to avoid using (and even installing / making available) those locales as they cause all sorts of bugs and vulnerabilities of that sort.
Some
printfimplementations support options, and even those that don't are required to support--as the option delimiter. Soprintf --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.
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 '\"'
\"