Interview Q&A · C · 10 questions in this topic

Data Types & Variables — C Interview Q&A

All C topics

The C type system — sizes, signedness, conversions, and the fixed-width types portable code relies on. Part of Data Types & Operators · 202 questions in the C bank.

What are the fundamental data types and their typical sizes?

Answer

Core types are char, short, int, long, long long, float, double, _Bool/bool plus signed/unsigned modifiers. The standard guarantees minimum ranges (int is at least 16 bits) rather than exact sizes — exact sizes are implementation-defined, so portable code checks with sizeof or uses fixed-width types from <stdint.h>.

On typical 64-bit Linux/macOS: char 1, short 2, int 4, long 8, long long 8, float 4, double 8, pointers 8. On 64-bit Windows, long is 4 (LLP64).

Key points

  1. 1char — 1 byte; plain-char signedness is implementation-defined.
  2. 2int — at least 16 bits, almost always 32 bits today.
  3. 3Use int32_t / uint64_t from <stdint.h> when exact width matters.

Example

Example.cc
#include <stdio.h>
#include <stdint.h>
int main(void) {
    printf("%zu %zu %zu\n", sizeof(int), sizeof(long), sizeof(void*));
    int32_t exact = 42; /* exactly 32 bits, everywhere */
    return 0;
}

What is the difference between signed and unsigned integers? What goes wrong mixing them?

Answer

Signed overflow is undefined behavior; unsigned arithmetic wraps modulo 2^N (defined but often still a logic bug). In mixed expressions the signed operand converts to unsigned (the usual arithmetic conversions), so -1 < 1u is false — -1 becomes a huge unsigned value.

Guideline: use signed ints for quantities by default, unsigned for bit manipulation and genuinely modular arithmetic, and compile with -Wsign-compare.

Example

Example.cc
#include <stdio.h>
int main(void) {
    /* -1 converts to unsigned: comparison is false! */
    if (-1 < 1u)
        printf("true\n");
    else
        printf("false (signed converted to unsigned)\n");
    return 0;
}

What are the integer promotion and usual arithmetic conversion rules?

Answer

Small integer types (char, short, bit-fields, bool) promote to int (or unsigned int if int cannot hold them) before any arithmetic — so char + char is computed as int. Then the usual arithmetic conversions unify the operands to a common type: int to unsigned int, to long, to unsigned long, to float/double in a fixed rank order.

These silent promotions explain why `uint8_t a, b; a + b` is int, and why mixing signed/unsigned surprises even seniors.

Example

Example.cc
#include <stdint.h>
uint8_t a = 200, b = 100;
/* promoted to int BEFORE adding: result is int 300, not wrapped uint8_t */
int sum = a + b;

💡 Small types compute as int is the one-line rule behind most promotion questions.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits