Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between {0} and ""?

Tags:

c

I would like to know if there is a difference between:

char s[32] = "";

and:

char s[32] = {0};

Thanks.

like image 778
md5 Avatar asked Apr 25 '12 13:04

md5


People also ask

What is the difference between 0 and 0+ in maths?

Is there any case when the sign before 0 matters? In mathematics +0 is a value just a little greater than 0 . Also, -0 is a value just a little lower than 0 . For example: n / Infinity would return +0 and n / -Infinity -0 (supposing that n is a real number greater than 0).

What is the difference between 0 and minus 0?

Here is how Wikipedia explains signed zeroes: “Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are identical.

Is zero Plus or minus?

Depending on local conventions, zero may be considered as being neither positive nor negative (having no sign or a unique third sign), or it may be considered both positive and negative (having both signs).

What's the difference between 0 and 1?

There are many differences between one and zero. For one, (haha, get it, one) zero is neither positive nor negative while one is positive. Another difference would be that one is the multiplicative identity, while zero is the additive identity. Also, any 1 to any power is 1, while any 0 to any power is 0.

How do you write 0 and O differently?

The standard way of telling a letter O from a number 0 in handwriting is to put a slash through the number. If you're writing a question or answer it is the "Preformatted text" button (looks like { } ). Or you can just indent the line 4 spaces. To make some inline c0d3 surround the text with backticks (``).

What is the difference between a number and zero?

Dear student, The difference of any given number and 0 is always the number itself. See in the example:- (2-0) =2 , (15-0) =15 so difference of any given number with 0 is the number itself.


3 Answers

No there is no difference between both declarations:

char bla[32] = {0};

and

char bla[32] = "";

See the relevant paragraph of the C Standard (emphasis mine):

(C99, 6.7.8p21) "If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration."

like image 86
ouah Avatar answered Oct 22 '22 15:10

ouah


In that case, there's no difference, both initialise all slots of the array to 0. In general, "" works only for char arrays (with or without modifications like const or unsigned), but {0} works for arrays of all numeric types.

In section 6.7.9 of the standard (n1570), point 21 reads

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

so even "" initialises the complete array.

like image 44
Daniel Fischer Avatar answered Oct 22 '22 17:10

Daniel Fischer


The result of both expressions is the same: an empty string. However, the first is more explicit, thus more readable.

like image 26
Péter Török Avatar answered Oct 22 '22 15:10

Péter Török