c - Declaring a variable with both register and static storage classes

Hawknskola picture Hawknskola · Jan 9, 2016 · Viewed 7.7k times · Source

(ANSI-C 89) Hi, is it possible to declare a variable both static and register ? When ever I try to do this I just get as an error massage: multiple storage classes in declaration specifiers

This is the code:

#include <stdio.h>

void f1(static int i);
int main()
{
    static register int i;

    i = 5;
    f1(i);
}

void f1(static int i)
{
    static int y =6;
    y+=1;
    printf("\n Y=%d \n",y);
}

and th

Answer

Iharob Al Asimi picture Iharob Al Asimi · Jan 9, 2016

Both register and static are storage class specifiers, and at most one storage class specifier can be specified.

From the C11 Standard ISO/IEC 9899:2011:

6.7.1 Storage-class specifiers

Syntax

  1.      storage-class-specifier:
              typedef
              extern
              static
              _Thread_local
              auto
              register

Constraints

  1. At most, one storage-class specifier may be given in the declaration specifiers in a declaration, except that _Thread_local may appear with static or extern.120)

120)See ‘‘future language directions’’ (6.11.5).