Storage Class-Register variables are stored in the machine s high speed registers making frequently accessed variables register leads to faster and smaller programs restrictions only local variables and function parameters not global variables register variables is not kept in memory so can'ttake it address with PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Storage Class - Register • variables are stored in the machine’s high speed registers • making frequently accessed variables register leads to faster and smaller programs • restrictions • only local variables and function parameters • not global variables • register variables is not kept in memory so can’t take it address with &
Storage class. register restrictions can'tuse scanf/similar functions to read a value directly into a register variable Example /* Faster version of array searching function. * int table search(int a[l, register int n, register int t) register int 1 for(=0;i<n&&a[i]!=t;i++) ;/* search for matching value * return (i!= n)? i:-1 PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Storage Class - register • restrictions • can’t use scanf/similar functions to read a value directly into a register variable Example /* Faster version of array searching function. */ int table_search(int a[], register int n, register int t) { register int i; for (i = 0; i < n && a[i] != t; i++) ; /* search for matching value */ return (i != n) ? i : -1; }