Why does it print (null) instead of 5? | SoloLearn: Learn to code for FREE!


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)

1/18/2021 2:26:10 AM

Abhijnan Saraswat Gogoi

2 Answers

New Answer


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'}


The code doesn't work because you are using double quotes. Change them for single quotes and the format string to %c and it will work. In C we use single quotes for characters char x = '5'; printf("Hello Universe! %c", x); and double quotes for strings char x[] = "Five"; printf("Hello Universe" %s", x); A string in C is an array of null terminated characters.