You're right to be confused — in C, a string like "0000000"
is not empty, even though it looks like zeros.
If arr
contains "0000000"
(seven characters + a null terminator), then:
if (arr[0])
will return true, because arr[0]
is '0'
(ASCII 48), which is a non-zero value.
Only if arr[0] == '\0'
(null terminator), the string is considered empty.
if (arr[0] != '\0') { // string is not empty }
But if you want to check for a string of only '0'
characters, you need to loop or use:
if (strcmp(arr, "0000000") == 0) { // arr contains exactly "0000000" }
Let me know if you're checking for zero characters or numeric zero.
checkout :https://rkoots.github.io/styleguide/