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!

2

3 Answers 3

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";
Sign up to request clarification or add additional context in comments.

3 Comments

Nothing to do with ASCII
wow thats some nit-picking. But ok, updated.
437 is more likely, but I suppose that would be picky
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

Comments

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

5 Comments

char pol[100] = {0} initializes all elements of the array to 0
However, char pol[100] = {1} initializes the 1st element to 1, and the rest to 0. Keep that in mind.
please add the ; after the declaration with initialization char pol[100] = {0};, thanks
First character it's a null character. What about all the rest of the characters?
Like @UnholySheep said, initialize all array with 0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.