Variables and Data Types

A variable is a named, typed storage location; the type fixes how many bytes are used, how the bit pattern is interpreted, and the range of values it can hold, which is why signed n-bit integers span −2^(n−1) to 2^(n−1)−1.

Key formulas & points

Skim these first — then read the full notes below.

  • Primitive types: int, float, char, bool — size is platform-dependent
  • Type promotion widens the smaller type before arithmetic
  • const prevents modification; volatile disables certain optimisations

Topic details

Introduction

This programming-fundamentals topic establishes how data is represented in memory. You learn primitive types and their sizes, how integers and floating-point numbers are encoded, how implicit type promotion works in mixed expressions, and how qualifiers like const and volatile modify a variable’s behaviour.

Key relations & formulas

Formulas (Indian textbook notation)

  • sizeof(type)bytesallocatedinCC++sizeof(type) → bytes allocated in \frac{C}{C}++

Formulas (Indian textbook notation)

  • rangeofnbitsignedinteger2(n1)to2(n1)1range of n-bit signed integer \approx -2^(n-1) to 2^(n-1) - 1

Formulas (Indian textbook notation)

  • ASCII:(char)65AASCII: (char)65 → 'A'

Notation and sign conventions

Relation 1 —
sizeofsizeof

Formulas (Indian textbook notation)

  • sizeof(type)bytesallocatedinCC++sizeof(type) → bytes allocated in \frac{C}{C}++
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
range of n-bit signed integer \approx -2^

Formulas (Indian textbook notation)

  • rangeofnbitsignedinteger2(n1)to2(n1)1range of n-bit signed integer \approx -2^(n-1) to 2^(n-1) - 1
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
ASCII:ASCII:

Formulas (Indian textbook notation)

  • ASCII:(char)65AASCII: (char)65 → 'A'
Write this relation with symbols exactly as in Let Us C — Yashavant Kanetkar before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

A type is a contract: it tells the compiler how to allocate storage and how to read the bits back. Signed integers use two’s-complement, giving the asymmetric range where the negative side has one more value than the positive; floating-point uses a sign, exponent and mantissa, trading exact representation for range, which is why 0.1 is not stored exactly. When operands of different types meet, the language promotes the narrower type to avoid losing information before computing. Understanding overflow (wrapping past the maximum) and truncation (assigning float to int) prevents a whole class of subtle bugs.

Assumptions and validity limits

State assumptions explicitly before using any relation for variables and data types — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In Programming Fundamentals viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in Programming Fundamentals papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to variables and data types.
4. Use equation 1:
sizeofsizeof
.
5. Use equation 2:
range of n-bit signed integer \approx -2^
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Variables and Data Types appears in all software development. In Indian it software curricula this topic is tested because it connects theory to core programming concepts in C/C++.
GATE and semester exams often combine variables and data types with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use variables and data types?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students assume int is always 4 bytes (it is platform-dependent), forget two’s-complement asymmetry when computing ranges, and expect floating-point equality to be exact. Ignoring integer overflow and implicit narrowing conversions causes wrong answers in trace questions.

Quick revision checklist

Before attempting variables and data types problems, confirm you can:
1. Primitive types: int, float, char, bool — size is platform-dependent
2. Type promotion widens the smaller type before arithmetic
3. const prevents modification; volatile disables certain optimisations
Revise the solved examples in Let Us C — Yashavant Kanetkar and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

Signed integer range

Problem

What is the value range of an 8-bit signed integer?

Solution

Range = −2^(8−1) to 2^(8−1) − 1 = −128 to 127. The extra negative value comes from two’s-complement, where zero occupies a positive-side code.

Conceptual check — Variables and Data Types

Problem

In a Programming Fundamentals semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of variables and data types." What should a complete answer include?

Exams & GATE

Know the size of int/float/double on 32-bit vs 64-bit for GATE CS.

📖 Standard books (India)

  • Let Us CYashavant Kanetkar

    Read: Syllabus unit

    First programming book for many Indian students