0

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!

Idan
  • 227
  • 3
  • 10
  • https://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value – Jason Brown Dec 06 '18 at 20:36
  • ╠ is 0xCC in codepage 437 which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714). You can find tons of questions about ╠ and 0xCC here on SO. [printf displays "-858993460" or "╠╠╠╠╠╠╠╠"; what are these?](https://stackoverflow.com/q/51771240/995714), [Displaying this symbol ╠ instead of desired characters](https://stackoverflow.com/q/12905027/995714) – phuclv Dec 07 '18 at 16:46

3 Answers3

3

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";
rustyx
  • 80,671
  • 25
  • 200
  • 267
2

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
ryyker
  • 22,849
  • 3
  • 43
  • 87
1

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

Kevin Kouketsu
  • 786
  • 6
  • 20