unsigned vs signed in C
What does unsigned or signed mean in terms of variables?
The unsigned
keyword before a variable in C means that the variable can only contain positive numbers.
The signed
keyword means that a variable can contain negative numbers and also positive numbers.
Here’s a simpler way of understanding it: Do you remember that Math you did in primary school?
They told you that if the number is positive, then you don’t have to put the plus (+) sign infront of it. That is unsigned
. It’s only for positive numbers because they don’t have the sign in front of them.
In the case of negative numbers, you have to, you must, it’s compulsory, it’s mandatory for you to put the sign minus (-) in front of it. That’s why it makes sense that a variable that can contain negative numbers should be given the keyword signed
.
Note: A signed
variable can contain both negative and positive numbers.