LearnFrenzy
Sign in Take a free test

C Programming · Topic 9 of 23

Data Types

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Introduction & Syntax

Data Types · Study material

Data types in C decide how much memory a value takes, which values it can hold and what operations mean. Choosing the wrong one gives some of C's subtlest bugs: -1 < 1u is false, 200 + 100 stored in an unsigned char is 44, and signed overflow is undefined behaviour. This page explains the types, their sizes and ranges, how C converts between them, and what C23 adds – then gives you 40 practice questions checked on a real compiler.

On this page

  • The basic types, their sizes on 64-bit systems and their ranges
  • Signed vs unsigned, wrap-around and overflow
  • Integer promotion and the usual arithmetic conversions
  • <stdint.h>, printf formats, and C23's bool, _BitInt and checked arithmetic

The basic types

C guarantees only minimum sizes (char ≥ 8 bits, short and int ≥ 16, long ≥ 32, long long ≥ 64). On 64-bit Linux and macOS (the "LP64" model) the sizes are:

#include <stdio.h>

int main(void) {
    printf("char %zu, short %zu, int %zu, long %zu, long long %zu\n",
           sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(long long));
    printf("float %zu, double %zu, pointer %zu\n", sizeof(float), sizeof(double), sizeof(void *));
    return 0;
}

Output

char 1, short 2, int 4, long 8, long long 8
float 4, double 8, pointer 8
TypeSize (LP64)Typical rangeprintf
char / signed char / unsigned char1−128…127 or 0…255%c / %d / %u
short2−32 768…32 767%hd
int4−2 147 483 648…2 147 483 647%d
long8 (4 on Windows)±9.2 × 10¹⁸%ld
long long8±9.2 × 10¹⁸%lld
float / double4 / 8~7 / ~15 significant digits%f
size_t80…18 446 744 073 709 551 615%zu

Windows uses "LLP64": long stays 4 bytes even on 64-bit. Code that needs an exact size should use the types from <stdint.h>.

Ranges from limits.h

#include <limits.h>
#include <stdint.h>
#include <stdio.h>

int main(void) {
    printf("INT_MIN   = %d\nINT_MAX   = %d\n", INT_MIN, INT_MAX);
    printf("UINT_MAX  = %u\n", UINT_MAX);
    printf("LLONG_MAX = %lld\n", LLONG_MAX);
    printf("SIZE_MAX  = %zu\n", SIZE_MAX);
    return 0;
}

Output

INT_MIN   = -2147483648
INT_MAX   = 2147483647
UINT_MAX  = 4294967295
LLONG_MAX = 9223372036854775807
SIZE_MAX  = 18446744073709551615

Signed, unsigned and overflow

Unsigned arithmetic is defined to wrap around modulo 2ⁿ: 0 − 1 is UINT_MAX. Signed overflow is undefined behaviour – INT_MAX + 1 may give a negative number, or the optimiser may assume it never happens. Whether plain char is signed is up to the platform (signed on x86 and Apple, unsigned on most ARM Linux).

Before any arithmetic, types smaller than int (char, short) are promoted to int – so adding two unsigned chars cannot wrap, but storing the result back into one can.

#include <stdio.h>

int main(void) {
    unsigned char c = 250;
    c += 10;                        /* unsigned arithmetic wraps: (250 + 10) mod 256 */
    unsigned u = 0;
    u--;                            /* wraps to UINT_MAX */
    unsigned char x = 200, y = 100;
    int sum = x + y;                /* both are promoted to int first: no wrap */
    printf("%u %u %d\n", c, u, sum);
    return 0;
}

Output

4 4294967295 300

The usual arithmetic conversions

When an operator mixes types, both operands are converted to a common type first: to the floating type if either is floating, otherwise to the larger integer type – and if a signed and an unsigned type of the same rank meet, the signed one becomes unsigned. That is why comparing an int with an unsigned or a size_t goes wrong for negative numbers (compilers warn with -Wsign-compare).

#include <stdio.h>

int main(void) {
    int a = -1;
    unsigned b = 1;
    /* In a < b the int is converted to unsigned: -1 becomes 4294967295 */
    printf("(unsigned) a = %u\n", (unsigned) a);
    printf("as unsigned: %s\n", (unsigned) a < b ? "a < b" : "a >= b");
    printf("as long long: %s\n", (long long) a < (long long) b ? "a < b" : "a >= b");
    return 0;
}

Output

(unsigned) a = 4294967295
as unsigned: a >= b
as long long: a < b

Fixed-width types: stdint.h

int8_t, int16_t, int32_t, int64_t and their uint… versions have exactly that many bits. Print them with the macros from <inttypes.h>, such as PRId32 and PRIu64, which expand to the right format on every platform.

#include <inttypes.h>
#include <stdio.h>

int main(void) {
    int32_t  a = -42;                       /* exactly 32 bits */
    uint64_t big = UINT64_C(1) << 40;       /* exactly 64 bits, unsigned */
    int8_t   small = INT8_MIN;              /* -128 */
    printf("%" PRId32 " %" PRIu64 " %" PRId8 "\n", a, big, small);
    return 0;
}

Output

-42 1099511627776 -128

What C23 adds

bool, true and false are now keywords (no <stdbool.h> needed, and sizeof(true) is 1). _BitInt(N) gives integers of exactly N bits. <stdckdint.h> adds ckd_add, ckd_sub and ckd_mul, which report overflow instead of causing undefined behaviour. C23 also requires two's complement for signed integers.

#include <limits.h>
#include <stdckdint.h>
#include <stdio.h>

int main(void) {
    bool ok = true;                          /* C23: bool, true and false are keywords */
    unsigned _BitInt(12) reading = 4095;     /* C23: an unsigned integer of exactly 12 bits */
    reading += 1;                            /* wraps to 0, like any unsigned type */

    int r;
    bool overflow = ckd_add(&r, INT_MAX, 1); /* C23: checked addition, no undefined behaviour */
    printf("%d %d %zu %d\n", ok, (int) reading, sizeof(true), overflow);
    return 0;
}

Output

1 0 1 1

Key points to remember

  • sizeof(char) is 1 by definition; other sizes vary – use sizeof and <stdint.h>.
  • Unsigned overflow wraps; signed overflow is undefined.
  • char and short are promoted to int in expressions.
  • Mixing signed and unsigned converts the signed value to unsigned.
  • Match printf formats to types: %zu for size_t, %lld for long long, PRIu64 for uint64_t.

Frequently asked questions

What is the size of int in C?

At least 16 bits by the standard; 4 bytes (32 bits) on every mainstream desktop and mobile platform today.

When should I use unsigned types?

For bit patterns, sizes and counts that can never be negative (size_t), and when you need defined wrap-around. Avoid them for ordinary arithmetic that may go below zero.

Is char signed or unsigned?

It is implementation-defined. Use signed char or unsigned char (or int8_t/uint8_t) when it matters.

Where can I learn data types step by step?

Read Data Types in C, Type Conversion in C and printf and scanf in C, then practise with the exercises below.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links