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 (compiletime 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 (linktime concept) external: The variable is accessible from multiple files. internal: The variable is accessible from only the file in which it is declared. Duration (runtime 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 Comment int a = 5; definition file external process int b; declaration* file external process extern int c = 5; definition file external process extern int d; declaration file external process static int e = 5; definition file internal process static int f; definition file internal process void fun(int g) { definition block internal temporary Common int h = 5; definition block internal temporary Common int i; definition block internal temporary Common extern int j = 5; ILLEGAL extern int k; declaration block UNKNOWN process Rare static int l = 5; definition block internal process static int m; definition block internal process ... } * Special rule: If no definition appears in any other .c file, this becomes a definition. 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: Example # file1.c file2.c Result 1 int i = 5; int i = 5; Error. Multiple def of i. 2 int i = 5; int i; OK. 3 int i = 5; extern int i = 5 Error. Multiple def of i. 4 int i = 5; extern int i; OK. Normal. 5 int i = 5; static int i = 5; OK. 6 int i = 5; static int i; OK. 7 int i; int i; OK. Relies on special rule. 8 int i; extern int i = 5; OK. 9 int i; extern int i; OK. Relies on special rule. 10 int i; static int i = 5; OK. Relies on special rule. 11 int i; static int i; OK. Relies on special rule. 12 extern int i = 5; extern int i = 5; Error. Multiple def of i 13 extern int i = 5; extern int i; OK. 14 extern int i = 5; static int i = 5; OK. 15 extern int i = 5; static int i; OK 16 extern int i; extern int i; Error. No def of i. 17 extern int i; static int i = 5; Error. No def of i. 18 extern int i; static int i; Error. No def of i. 19 static int i = 5; static int i = 5; OK. Normal 20 static int i = 5; static int i; OK. Normal. 21 static int i; static int i; OK. Normal. Copyright © 2008 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?