WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Intermediate · question 23 of 100

What are the differences between signed and unsigned integers in C?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, there are two types of integers: signed and unsigned. The main difference between signed and unsigned integers is the range of values that they can represent and how those values are interpreted.

Signed Integers: Signed integers are used to represent both positive and negative values. Signed integers use a sign bit to indicate whether the value is positive or negative. The sign bit is typically the most significant bit of the integer, and it is set to 1 for negative values and 0 for positive values.

For example, a signed integer with 8 bits can represent values between -128 and 127. The range of values that a signed integer can represent depends on the number of bits used to store the integer.

    signed char x = -128;

In this example, the variable x is a signed char that is set to its minimum value of -128.

Unsigned Integers: Unsigned integers are used to represent only non-negative values. Unsigned integers do not use a sign bit to indicate the sign of the value. Instead, all bits in the integer are used to represent the value.

For example, an unsigned integer with 8 bits can represent values between 0 and 255. The range of values that an unsigned integer can represent also depends on the number of bits used to store the integer.

    unsigned char x = 255;

In this example, the variable x is an unsigned char that is set to its maximum value of 255.

One important thing to note is that signed and unsigned integers are not interchangeable, and care must be taken when performing operations on them. For example, if a signed integer with a negative value is added to an unsigned integer, the result may not be what is expected.

    signed int x = -1;
    unsigned int y = 1;
    int z = x + y; // z may not be what is expected

In this example, the variables x and y are different types of integers, and the result of adding them together and storing the result in a regular int variable z may not be what is expected due to the different ways that signed and unsigned integers are represented.

In summary, the main differences between signed and unsigned integers in C programming language are the range of values that they can represent and how those values are interpreted. Signed integers can represent both positive and negative values and use a sign bit to indicate the sign of the value, while unsigned integers can only represent non-negative values and do not use a sign bit. Care must be taken when performing operations on signed and unsigned integers, as they are not interchangeable.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics