Course contentsShow
← C
Lesson 89 of 1,2192. Variables and TypesFree lesson

The `signed` keyword

The signed keyword

What you'll learn: How to use the signed keyword to explicitly declare variables that can hold both positive and negative numbers.

The Default Behavior

In C, when you create an integer variable using int, the compiler automatically assumes you want it to handle both positive and negative values. For example, int balance = -50; works perfectly fine. Because this is the default behavior, most programmers simply write int.

However, C provides a specific keyword for this: signed. While using it is often optional, it acts as a clear label telling the computer (and other programmers) that the number's "sign" (positive or negative) matters.

An Explicit Choice

Think of the signed keyword like a "Directional" sign on a elevator. An elevator doesn't just go up; it can go below ground level to floor -1 or -2. If you declare a variable as signed, you are reserving a tiny bit of the variable's memory to act as a "plus or minus" flag.

If you have a 1-byte variable, it has 8 bits. When you use signed, one of those bits is dedicated to tracking whether the number is positive or negative. This means you have slightly less room for huge positive numbers, but you gain the ability to drop below zero.

Using signed in Code

You will most commonly see signed used with the char type, but it can be used with int, short, and long as well.

#include <stdio.h>

int main() {
    // These two lines do exactly the same thing
    int normalTemperature = -10;
    signed int explicitTemperature = -10;

    // Explicitly marking a character as signed
    signed char floorLevel = -2;

    printf("The basement level is %d\n", floorLevel);

    return 0;
}

In the example above, signed int and int are identical. Why use the extra word? Sometimes, being explicit makes your intentions clearer. If you are writing a program where some numbers must be positive (unsigned) and others must allow negatives, using the signed keyword side-by-side with unsigned makes your code much easier to read and less prone to mistakes.

Key takeaway

The signed keyword explicitly allows a variable to store negative numbers, though it is usually the default for integers in C.