Declaring a char array

hello,

is this:

  char msg[64];
  msg[0] = '\0';

the same as

char msg[64] = "";

?

I want to null terminate my char declaration (msg) before I start concatenating other chars into it.

Thanks

Yes, those two are functionally identical.

2 Likes

Note that if msg is an automatic variable (i.e., a non-static local variable), it will not be filled with zeroes in the first case. Only the first char will have a value of zero (after the msg[0] = '\0'; statement). In the second case, the compiler will fill the entire array with zeroes.

Whether this matters to your application is up to you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.