Bird
Raised Fist0
Cprogramming~20 mins

Type modifiers in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Type Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of signed and unsigned char addition
What is the output of this C code snippet?
C
#include <stdio.h>

int main() {
    signed char a = 127;
    unsigned char b = 1;
    int c = a + b;
    printf("%d\n", c);
    return 0;
}
A128
B-128
C0
D255
Attempts:
2 left
💡 Hint
Remember that signed char max is 127 and unsigned char max is 255. The addition promotes both to int.
❓ Predict Output
intermediate
2:00remaining
Effect of long modifier on integer size
What is the output of this C code?
C
#include <stdio.h>

int main() {
    long int x = 2147483648L;
    printf("%ld\n", x);
    return 0;
}
A0
B-2147483648
C2147483648
DCompilation error
Attempts:
2 left
💡 Hint
Check the size of long int on a 32-bit or 64-bit system and the literal value.
🔧 Debug
advanced
2:00remaining
Why does this code print a negative number?
This code prints a negative number instead of the expected positive value. Why?
C
#include <stdio.h>

int main() {
    unsigned int a = 4000000000U;
    int b = a;
    printf("%d\n", b);
    return 0;
}
ABecause 4000000000 is too small for unsigned int.
BBecause unsigned int cannot hold values above 32767.
CBecause printf with %d expects unsigned int, but got int.
DBecause assigning unsigned int to int causes overflow and negative value due to two's complement.
Attempts:
2 left
💡 Hint
Think about how large values in unsigned int convert to signed int.
📝 Syntax
advanced
2:00remaining
Which option causes a compilation error?
Which of the following C declarations will cause a compilation error?
Along long int y;
Bunsigned signed int x;
Csigned int z;
Dunsigned int w;
Attempts:
2 left
💡 Hint
Check if 'unsigned signed' is a valid combination.
🧠 Conceptual
expert
2:00remaining
Size difference between short, int, and long with modifiers
On a typical 64-bit system, which statement about sizes of these types is true?
Ashort int < int < long int
Bshort int == int < long int
Cshort int == int == long int
Dshort int < int == long int
Attempts:
2 left
💡 Hint
Check typical sizes: short is 2 bytes, int and long are usually 4 or 8 bytes on 64-bit.

Practice

(1/5)
1. Which of the following type modifiers in C is used to store only non-negative values?
easy
A. short
B. signed
C. long
D. unsigned

Solution

  1. Step 1: Understand the meaning of 'unsigned'

    The 'unsigned' modifier means the variable can only hold zero or positive values, no negatives.
  2. Step 2: Compare with other modifiers

    'signed' allows negative and positive values, 'short' and 'long' change size but not sign.
  3. Final Answer:

    unsigned -> Option D
  4. Quick Check:

    unsigned = non-negative only [OK]
Hint: Unsigned means no negative numbers allowed [OK]
Common Mistakes:
  • Confusing 'signed' with 'unsigned'
  • Thinking 'short' affects sign
  • Assuming 'long' means unsigned
2. Which of the following is the correct way to declare a variable as a long unsigned integer in C?
easy
A. unsigned long int var;
B. int long unsigned var;
C. int unsigned long var;
D. unsigned int long var;

Solution

  1. Step 1: Recall correct order of type modifiers

    The standard order is 'unsigned long int' to declare a long unsigned integer.
  2. Step 2: Check syntax correctness

    unsigned long int var; follows the correct order and syntax; others mix order incorrectly.
  3. Final Answer:

    unsigned long int var; -> Option A
  4. Quick Check:

    unsigned long int = correct order [OK]
Hint: Use 'unsigned long int' order for clarity [OK]
Common Mistakes:
  • Mixing order of 'unsigned' and 'long'
  • Placing 'int' before 'unsigned'
  • Using invalid modifier order
3. What will be the output of the following C code?
unsigned short x = 65535;
unsigned short y = 1;
unsigned short z = x + y;
printf("%u", z);
medium
A. 0
B. 65536
C. 1
D. 65535

Solution

  1. Step 1: Understand the range of unsigned short

    Unsigned short typically stores values from 0 to 65535 (16-bit).
  2. Step 2: Calculate the sum and check overflow

    65535 + 1 = 65536, which exceeds max value, so it wraps around to 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Unsigned short overflow wraps to 0 [OK]
Hint: Unsigned short max is 65535; adding 1 wraps to 0 [OK]
Common Mistakes:
  • Expecting 65536 as output (overflow ignored)
  • Confusing signed and unsigned overflow
  • Using wrong printf format specifier
4. Identify the error in this C declaration:
unsigned signed int num;
medium
A. Missing variable name
B. Should use 'long' instead of 'signed'
C. Cannot combine 'unsigned' and 'signed' modifiers
D. No error, valid declaration

Solution

  1. Step 1: Understand modifier compatibility

    'unsigned' and 'signed' are opposite modifiers and cannot be combined together.
  2. Step 2: Check declaration validity

    Combining both causes a syntax error; variable name is present, so no missing name error.
  3. Final Answer:

    Cannot combine 'unsigned' and 'signed' modifiers -> Option C
  4. Quick Check:

    unsigned + signed = invalid [OK]
Hint: Don't mix 'unsigned' and 'signed' together [OK]
Common Mistakes:
  • Thinking both can be combined
  • Ignoring opposite meanings
  • Assuming 'signed' is default and can be added
5. You want to store a large positive number that fits in 64 bits but never needs to be negative. Which type modifier combination is best in C?
hard
A. short unsigned int
B. unsigned long long int
C. long int
D. signed long long int

Solution

  1. Step 1: Identify size and sign requirements

    64 bits means 'long long' type; only positive means 'unsigned'.
  2. Step 2: Match modifiers to requirements

    'unsigned long long int' fits large positive numbers without negatives.
  3. Final Answer:

    unsigned long long int -> Option B
  4. Quick Check:

    64-bit positive number = unsigned long long int [OK]
Hint: Use 'unsigned long long int' for large positive 64-bit numbers [OK]
Common Mistakes:
  • Choosing signed type when negatives not needed
  • Using 'short' which is too small
  • Ignoring 'unsigned' for positive-only data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard