MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 2 of 11

Block 1 · Unit 2Data Types, Operators and Expressions11 min · Section 2 of 11

2.2Data Types and Their Sizes

Covers IGNOU MCS-201 §2.4, 2.5

Before C can store a value, it must know what kind of value it is: a whole number, a character or a number with a fraction. The data type decides how much memory is used, which values fit, and which operations are allowed. This section covers the basic types, their real sizes on today’s computers, and the modifiers that change them.

What a data type tells the compiler

C has four basic data types:

TypeStoresExample values
charOne character (stored as a small integer code)'A', '7', '\n'
intWhole numbers-42, 0, 2026
floatReal numbers, single precision (about 6 significant digits)3.14f, -0.5f
doubleReal numbers, double precision (about 15 significant digits)3.14159265358979

C also has void (no value, used with functions) and, since C99, _Bool for true/false values.

Sizes and ranges on a real system

The C standard fixes only minimum sizes. The actual size depends on the compiler and processor. Almost all 64-bit Linux and macOS systems use the same model, called LP64, where long and pointers are 64 bits.

TypeStandard minimum16-bit DOS compilers64-bit Linux / macOS (LP64)Range on LP64
char8 bits1 byte1 byte−128 to 127 (signed) or 0 to 255
short16 bits2 bytes2 bytes−32,768 to 32,767
int16 bits2 bytes4 bytes−2,147,483,648 to 2,147,483,647
long32 bits4 bytes8 bytesabout ±9.22 × 10¹⁸
long long64 bits—8 bytesabout ±9.22 × 10¹⁸
float—4 bytes4 bytesabout ±3.4 × 10³⁸, 6 significant digits
double—8 bytes8 bytesabout ±1.8 × 10³⁰⁸, 15 significant digits

For signed integer types of nn bits, the range is

−2n−1 to 2n−1−1-2^{n-1} \text{ to } 2^{n-1} - 1

and for unsigned types it is 00 to 2n−12^{n} - 1. The extra bit that a signed type uses for the sign is used for magnitude in an unsigned type, which is why the unsigned maximum is about twice as large.

Modifiers: short, long, signed, unsigned

The words short, long, signed and unsigned modify the basic types:

CombinationMeaning
short int, long int, long long intSmaller or larger integer types (int may be omitted: short, long)
unsigned int, unsigned charNon-negative only; the whole range is used for magnitude
signed charA character type that is definitely signed
long doubleExtended-precision floating type

There is no short float, unsigned float or unsigned double. Separately, C has true type qualifiers: const (the value must not be changed after initialisation) and volatile (the value may change outside the program’s control, for example hardware registers).

Key takeaways

  • The basic types are char, int, float and double; sizes depend on the system.
  • On 64-bit Linux and macOS: int 4 bytes, long 8 bytes, float 4 bytes, double 8 bytes.
  • Signed nn-bit range: −2n−1-2^{n-1} to 2n−1−12^{n-1}-1; unsigned: 00 to 2n−12^n-1.
  • short, long, signed and unsigned modify integer types; const and volatile are the qualifiers.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the range of an 8-bit signed char?

Show answer

Answer: C. −128 to 127

For n=8n = 8, the range is −27-2^7 to 27−12^7 - 1, which is −128 to 127. The upper limit 128 would need a ninth bit.

Question 2 · multiple choice

On a typical 64-bit Linux or macOS system, how many bytes does int occupy?

Show answer

Answer: B. 4

Modern compilers use 32-bit int. The value 2 comes from old 16-bit DOS compilers; 8 is the size of long on these systems.

Question 3 · multiple choice

What is the largest value of a 16-bit unsigned int?

Show answer

Answer: D. 65,535

Unsigned nn-bit values range from 0 to 2n−12^n - 1, and 216−1=65,5352^{16} - 1 = 65{,}535. 32,767 is the signed maximum.

Question 4 · multiple choice

Which declaration is valid C?

Show answer

Answer: C. long double x;

long double is a valid extended-precision type. unsigned applies only to integer types, and there is no short double.

Question 5 · multiple choice

Which of these is a type qualifier in C?

Show answer

Answer: A. const

The qualifiers are const and volatile (plus newer ones). short, long and unsigned are specifiers that change the type itself.

Question 6 · short answer

Using the formula, find the range of a 32-bit signed integer and of a 32-bit unsigned integer.

Show worked answer

Signed: −231-2^{31} to 231−12^{31} - 1. Since 231=2,147,483,6482^{31} = 2{,}147{,}483{,}648, the range is −2,147,483,648 to 2,147,483,647. Unsigned: 00 to 232−12^{32} - 1. Since 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296, the range is 0 to 4,294,967,295.

↑ ↓ move↵ openTab switch type