The New C Standard (Excerpted material)

Similar documents
The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

The New C Standard (Excerpted material)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

DECISION MAKING STATEMENTS

The New C Standard (Excerpted material)

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

LECTURE 18. Control Flow

The New C Standard (Excerpted material)

Review of the C Programming Language

The New C Standard (Excerpted material)

Chapter 6 Control Flow. June 9, 2015

The New C Standard (Excerpted material)

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Statement level control structures

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

MIDTERM EXAMINATION - CS130 - Spring 2003

Flow Control. CSC215 Lecture

G Programming Languages - Fall 2012

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

1 Lexical Considerations

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Compiler Construction D7011E

This book is licensed under a Creative Commons Attribution 3.0 License

Review of the C Programming Language for Principles of Operating Systems

Informatica 3 Syntax and Semantics

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

System Verilog Tagged Unions and Pattern Matching

The role of semantic analysis in a compiler

CSE 582 Autumn 2002 Exam 11/26/02

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Syntax. A. Bellaachia Page: 1

Introduction to Lexical Analysis

Programming Languages Third Edition. Chapter 7 Basic Semantics

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Modules:Context-Sensitive Keyword

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Expressions & Assignment Statements

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

The New C Standard (Excerpted material)

CS 415 Midterm Exam Spring SOLUTION

Introduction. C provides two styles of flow control:

Safe and Secure Software. Ada An Invitation to. Safe Syntax. Courtesy of. John Barnes. The GNAT Pro Company

Lexical Considerations

Introduction to Lexical Analysis

Ch. 7: Control Structures

COMPILER DESIGN LECTURE NOTES

The Syntax of auto Declarations

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

VARIABLES AND CONSTANTS

Programmiersprachen (Programming Languages)


Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Intermediate Code Generation

Scheme Quick Reference

! Non-determinacy (unspecified order) Maria Hybinette, UGA 2. 1 Landin adding sugar to a language to make it easier to read (for humans)

Lexical Considerations

Topic IV. Block-structured procedural languages Algol and Pascal. References:

LECTURE 14. Names, Scopes, and Bindings: Scopes

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

1.1 Introduction to C Language. Department of CSE

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Definition Checklist for Source Statement Counts

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

QUIZ. What is wrong with this code that uses default arguments?

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Programming for Engineers Iteration

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

8. Control statements

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Chapter 8 Statement-Level Control Structures

15 FUNCTIONS IN C 15.1 INTRODUCTION

5 The Control Structure Diagram (CSD)

Today's Topics. CISC 458 Winter J.R. Cordy

Transcription:

The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved.

1722 6.8.1 Labeled statements labeled statements syntax 6.8.1 Labeled statements labeled-statement: identifier : statement case constant-expression : statement default : statement 1722 Rationale Case ranges of the form, lo.. hi, were seriously considered, but ultimately not adopted in the Standard on the grounds that it added no new capability, just a problematic coding convenience. The construct seems to promise more than it could be mandated to deliver: A great deal of code or jump table space might be generated for an innocent-looking case range such as 0.. 65535. The range A.. Z would specify all the integers between the character code for upper-case-a and that for upper-case-z. In some common character sets this range would include non-alphabetic characters, and in others it might not include all the alphabetic characters, especially in non-english character sets. No serious consideration was given to making switch more structured, as in Pascal, out of fear of invalidating working code. When source code oriented symbolic debuggers (i.e., the ability to refer to lines in the source) are not available labels are sometimes used as a means of associating a symbol with a known point in the code. Some languages (e.g., Fortran and Pascal) require that labels be digit, rather than alphabetic, character sequences. Languages in the Pascal family do not classify case, or any equivalent to default, as a label. They are usually defined as part of the syntax of the selection statement in which they appear. Ada intentionally [1] differentiates visually between statement labels and case labels. An identifier that is a statement label is required to appear between the tokens << and >>. Some languages use else or otherwise rather than default in their selection statements. Common Implementations Some implementations (e.g., gcc and Code Warrior [2] ) support case ranges as an extension. jump statement syntax jump statement causes jump to Many coding guideline documents recommend against the use of goto statements. However, in the case of C language, prohibiting labels (except case and default) would be more encompassing. For instance, a mistyped label in a switch statement may turn it into another kind of label (e.g., misspelling default, defolt is often seen for non-english speakers, creates a different kind of label). The issues associated with the use of jump statements are discussed elsewhere. Usage In the translated form of this book s benchmark programs 2% of labels were not the destination of any goto statement. Usage information on goto statements is given elsewhere. 2 v 1.1 January 30, 2008

