Outline (Cont) History of CC Program Structure

Total Page:16

File Type:pdf, Size:1020Kb

Outline (Cont) History of CC Program Structure Introduction to C Outline #include <stdio.h> II. Program Basics A. Program skeleton preprocessor directives int main () global declarations { functions local declarations printf(“Welcome to CS 1621!\n”); statements } B. Comments and Documentation C. Names (identifiers) reserved words Outline (cont) Outline (cont) II. Program Basics (cont) II. Program Basics (cont) D. Variable declarations F. Formatted input/output 1. Memory allocation 1. Files 2. Printf (monitor output) 2. Atomic types a. format strings void, int, float, char field specifications E. Constants b. data list 1. literal 3. Scanf (keyboard input) a. format strings 2. defined b. address list 3. memory 4. Prompting for Input History of C C Program Structure 1960: ALGOL (ALGOrithmic Language) Preprocessor Directives • Program defined by: – global declarations 1967: BCPL (Basic Combined Programming Global Declarations – function definitions Language) Function Definitions • May contain preprocessor 1970: B programming language (typeless) int main () { directives 1972: C: BCPL plus B with types Local Declarations • Always has one function 1978: Kernighan + Ritchie standard for C Statements named main, may contain others 1989: ANSI standard for C } 1 Parts of a Program Preprocessor Directives • Begin with # #include <stdio.h> Preprocessor Directive • Instruct compiler to perform some int x; Global Declaration transformation to file before compiling int main () { int y; Local Declaration • Example: #include <stdio.h> Function printf("Enter x and y: "); – add the header file stdio.h to this file scanf(&x,&y); Statements – .h for header file printf("Sum is %d\n",x+y); } – stdio.h defines useful input/output functions Declarations Functions • Global • Consists of header and body – visible throughout program – header: int main () – describes data used throughout program – body: contained between { and } • Local • starts with location declarations – visible within function • followed by series of statements – describes data used only in function • More than one function may be defined • Functions are called (invoked) - more later Main Function Comments • Every program has one function main • Text between /* and */ • Header for main: int main () • Used to “document” the code for the human • Program is the sequence of statements reader between the { } following main • Ignored by compiler (not part of program) • Statements are executed one at a time from • Have to be careful the one immediately following to main to – comments may cover multiple lines the one before the } – ends as soon as */ encountered (so no internal comments - /* An /* internal */ comment */) 2 Comment Example Documentation #include <stdio.h> • Global - start of program, outlines overall /* This comment covers solution, may include structure chart * multiple lines • Module - when using separate files, * in the program. indication of what each file solves */ • Function - inputs, return values, and logic int main () /* The main header */ { used in defining function /* No local declarations */ • Add documentation for key (tough to understand) comments printf(“Too many comments\n”); • Names of variables - should be chosen to be } /* end of main */ meaningful, make program readable Syntax of C Identifier • Rules that define C language • Names used for objects in C – Specify which tokens are valid • Rules for identifiers in C: – Also indicate the expected order of tokens – first char alphabetic [a-z,A-Z] or underscore (_) • Some types of tokens: – has only alphabetic, digit, underscore chars – reserved words: include printf int ... – first 31 characters are significant – identifiers: x y ... – cannot duplicate a reserved word – literal constants: 5 ‘a’ 5.0 ... – case (upper/lower) matters – punctuation: { } ; < > # /* */ Reserved Words Valid/Invalid Identifiers • Identifiers that already have meaning in C Valid Invalid sum 7of9 • Examples: c4_5 x-name – include, main, printf, scanf, if, else, … A_NUMBER name with spaces – more as we cover C language longnamewithmanychars 1234a TRUE int _split_name AXYZ& 3 Program Execution Variables • Global declarations set up • Named memory location • Function main executed • Variables declared in global or local – local declarations set up declaration sections – each statement in statement section executed • Syntax: Type Name; • executed in order (first to last) • Examples: • changes made by one statement affect later statements int sum; float avg; char dummy; Variable Type Variable Name • Indicates how much memory to set aside for • Legal identifier the variable • Not a reserved word • Also determines how that space will be • Must be unique: interpreted – not used before • Basic types: char, int, float – variable names in functions (local declarations) – specify amount of space (bytes) to set aside considered to be qualified by function name – what can be stored in that space – variable x in function main is different from x – what operations can be performed on those vars in function f1 Multiple Variable Declarations Variable Initialization • Can create multiple variables of the same • Giving a variable an initial value type in one statement: • Variables not necessarily initialized when int x, y, z; declared (value is unpredictable - garbage) is a shorthand for • Can initialize in declaration: int x; • Syntax: Type Name = Value; int y; • Example: int z; int x = 0; - stylistically, the latter is often preferable 4 Initialization Values Multiple Declaration Initialization • Literal constant (token representing a value, • Can provide one value for variables like 5 representing the integer 5) initialized in one statement: • An expression (operation that calculates a int x, y, z = 0; value) • Each variable declared and then initialized • Function call with the value • The value, however specified, must be of the correct type Type Standard Types • Set of possible values • Atomic types (cannot be broken down) – defines size, how values stored, interpreted – void • Operations that can be performed on those – char possible values – int • Data types are associated with objects in C – float, double (variables, functions, etc.) • Derived types – composed of other types Literal Constants Void Type • Sequences of characters (tokens) that • Type name: void correspond to values from that type • Possible values: none -35 is the integer -35 • Operations: none 3.14159 is the floating pointer number 3.14159 • Useful as a placeholder ‘A’ is the character A • Can be used to initialize variables 5 Integer Type Integer Types/Values • Type name: Type Bytes Bits Min Val Max Val – int – short int short int 2 16 -32768 32767 – long int int 4 32 -2147483648 2147483647 • Possible values: whole numbers (within given ranges) as in 5, -35, 401 long int 4 32 -2147483648 2147483647 • Operations: arithmetic (addition, subtraction, multiplication, …), and others Why Limited? Two’s Complement • With a fixed number of bits, only a certain Integers: number of possible patterns positive number: 0, number in binary • 16 bits, 65,536 possible patterns 97 in binary 1*64 + 1*32 + 1*1 (1100001) – 32768 negative numbers pad with leading zeroes (0 00000001100001) - 16 bits zero: 0, all zeroes – 1 zero negative number: 1, (inverse of number + 1) – 32767 positive numbers -97 (1, 111111110011110 + 1) • Overflow: attempt to store a value to large 1 111111110011111 in a variable (40000 in short int) Unsigned Integers Integer Literal Constants • Type: unsigned int Syntax: • No negative values 1 or more digits • unsigned int: Optional leading sign (+ or -) – possible values: 0 to 65536 Optional l or L at the end for long • Representation: binary number Optional u or U for unsigned Examples: 5, -35, 401, 4010L, -350L, 2000UL 6 Floating-Point Type Floating-Point Representation • Type names: • float: 4 bytes, 32 bits – float • double: 8 bytes, 64 bits – double • long double: 10 bytes, 80 bits – long double • Representation: • Possible values: floating point numbers, 5.0 – magnitude (some number of bits) plus exponent -3.5, 4.01 (remainder of bits) • Operations: arithmetic (addition, – 3.26 * 10^4 for 32600.0 subtraction, multiplication, …), and others Floating-Point Limitations Floating-Point Literals • Maximum, minimum exponents • Syntax: – Zero or more digits, decimal point, then zero or – maximum possible value (largest positive more digits (at least one digit) magnitude, largest positive exponent) – Whole numbers also treated as float – minimum value (largest negative magnitude, – Optional sign at start largest positive exponent) – Can be followed by e and whole number (to – can have overflow, and underflow represent exponent) – f or F at end for float • Magnitude limited – l or L at end for long double – cannot differentiate between values such as • Examples: 5, .5, 0.5, -1.0, 2.1e+3, 5.1f 1.00000000 and 1.00000001 Character Type Character Literals • Type name: char • Single key stroke between quote char ‘ • Possible values: keys that can be typed at • Examples: ‘A’, ‘a’, ‘b’, ‘1’, ‘@’ the keyboard • Some special chars: • Representation: each character assigned a value (ASCII values), 8 bits – ‘\0’ - null char – A - binary number 65 – ‘\t’ - tab char – a - binary number 97 – ‘\n’ - newline char – b - binary number 98 – ‘\’’ - single quote char – 2 - binary number 50 – ‘\\’ - backslash char 7 String Literals Constants • No string type (more later) • Literal constants - tokens representing • Contained between double quote chars (“) values from type • Examples: • Defined constants “” - null string – syntax: #define Name Value
Recommended publications
  • Chapter 5 Names, Bindings, and Scopes
    Chapter 5 Names, Bindings, and Scopes 5.1 Introduction 198 5.2 Names 199 5.3 Variables 200 5.4 The Concept of Binding 203 5.5 Scope 211 5.6 Scope and Lifetime 222 5.7 Referencing Environments 223 5.8 Named Constants 224 Summary • Review Questions • Problem Set • Programming Exercises 227 CMPS401 Class Notes (Chap05) Page 1 / 20 Dr. Kuo-pao Yang Chapter 5 Names, Bindings, and Scopes 5.1 Introduction 198 Imperative languages are abstractions of von Neumann architecture – Memory: stores both instructions and data – Processor: provides operations for modifying the contents of memory Variables are characterized by a collection of properties or attributes – The most important of which is type, a fundamental concept in programming languages – To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility 5.2 Names 199 5.2.1 Design issues The following are the primary design issues for names: – Maximum length? – Are names case sensitive? – Are special words reserved words or keywords? 5.2.2 Name Forms A name is a string of characters used to identify some entity in a program. Length – If too short, they cannot be connotative – Language examples: . FORTRAN I: maximum 6 . COBOL: maximum 30 . C99: no limit but only the first 63 are significant; also, external names are limited to a maximum of 31 . C# and Java: no limit, and all characters are significant . C++: no limit, but implementers often impose a length limitation because they do not want the symbol table in which identifiers are stored during compilation to be too large and also to simplify the maintenance of that table.
    [Show full text]
  • A Concurrent PASCAL Compiler for Minicomputers
    512 Appendix A DIFFERENCES BETWEEN UCSD'S PASCAL AND STANDARD PASCAL The PASCAL language used in this book contains most of the features described by K. Jensen and N. Wirth in PASCAL User Manual and Report, Springer Verlag, 1975. We refer to the PASCAL defined by Jensen and Wirth as "Standard" PASCAL, because of its widespread acceptance even though no international standard for the language has yet been established. The PASCAL used in this book has been implemented at University of California San Diego (UCSD) in a complete software system for use on a variety of small stand-alone microcomputers. This will be referred to as "UCSD PASCAL", which differs from the standard by a small number of omissions, a very small number of alterations, and several extensions. This appendix provides a very brief summary Of these differences. Only the PASCAL constructs used within this book will be mentioned herein. Documents are available from the author's group at UCSD describing UCSD PASCAL in detail. 1. CASE Statements Jensen & Wirth state that if there is no label equal to the value of the case statement selector, then the result of the case statement is undefined. UCSD PASCAL treats this situation by leaving the case statement normally with no action being taken. 2. Comments In UCSD PASCAL, a comment appears between the delimiting symbols "(*" and "*)". If the opening delimiter is followed immediately by a dollar sign, as in "(*$", then the remainder of the comment is treated as a directive to the compiler. The only compiler directive mentioned in this book is (*$G+*), which tells the compiler to allow the use of GOTO statements.
    [Show full text]
  • Software Comprehension Using Open Source Tools: a Study
    International Journal of Computer Sciences and Engineering Open Access Survey Paper Vol.-7, Issue-3, March 2019 E-ISSN: 2347-2693 Software Comprehension Using Open Source Tools: A Study Jyoti Yadav Department of Computer Science, Savitribai Phule Pune University, Maharashtra, India *Corresponding Author: [email protected], Tel.: 9881252910 DOI: https://doi.org/10.26438/ijcse/v7i3.657668 | Available online at: www.ijcseonline.org Accepted: 12/Mar/2019, Published: 31/Mar/2019 Abstract: Software applications developed in recent times are written in lakhs of lines of code and have become increasingly complex in terms of structure, behaviour and functionality. At the same time, development life cycles of such applications reveal a tendency of becoming increasingly shorter, due to factors such as rapid evolution of supporting and enabling technologies. As a consequence, an increasing portion of software development cost shifts from the creation of new artefacts to the adaptation of existing ones. A key component of this activity is the understanding of the design, operation, and behaviour of existing artefacts of the code. For instance, in the software industry, it is estimated that maintenance costs exceed 80% of the total costs of a software product’s lifecycle, and software understanding accounts for as much as half of these maintenance costs. Software Comprehension is a key subtask of software maintenance and evolution phase, which is driven by the need to change software. This paper will help in enhancing the ability of the developers to read and comprehend large pieces of software in an organized manner, even in the absence of adequate documentation by using existing open source tools.
    [Show full text]
  • CMSC 246 Systems Programming Spring 2018 Bryn Mawr College Instructor: Deepak Kumar
    1/23/2018 CMSC 246 Systems Programming Spring 2018 Bryn Mawr College Instructor: Deepak Kumar Input • scanf() is the C library’s counterpart to printf. • Syntax for using scanf() scanf(<format-string>, <variable-reference(s)>) • Example: read an integer value into an int variable data. scanf("%d", &data); //read an integer; store into data • The & is a reference operator. More on that later! 2 1 1/23/2018 Reading Input • Reading a float: scanf("%f", &x); • "%f" tells scanf to look for an input value in float format (the number may contain a decimal point, but doesn’t have to). 3 Standard Input & Output Devices • In Linux the standard I/O devices are, by default, the keyboard for input, and the terminal console for output. • Thus, input and output in C, if not specified, is always from the standard input and output devices. That is, printf() always outputs to the terminal console scanf() always inputs from the keyboard • Later, you will see how these can be reassigned/redirected to other devices. 4 2 1/23/2018 Program: Convert Fahrenheit to Celsius • The celsius.c program prompts the user to enter a Fahrenheit temperature; it then prints the equivalent Celsius temperature. • Sample program output: Enter Fahrenheit temperature: 212 Celsius equivalent: 100.0 • The program will allow temperatures that aren’t integers. 5 Program: Convert Fahrenheit to Celsius ctof.c #include <stdio.h> int main(void) { float f, c; printf("Enter Fahrenheit temperature: "); scanf("%f", &f); c = (f – 32) * 5.0/9.0; printf("Celsius equivalent: %.1f\n", c); return
    [Show full text]
  • Concise Introduction to C++
    i i final3 2012/4/19 page 47 i i Chapter 2 Concise Introduction to C++ C++ seems to be most suitable for many practical applications. Indeed, C++ is sufficiently high-level to allow transparent object-oriented programming and efficient use of human resources, yet is also sufficiently low-level to have types, variables, pointers, arrays, and loops to allow efficient use of computer resources. In this chapter, we give a concise description of C++ and illustrate its power as an object-oriented programming language. In particular, we show how to construct and use abstract mathematical objects such as vectors and matrices. We also explain the notion of a template class, which can be filled with a concrete type later on in compilation time. We also discuss inheritance and illustrate its potential. 2.1 Objects As we have seen above, C is a language based on functions. Every command is also a function that returns a value that can be further used or abandoned, according to the wish of the programmer. Furthermore, programmers can write their own functions, which may also return variables of the type specified just before the function name. When the function is called, a temporary, unnamed variable is created to store the returned value until it has been used. C++, on the other hand, is an object-oriented programming language. In this kind of language, the major concern is not the functions that can be executed but rather the objects upon which they operate. Although C++ supports all the operations and features available in C, its point of view is different.
    [Show full text]
  • Concepts in Programming Languages Practicalities
    Concepts in Programming Languages Practicalities I Course web page: Alan Mycroft1 www.cl.cam.ac.uk/teaching/1617/ConceptsPL/ with lecture slides, exercise sheet and reading material. These slides play two roles – both “lecture notes" and “presentation material”; not every slide will be lectured in Computer Laboratory detail. University of Cambridge I There are various code examples (particularly for 2016–2017 (Easter Term) JavaScript and Java applets) on the ‘materials’ tab of the course web page. I One exam question. www.cl.cam.ac.uk/teaching/1617/ConceptsPL/ I The syllabus and course has changed somewhat from that of 2015/16. I would be grateful for comments on any remaining ‘rough edges’, and for views on material which is either over- or under-represented. 1Acknowledgement: various slides are based on Marcelo Fiore’s 2013/14 course. Alan Mycroft Concepts in Programming Languages 1 / 237 Alan Mycroft Concepts in Programming Languages 2 / 237 Main books Context: so many programming languages I J. C. Mitchell. Concepts in programming languages. Cambridge University Press, 2003. Peter J. Landin: “The Next 700 Programming Languages”, I T.W. Pratt and M. V.Zelkowitz. Programming Languages: CACM (published in 1966!). Design and implementation (3RD EDITION). Some programming-language ‘family trees’ (too big for slide): Prentice Hall, 1999. http://www.oreilly.com/go/languageposter http://www.levenez.com/lang/ ? M. L. Scott. Programming language pragmatics http://rigaux.org/language-study/diagram.html (4TH EDITION). http://www.rackspace.com/blog/ Elsevier, 2016. infographic-evolution-of-computer-languages/ I R. Harper. Practical Foundations for Programming Plan of this course: pick out interesting programming-language Languages.
    [Show full text]
  • Java Terms/Concepts You Should Know from OOPDA Glossary Terms
    Java Terms/Concepts You Should Know from OOPDA A abstract class A class with the abstract reserved word in its header. Abstract classes are distinguished by the fact that you may not directly construct objects from them using the new operator. An abstract class may have zero or more abstract methods. abstract method A method with the abstract reserved word in its header. An abstract method has no method body. Methods defined in an interface are always abstract. The body of an abstract method must be defined in a sub class of an abstract class, or the body of a class implementing an interface. Abstract Windowing Toolkit The Abstract Windowing Toolkit (AWT) provides a collection of classes that simplify the creation of applications with graphical user interfaces. These are to be found in the java.awt packages. Included are classes for windows, frames, buttons, menus, text areas, and so on. Related to the AWT classes are those for the Swing packages. aggregation A relationship in which an object contains one or more other subordinate objects as part of its state. The subordinate objects typically have no independent existence separate from their containing object. When the containing object has no further useful existence, neither do the subordinate objects. For instance, a gas station object might contain several pump objects. These pumps will only exist as long as the station does. Aggregation is also referred to as the has-a relationship, to distinguish it from the is-a relationship, which refers to inheritance. anonymous class A class created without a class name.
    [Show full text]
  • The C Programming Language
    The C programming Language The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 Contents ● Preface ● Preface to the first edition ● Introduction 1. Chapter 1: A Tutorial Introduction 1. Getting Started 2. Variables and Arithmetic Expressions 3. The for statement 4. Symbolic Constants 5. Character Input and Output 1. File Copying 2. Character Counting 3. Line Counting 4. Word Counting 6. Arrays 7. Functions 8. Arguments - Call by Value 9. Character Arrays 10. External Variables and Scope 2. Chapter 2: Types, Operators and Expressions 1. Variable Names 2. Data Types and Sizes 3. Constants 4. Declarations http://freebooks.by.ru/view/CProgrammingLanguage/kandr.html (1 of 5) [5/15/2002 10:12:59 PM] The C programming Language 5. Arithmetic Operators 6. Relational and Logical Operators 7. Type Conversions 8. Increment and Decrement Operators 9. Bitwise Operators 10. Assignment Operators and Expressions 11. Conditional Expressions 12. Precedence and Order of Evaluation 3. Chapter 3: Control Flow 1. Statements and Blocks 2. If-Else 3. Else-If 4. Switch 5. Loops - While and For 6. Loops - Do-While 7. Break and Continue 8. Goto and labels 4. Chapter 4: Functions and Program Structure 1. Basics of Functions 2. Functions Returning Non-integers 3. External Variables 4. Scope Rules 5. Header Files 6. Static Variables 7. Register Variables 8. Block Structure 9. Initialization 10. Recursion 11. The C Preprocessor 1. File Inclusion 2. Macro Substitution 3. Conditional Inclusion 5. Chapter 5: Pointers and Arrays 1.
    [Show full text]
  • Intel ® Fortran Programmer's Reference
    ® Intel Fortran Programmer’s Reference Copyright © 1996-2003 Intel Corporation All Rights Reserved Issued in U.S.A. Version Number: FWL-710-02 World Wide Web: http://developer.intel.com Information in this document is provided in connection with Intel products. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted by this document. EXCEPT AS PROVIDED IN INTEL’S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSO- EVER, AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PAR- TICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. Intel products are not intended for use in medical, life saving, or life sustaining applications. This Intel® Fortran Programmer’s Reference as well as the software described in it is furnished under license and may only be used or copied in accordance with the terms of the license. The information in this manual is furnished for infor- mational use only, is subject to change without notice, and should not be construed as a commitment by Intel Corpora- tion. Intel Corporation assumes no responsibility or liability for any errors or inaccuracies that may appear in this document or any software that may be provided in association with this document. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "unde- fined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibil- ities arising from future changes to them.
    [Show full text]
  • PHP Advanced and Object-Oriented Programming
    VISUAL QUICKPRO GUIDE PHP Advanced and Object-Oriented Programming LARRY ULLMAN Peachpit Press Visual QuickPro Guide PHP Advanced and Object-Oriented Programming Larry Ullman Peachpit Press 1249 Eighth Street Berkeley, CA 94710 Find us on the Web at: www.peachpit.com To report errors, please send a note to: [email protected] Peachpit Press is a division of Pearson Education. Copyright © 2013 by Larry Ullman Acquisitions Editor: Rebecca Gulick Production Coordinator: Myrna Vladic Copy Editor: Liz Welch Technical Reviewer: Alan Solis Compositor: Danielle Foster Proofreader: Patricia Pane Indexer: Valerie Haynes Perry Cover Design: RHDG / Riezebos Holzbaur Design Group, Peachpit Press Interior Design: Peachpit Press Logo Design: MINE™ www.minesf.com Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact [email protected]. Notice of Liability The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it. Trademarks Visual QuickPro Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
    [Show full text]
  • Software II: Principles of Programming Languages Control Statements
    Software II: Principles of Programming Languages Lecture 8 – Statement-Level Control Structures Control Statements: Evolution • FORTRAN I control statements were based directly on IBM 704 hardware • Much research and argument in the 1960s about the issue – One important result: It was proven that all algorithms represented by flowcharts can be coded with only two-way selection and pretest logical loops Control Structure • A control structure is a control statement and the statements whose execution it controls • Design question – Should a control structure have multiple entries? Selection Statements • A selection statement provides the means of choosing between two or more paths of execution • Two general categories: – Two-way selectors – Multiple-way selectors Two-Way Selection Statements • General form: if control_expression then clause else clause • Design Issues: – What is the form and type of the control expression? – How are the then and else clauses specified? – How should the meaning of nested selectors be specified? The Control Expression • If the then reserved word or some other syntactic marker is not used to introduce the then clause, the control expression is placed in parentheses • In C89, C99, Python, and C++, the control expression can be arithmetic • In most other languages, the control expression must be Boolean Clause Form • In many contemporary languages, the then and else clauses can be single statements or compound statements • In Perl, all clauses must be delimited by braces (they must be compound) • In Fortran
    [Show full text]
  • Software II: Principles of Programming Languages Introduction
    Software II: Principles of Programming Languages Lecture 5 – Names, Bindings, and Scopes Introduction • Imperative languages are abstractions of von Neumann architecture – Memory – Processor • Variables are characterized by attributes – To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility Names • Design issues for names: – Are names case sensitive? – Are special words reserved words or keywords? Names (continued) • Length – If too short, they cannot be connotative – Language examples: • FORTRAN 95: maximum of 31 (only 6 in FORTRAN IV) • C99: no limit but only the first 63 are significant; also, external names are limited to a maximum of 31 (only 8 are significant K&R C ) • C#, Ada, and Java: no limit, and all are significant • C++: no limit, but implementers often impose one Names (continued) • Special characters – PHP: all variable names must begin with dollar signs – Perl: all variable names begin with special characters, which specify the variable’s type – Ruby: variable names that begin with @ are instance variables; those that begin with @@ are class variables Names (continued) • Case sensitivity – Disadvantage: readability (names that look alike are different) • Names in the C-based languages are case sensitive • Names in others are not • Worse in C++, Java, and C# because predefined names are mixed case (e.g. IndexOutOfBoundsException ) Names (continued) • Special words – An aid to readability; used to delimit or separate statement clauses • A keyword is a word that is special only
    [Show full text]