DOC PREVIEW
Princeton COS 217 - C Variable Declarations and Definitions

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Princeton University COS 217: Introduction to Programming Systems C Variable Declarations and Definitions Variable declaration is a statement that informs the compiler of the name, type, scope, linkage, and duration of the variable. A variable definition is a declaration that causes the compiler to allocate memory. Scope (compile-time concept) File: The variable is accessible within the file in which it is declared, from the point of declaration to the end of the file. Block: The variable is accessible within the block in which it is declared, from the point of declaration to the end of the block. Linkage (link-time concept) External: The variable is accessible from multiple files. Internal: The variable is accessible from only the file in which it is declared. Duration (run-time concept) Temporary: The variable exists only during the execution of the function or block in which it is declared. Physically, the variable’s value is stored in the runtime Stack. Process: The variable exists throughout the entire process. Physically, the variable’s value is stored in the Data Section (if the programmer specifies an initial value) or the BSS Section (if the programmer does not specify an initial value). The variable’s value is initialized at program startup. If in the BSS section, its initial value is 0. C Code Decl/Def Scope Linkage Duration Location int a = 5; definition file external process Data int b; definition* file external process BSS extern int c = 5; definition file external process Data extern int d; declaration file external process ??? static int e = 5; definition file internal process Data static int f; definition file internal process BSS void fun(int g) { definition block internal temporary Stack int h = 5; definition block internal temporary Stack int i; definition block internal temporary Stack extern int j = 5; ILLEGAL extern int k; declaration block ??? process ??? static int l = 5; definition block internal process Data static int m; definition block internal process BSS ... } * Special rule: If a definition of b appears another .c file, then this becomes a declaration. Page 1 of 2Examples of Global Variable Declarations and Definitions Suppose a program consists of file1.c and file2.c (only). Consider these combinations of global variable declarations and definitions: file1.c file2.c Result Reasonable combinations: 1 static int i = 5; static int i = 5; static def / static def => OK 2 static int i = 5; static int i; static def / static def => OK 3 static int i; static int i; static def / static def => OK 4 int i = 5; extern int i; def / decl => OK 5 int i; extern int i; def / decl => OK Less reasonable combinations: 6 int i = 5; int i; def / decl => OK (by special rule) 7 int i; int i; def / decl => OK (by special rule) 8 int i = 5; static int i = 5; def / static def => OK 9 int i = 5; static int i; def / static def => OK 10 int i; static int i = 5; def / static def => OK 11 int i; static int i; def / static def => OK Erroneous combinations: 12 int i = 5; int i = 5; def / def => error 13 extern int i; extern int i; decl / decl => error 14 extern int i; static int i = 5; decl / static def => error 15 extern int i; static int i; decl / static def => error Copyright © 2010 by Robert M. Dondero, Jr. Page 2 of


View Full Document

Princeton COS 217 - C Variable Declarations and Definitions

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download C Variable Declarations and Definitions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Variable Declarations and Definitions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Variable Declarations and Definitions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?