18. DECLARATIONS.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Programming Languages and Paradigms The C Programming Language.
Chapter 7: User-Defined Functions II
Structure of a C program
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 6: User-Defined Functions
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Learners Support Publications Classes and Objects.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Chapter 9 Scope, Lifetime, and More on Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 18: Declarations Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 18 Declarations.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
C++ Lesson 1.
Eine By: Avinash Reddy 09/29/2016.
The Three Attributes of an Identifier
C Functions -Continue…-.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
The Three Attributes of an Identifier
Declarations Chapter 18 Copyright © 2008 W. W. Norton & Company.
Instructor: Ioannis A. Vetsikas
Chapter 9 Scope, Lifetime, and More on Functions
Advanced Programming Basics
Pointers, Dynamic Data, and Reference Types
Local Variables, Global Variables and Variable Scope
CSC 253 Lecture 7.
Classes and Objects.
Programming Language C Language.
Submitted By : Veenu Saini Lecturer (IT)
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
The Three Attributes of an Identifier
Pointers, Dynamic Data, and Reference Types
C Programming Lecture-17 Storage Classes
Storage Classes.
Presentation transcript:

18. DECLARATIONS

Declaration Syntax • A declaration tells the C compiler about the properties of variables and/or functions. • Declarations have the form declaration-specifiers declarators ; • Each declarator may be followed by an initializer. • Examples of declarations: static float x, y, *p; const char computer[] = "Dell laptop"; extern unsigned long int a[10], j; extern double f(double a);

Declaration Syntax • A declaration specifier is one of the following: Storage class (auto, static, extern, or register) Type specifier (for example, long, short, unsigned, int, or float) Type qualifier (const or volatile) • At most one storage class is allowed; if present, it should come first. There may be several type specifiers and/or qualifiers; these may appear in any order.

Storage Class of a Variable • The choice of storage class affects a variable’s storage duration, scope, and linkage. • The storage duration of a variable is the portion of program execution during which storage for the variable exists. Choices: Automatic — Storage for the variable is allocated when the surrounding block is executed; storage is deallocated when the block terminates, causing the variable to lose its value. Static — The variable has a permanent storage location, hence it retains its value throughout the execution of the program.

Storage Class of a Variable • The scope of a variable is the portion of the program text in which the variable can be referenced. Choices: Block scope — The variable is visible from its point of declaration to the end of the enclosing block. File scope — The variable is visible from its point of declaration to the end of the enclosing file. • The linkage of a variable determines whether it may be shared by more than one file in the same program. Choices: External — The same variable may be shared by several files. Internal — The variable is restricted to a single file. None — The variable belongs to a single function and can’t be shared at all.

Storage Class of a Variable • A variable’s default storage duration, scope, and linkage depend on where the variable is declared: Inside a block: The variable’s storage duration is automatic and it has block scope. At the outermost level of a program: The variable’s storage duration is static, it has file scope, and its linkage is external.

Variables with External Linkage • The extern class makes it possible for several source files to share the same variable. One file should contain a definition of the variable; the other files contain declarations that refer to the variable. • Suppose that the variable to be shared is named i. One of the files declares i without the word extern: int i; (If desired, an initial value for i may be supplied.) The other files contain declarations that are identical except for the presence of the word extern: extern int i; The compiler allocates storage for i only once; the other declarations of i are treated as references to the original variable i.

Variables with External Linkage • Warning: When declarations of an external variable appear in different files, the compiler cannot check that the declarations are the same. One file may contain the declaration int i; while another file contains the declaration extern long int i; An error of this kind may cause the program to behave unpredictably.

Storage Class of a Function • Function declarations (and definitions) may include a storage class; the only options are extern and static. extern indicates external linkage, allowing the function to be called from other files. static indicates internal linkage - the function may be called only within the same file. If no storage class is specified, the function is assumed to have external linkage.

Storage Class of a Function • Examples: extern int f(int i); static int g(int i); int h(int i); • Advantages of using static: Simplifies maintenance by indicating that a function is not called from outside a file. Allows functions in different files to have the same name.

Type Qualifiers • There are only two type qualifiers: const and volatile. • const is used to create “read-only” variables: const int n = 10; const int months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; • Advantages of using const: Documents that the value of a variable will not change. The compiler will check that the program doesn’t inadvertently attempt to change the value of the object.

Declarators • In the simplest case, a declarator can be a single identifier: int i, j; • Declarators may also include a preceding *, indicating “pointer to”: int *p; • A declarator may be followed by square brackets to indicate that it represents an array: int a[10]; • A declarator may be followed by a parameter list to indicate that it represents a function: int f(int i);

Initializers A declarator that represents a variable may be followed by an initializer: int i = 0, *p = &i, a[5] = {1,2,3,4,5}; • An initializer for an array, structure, or union must contain only constant expressions, never variables or function calls: #define N 2 int powers[5] = {1, N, N*N, N*N*N, N*N*N*N}; • An initializer for a variable with static storage duration is also limited to constant expressions: #define FIRST 1 #define LAST 100 static int i = LAST - FIRST + 1;

Initializers • If a variable has automatic storage duration (and is not an array, structure, or union), its initializer can be any expression of the appropriate type—the expression need not be constant: int f(int n) { int last = n - 1; … } • What is the initial value of a variable if we don’t explicitly initialize it? Variables whose storage duration is static are initialized to 0 by default. Automatic variables have no default initial value.