JMU
Scope, Storage Duration, and Linkage in C
an Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Scope, Storage Duration, and Linkage
Declarations in C
Examples of Declarations
  // Type Specifier
  int i;

  // Type Specifier with a Modifier
  double grades[10];
  void error_exit();

  // Type Specifier with a Type Qualifier
  volatile int size;

  // Type Specifier with a Type Qualifier and Modifier
  const int *fp;
  
Scope in C
Scope in C (cont.)
#include <stdio.h>

int
main(void)
{
  n += 5;
  printf("%d\n", n);

  return 0;
}  
Scope in C (cont.)
#include <stdio.h>

int n = 0;

int
main(void)
{
  n += 5;
  printf("%d\n", n);

  return 0;
}
  
Scope in C (cont.)
#include <stdio.h>

int n = 0;

int
main(void)
{
  int n;

  n += 5;
  printf("%d\n", n);

  return 0;
}
  
Scope in C (cont.)
#include <stdio.h>

int n = 0;

int
main(void)
{
  int n = 1;

  n += 5;
  printf("%d\n", n);

  return 0;
}
 
Storage Duration in C
Using Storage Specifiers to Specify Duration
images/c_scope-duration.gif
Storage Duration in C (cont.)
#include <stdio.h>

int 
total(int x)
{
  static int total = 0;

  total += x;
  return total;
}

int 
main(void)
{
  total(5);
  total(10);
  printf("%d\n", total(15));
  return 0;
}
  
Linkage in C
Specifying Linkage for Functions
images/c_linkage_functions.gif
Specifying Linkage for Functions (cont.)
library.h
#ifndef LIBRARY_H
#define LIBRARY_H

int 
net_price(int price, int discount);

static int 
validate(int n);

#endif
  
library.c
#include "library.h"

int 
net_price(int price, int discount)
{
  return validate(price - discount);
}

static int 
validate(int n)
{
  if (n < 0) return 0;
  else       return n;
}
  
Specifying Linkage for Functions (cont.)
driver.c
#include "library.h"

int 
main(void)
{
  int i, j;

  i = validate(-5);
  j = net_price(6, 2);
}
  
Specifying Linkage for Variables
images/c_linkage_variables.gif
Specifying Linkage for Variables (cont.)
#include <stdio.h>

int n = 0;

int
main(void)
{
  extern int n;

  n += 5;
  printf("%d\n", n);

  return 0;
}  
Definitions in C
Examples of Definitions
The Following are at the Top Level of a Translation Unit
  // A definition (and implicit declaration) of a function
  int 
  max(int a, int b)
  {
    if (a > b) return a;
    else       return b;
  }

  // A definition (and implicit declaration) of a variable with file scope
  int total = 0;
  
Declarations vs. Definitions (cont.)
Declarations/Definitions of External Variables
Declarations/Definitions of External Variables (cont.)
The Following are at the Top Level of a Translation Unit
  // A declaration with external linkage
  extern int i;  

  // A definition with external linkage
  int total = 0; 

  // A definition with internal linkage
  static const char *university = "JMU";

  // A tentative definition with external linkage
  int  j;

  // A tentative definition with internal linkage
  static int  k;
  
A Common "Problem"
A Common "Problem": Attempt 1
account.h
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  #define ACCT_OK      0
  #define ACCT_INVALID 1
  #define ACCT_CLOSED  2

  int acct_status;
  #endif
  
account.c
  #include "account.h"

  // Use acct_status
  
driver.c
  #include "account.h"

  // Use acct_status
  
A Common "Problem": Attempt 1 (cont.)
A Common "Problem": Attempt 2
account.h
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  #define ACCT_OK      0
  #define ACCT_INVALID 1
  #define ACCT_CLOSED  2

  // Don't define acct_status since that caused the problem
  #endif
  
account.c
  #include "account.h"

  // Use acct_status
  
driver.c
  #include "account.h"

  // Use acct_status
  
A Common "Problem": Attempt 2 (cont.)
A Common "Problem": Attempt 3
account.h
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  #define ACCT_OK      0
  #define ACCT_INVALID 1
  #define ACCT_CLOSED  2

  // Don't define acct_status
  #endif
  
account.c
  #include "account.h"

  int acct_status;

  // Use acct_status
  
driver.c
  #include "account.h"

  // Use acct_status
  
A Common "Problem": Attempt 3 (cont.)
A Common "Problem": Attempt 4
account.h
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  #define ACCT_OK      0
  #define ACCT_INVALID 1
  #define ACCT_CLOSED  2

  extern int acct_status;
  #endif
  
account.c
  #include "account.h"

  // Use acct_status
  
driver.c
  #include "account.h"

  // Use acct_status
  
A Common "Problem": Attempt 4 (cont.)
A Common "Problem": Attempt 5
account.h
  #ifndef ACCOUNT_H
  #define ACCOUNT_H

  #define ACCT_OK      0
  #define ACCT_INVALID 1
  #define ACCT_CLOSED  2

  extern int acct_status;
  #endif
  
account.c
  #include "account.h"

  int acct_status = ACCT_OK;

  // Use acct_status
  
driver.c
  #include "account.h"

  // Use acct_status
  
A Common "Problem": Attempt 5 (cont.)