There is a big bitfall here in the line printf("%s", hasSign + "\n" );
"\n"
is actually a pointer to const char (const char *)
which is array of two characters: [0] = '\n'
, [1] = '\0'
so when you add hasSign
to it, it is actually doing pointer arithmetic.
To make it clear by example, let's say hasSign
equals 0 so hasSign + "\n"
evaluates to 0 + "\n"
which is like &"\n"[0]
= pointer to '\n'
so a new line will printed in this case.
But when hasSign
equals 1 so hasSign + "\n"
evaluates to 1 + "\n"
which is like &"\n"[1]
= pointer to '\0'
which is a null character or in other words 'nothing' and therefore nothing will be printed in this case.
To your question:
How can I print a newline when I am printing the value of hasSign like in the upper code?
you can do it like printf("%d\n", hasSign);