+ 2

Why does it print (null) instead of 5?

I tried running this c code just to see what happens: #include <stdio.h> int main() { char x="5"; //creating a string with 1 element printf("Hello Universe! %s",x); return 0; } Why does it print this: Hello Universe! (null)

18th Jan 2021, 2:26 AM
Abhijnan Saraswat Gogoi
Abhijnan Saraswat Gogoi - avatar
2 Answers
+ 5
You want char * x = "5"; %s expects a char pointer Also char x = "5"; Wouldn't really be valid as char will only hold 1 char and "5" is actually 2 due to the string null character it's actually "5\0" or { '5', '\0'}
18th Jan 2021, 2:39 AM
ChaoticDawg
ChaoticDawg - avatar