Use
printf("%s", str_a);
It will print your str_a. But keep in mind that every char*-string in C has to be terminated by a \0
-character. If it is not terminated, then everything that is in the ram after the string is also printed and accessed.
In the best case this results in a SIGSEGV, and your programm terminates. In the worst case somebody can use this to print for example plaintext password data stored in the RAM right beside the string you tried to print.
Read about "buffer overflow" and "stack overflow".
If you define the string by
const char* str = "Hello World";
then C will automatically add the \0
character for you, and the actual length of the string is 12 Bytes (for 11 Characters).
But if you go by strcpy
or by reading it from stdin
or from any untrusted source (like network) then you have a security leak.
But just for testing printf("%s", str_a)
is just fine.
Other parameters for printf would be:
d or i Signed decimal integer 392
u Unsigned decimal integer 7235
o Unsigned octal 610
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (uppercase) 7FA
f Decimal floating point, lowercase 392.65
F Decimal floating point, uppercase 392.65
e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
g Use the shortest representation: %e or %f 392.65
G Use the shortest representation: %E or %F 392.65
a Hexadecimal floating point, lowercase -0xc.90fep-2
A Hexadecimal floating point, uppercase -0XC.90FEP-2
c Character a
s String of characters sample
p Pointer address b8000000
n Nothing printed.
(source http://www.cplusplus.com/reference/cstdio/printf/)
You use these parameters like:
printf("%i: %f and i am a Character: [%a]", 10, 4.4, (char)a);