How does c compare character variable against string? -
the following code ok in c not in c++. in following code if
statement false. how c compares character variable against string?
int main() { char ch='a'; if(ch=="a") printf("confusion"); return 0; }
the following code ok in c
no, not @ all.
in code
if(ch=="a")
is trying compare value of ch
base address of string literal "a"
,. meaning-and-use-less.
what want here, use single quotes ('
) denote char
literal, like
if(ch == 'a')
note 1:
to elaborate on difference between single quotes char
literals , double quotes string literal s,
for char
literal, c11
, chapter §6.4.4.4
an integer character constant sequence of 1 or more multibyte characters enclosed in single-quotes, in
'x'
and, string literal, chapter §6.4.5
acharacter string literal sequence of 0 or more multibyte characters enclosed in double-quotes, in
"xyz"
.
note 2:
that said, note, recommend signature of main()
int main(void)
.
Comments
Post a Comment