When I intialize a string:
char pol[100];
and printf it, I get this weird ╠ sign 100 times. My question is how to intialize a string, print it and get only blanks? Thanks!
When I intialize a string:
char pol[100];
and printf it, I get this weird ╠ sign 100 times. My question is how to intialize a string, print it and get only blanks? Thanks!
╠
is a character with code 0xCC
in the OEM 850 codepage.
In debug builds, Visual C initializes uninitialized data with 0xCC
to help you detect uninitialized data.
You should initialize the array.
char pol[100] = {0};
or
char pol[100] = "test data";
When I intialize a string:...
The variable pol
has not been initialized, only declared, and as such, because of the definition of C string, may or may not even be a string
at this point. Using it in this state (eg. in a printf()
statement) is dangerous as it can invoke undefined behavior.
Change this:
char pol[100];
to This
char pol[100] = {0};//initializes entire array to `0`.
or this for example:
char pol[100] = {"temporary"};//initializes to |t|e|m|p|o|r|a|r|y|0|?|?|?|?|
//which is a null terminated string
You can do that with:
char pol[100];
pol[0] = '\0';
First characters will be a null character
.
If you want all of your array: char pol[100] = {0}
;
Test this way:
char test[100] = { 0 };
for (int i = 0; i < 100; i++)
printf("%c ", test[i]);
You can see the assembly here:
For x86 old compilers, that will be generate like:
push 100
push 0
lea eax, DWORD PTR _teste$[ebp]
push eax
call _memset
add esp, 12
On newer:
lea rdx, [rbp-112]
mov eax, 0
mov ecx, 12
mov rdi, rdx
rep stosq
mov rdx, rdi
mov DWORD PTR [rdx], eax
add rdx, 4
Test it on Godbolt.org selecting apropriate compiler