Skip to study material
← Question index

QUESTION 22 · Storage Classes

Explain the register storage class and its restrictions.

5 marks

Answer in brief

The register storage-class specifier suggests that a variable may be accessed frequently. It does not guarantee a particular hardware location or faster execution.

Declaration

register int i;

Main properties

  1. A register object at block scope has automatic storage duration.
  2. Its name has block scope and no linkage.
  3. It needs a valid value before it is read, just like an ordinary automatic local object.
  4. C11 does not permit taking its address with &.

Example explanation

The sample uses i as a small loop counter. It starts at 1, and each iteration adds i to sum. After i = 3 is processed, sum is 6. The loop would calculate the same result if register were omitted. A modern optimiser can choose efficient storage for ordinary variables too.

Restrictions and limitations

Do not write &i for a C11 register object. Consequently, a register int cannot directly be supplied to scanf as &i. The keyword is not a request for persistent storage and does not preserve a value across calls. It is also not a way to make shared updates safe between threads.

Complexity

The example performs one addition for each value from 1 through n in its general form, so the loop takes O(n) time and O(1) auxiliary space. Adding register does not change that growth rate. Optimisation of a particular build and algorithm complexity are separate matters.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int sum = 0;
    for (register int i = 1; i <= 3; ++i) { sum += i; }
    printf("Sum = %d\n", sum);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Sum = 6

Related exam questions

Related questions in the storage classes category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • September/October 2021 · Q1(b) · 8 marks

    If a variable has auto storage class by default, why was the auto keyword introduced in C? Explain.

    Paper P01 · page 1
  • July 2021 · Q2(c) · 5 marks

    List and explain storage classes in C.

    Paper P04 · page 1

View this category in the exam question bank →