6.8.1 Labeled statements 1725 Table 1722.1: Percentage of function definitions containing a given number of labeled statements (other than a case or default label). Based on the visible form of the.c files. Labels % Functions Labels % Functions 1 3.5 3 0.3 2 0.9 4 0.1 Constraints 1723 A case or default label shall appear only in a switch statement. case label appear within switch This constraint is necessary because these constructs are not distinguished syntactically from other kinds of labels. In other languages these kinds of labels are usually distinguished in the language syntax. This constraint does not prohibit case or default labels occurring in a variety of potentially surprising, to a reader, contexts within a switch statement. For instance, within a nested compound statement. 1 extern int glob; 2 3 void f(int valu) 4 { 5 switch (valu) 6 { 7 case 0: if (glob == 3) 8 { 9 case 2: value--; 10 } 11 break; 12 } 13 } In practice this usage is rare. Some practical applications of this usage are discussed elsewhere. Duff s Device 1724 Further constraints on such labels are discussed under the switch statement. This is essentially a forward reference. forward reference 1725 Label names shall be unique within a function. label name unique Label names have function scope, which means they are visible anywhere within the function definition that scope function contains them. C90 This wording occurred in Clause 6.1.2.1 Scope of identifiers in the C90 Standard. As such a program that contained non unique labels exhibited undefined behavior. A function definition containing a non-unique label name that was accepted by some C90 translator (a very rare situation) will cause a C99 translator to generate a diagnostic. January 30, 2008 v 1.1

1727 6.8.1 Labeled statements redundant code label naming conventions Semantics This requirement is common to most languages, although a few (e.g., Algol 68) give labels block scope. Some assembly languages supported duplicate label names. A jump instruction usually being defined, in these cases, to jump to the closest label with a given name. Common Implementations Although this was not a requirement that appeared within a Constraints clause in C90, all implementations known to your author issued a diagnostic if a label name was not unique within a function definition. Label names that are not referenced might be classified as redundant code. However, labels have uses other than as the destination of goto statements. They may also be used as breakpoints when stepping through code using a symbolic debugger. Automatically generated code may also contain labels that are not jumped to. Given that labels are relatively uncommon, and the only nontrivial cost involved in a redundant label name is likely to be a small increase in reader comprehension time, a recommendation that all defined labels be jumped to from somewhere does not appear to have a worthwhile benefit. The issues relating to the spelling of label names are discussed elsewhere. case fall through EXAMPLE case fall through Any statement may be preceded by a prefix that declares an identifier as a label name. It is not possible to prefix a declaration with a label (although a preceding null statement could be labeled). C does not provide any mechanism for declaring labels before they appear as a prefix on a statement. Labels may be referenced before they are declared. That is it is possible to goto a label that has not yet been seen within the current function. Some languages (e.g., those in the Pascal family) provide declaration syntax for label names. Their appearance as a statement prefix is simply a use of a name that has been previously defined. Some languages (e.g., Fortran) allow all lines to be prefixed by a line number. These line numbers can also be used as the destination in goto statements. Labels in themselves do not alter the flow of control, which continues unimpeded across them. An example of this is given elsewhere. C++ The C++ Standard only explicitly states this behavior for case and default labels (6.4.2p6). It does not specify any alternative semantics for ordinary labels. In some languages certain kinds of labels alter the flow of control. For instance, in Pascal encountering a case label cases flow of control to jump to the end of the switch statement (the following using Pascal syntax and keywords). 1 case expr of 2 1: begin 3 x:=88; 4 y:=99; 5 end; 6 7 2: z:=77; 8 end; 1726 1727 v 1.1 January 30, 2008

6.8.1 Labeled statements 1728 Common Implementations Labels may not alter the flow of control but they are usually the destination of a change of flow of control. As such they are bad news for code optimization, because accumulated information on the values of objects either has to be reset to nothing (worst-case assumption made by a simple optimizer), or merged with information obtained along other paths (more sophisticated optimizer). Most C coding guideline documents recommend against falling through to a statement prefixed by a case or default label. For instance, in the following fragment it is likely that the author had forgotten to place a break statement after the assignment to y. 1 case 1: x=88; 2 y=99; 3 4 case 2: z=77; There are occasions where falling through to a statement prefixed by a case label is the intended behavior. A Duff s Device convention sometimes adopted is to place a comment containing the words FALL THROUGH on the line before the statement that is fallen into (some static analysis tools recognize this usage and don t issue a diagnostic for the fall through that it comments). Experience suggests that code containing a statement prefixed by a case label whose flow of control falls through to another statement prefixed by a case label causes readers to spend significant effort in verifying whether the usage is intended or a fault in the code. Commenting such usage has a cost that is likely to be significantly smaller than the benefit. Cg 1727.1 If the flow of control can reach a statement prefixed by one or more case labels, other than by a direct jump from the controlling expression of a switch statement, the source line immediately before that statement shall contain the words FALL THROUGH in a comment. Table 1727.1: Common token pairs involving a case or default label. Based on the visible form of the.c files. Almost all of the sequences { case occur immediately after the controlling expression of the switch statement. Token Sequence % Occurrence First Token % Occurrence of Second Token ; default 0.4 81.4 ; case 2.1 52.1 : case 15.5 22.1 { case 2.6 15.0 } case 1.3 7.3 : default 0.5 5.7 #endif default 0.8 4.4 1728 Forward references: the goto statement (6.8.6.1), the switch statement (6.8.4.2). January 30, 2008 v 1.1

References 1. J. Ichbiah, J. Barnes, R. Firth, and M. Woodger. Rationale for the Design of the Ada Programming Language. Cambridge University Press, 1991. 2. Metroworks. CodeWarrior C Compilers Reference. Metroworks Corp., Aug. 2001. v 1.1 January 30, 2008