(Cforall) User Manual Version
Total Page:16
File Type:pdf, Size:1020Kb
A 1 C (Cforall) User Manual 2 Version 1.0 3 “describe not prescribe” A 4 C Team (past and present) Andrew Beach, Richard Bilson, Michael Brooks, Peter A. Buhr, Thierry Delisle, Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Colby Parsons, Rob Schluntz, Fangren Yu, Mubeen Zulfiqar 5 June 29, 2021 6 © 2016, 2018, 2021 C A Project 7 DRAFT 8 This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of 9 this license, visit http://creativecommons.org/licenses/by/4.0. 1 Contents 2 1 Introduction 1 3 1.1 Background ........................................... 1 4 2 WhyfixC? 2 5 3 History 2 6 4 Interoperability 3 7 5 C A Compilation 3 8 6 Backquote Identifiers 5 9 7 Constant Underscores 6 10 8 Exponentiation Operator 6 11 9 Control Structures 7 12 9.1 if / while Statement ....................................... 7 13 9.2 case Clause ........................................... 7 14 9.3 switch Statement ........................................ 8 15 9.4 Non-terminating and Labelled fallthrough ........................... 12 16 9.5 Loop Control .......................................... 12 17 9.6 Labelled continue / break Statement .............................. 14 18 9.7 with Statement ......................................... 14 19 10 Exception Handling 17 20 10.1 Exception Hierarchy ...................................... 18 21 11 Alternative Declarations 19 22 12 Pointer / Reference 20 23 12.1 Initialization ........................................... 23 24 12.2 Address-of Semantics ...................................... 25 25 12.3 Conversions ........................................... 25 26 13 Enumeration 26 27 13.1 Enum type ............................................ 27 28 13.2 Inheritance ........................................... 28 29 14 Routine Definition 28 30 14.1 Named Return Values ...................................... 30 31 14.2 Routine Prototype ........................................ 30 32 15 Routine Pointers 31 33 16 Named and Default Arguments 31 34 17 Unnamed Structure Fields 34 ii CONTENTS iii 1 18 Nesting 34 2 18.1 Type Nesting .......................................... 34 3 18.2 Routine Nesting ......................................... 34 4 19 Tuple 36 5 19.1 Multiple-Return-Value Functions ................................ 36 6 19.2 Expressions ........................................... 37 7 19.3 Variables ............................................ 38 8 19.4 Indexing ............................................. 38 9 19.5 Flattening and Structuring ................................... 38 10 19.6 Assignment ........................................... 39 11 19.7 Construction ........................................... 41 12 19.8 Member-Access Expression .................................. 41 13 19.9 Casting ............................................. 42 14 19.10Polymorphism .......................................... 43 15 19.10.1 Assertion Inference ................................... 44 16 20 Tuples 45 17 20.1 Tuple Coercions ......................................... 46 18 20.2 Mass Assignment ........................................ 47 19 20.3 Multiple Assignment ...................................... 48 20 20.4 Cascade Assignment ...................................... 48 21 21 Stream I/O Library 49 22 21.1 Basic I/O ............................................ 49 23 21.2 Implicit Separator ........................................ 52 24 21.3 Separation Manipulators .................................... 52 25 21.4 Newline Manipulators ...................................... 53 26 21.5 Output Value Manipulators ................................... 54 27 21.6 Input Value Manipulators .................................... 57 28 21.7 Concurrent Stream Access ................................... 58 29 22 String Stream 59 30 22.1 Structures ............................................ 59 31 23 Constructors and Destructors 61 32 24 Overloading 61 33 24.1 Constant ............................................. 61 34 24.2 Variable ............................................. 63 35 24.3 Function Overloading ...................................... 63 36 24.4 Operator ............................................. 64 37 25 Auto Type-Inferencing 65 38 26 Concurrency 66 39 26.1 Coroutine ............................................ 67 40 26.2 Monitors ............................................. 67 41 26.3 Tasks .............................................. 67 iv CONTENTS 1 27 Language Comparisons 68 2 27.1 C++ ............................................... 69 3 27.2 Go ................................................ 69 4 27.3 Rust ............................................... 69 5 27.4 D ................................................ 70 6 A Syntax Ambiguities 70 7 B C Incompatibles 70 8 C C A Keywords 74 9 D Standard Headers 74 10 E Standard Library 74 11 E.1 Storage Management ...................................... 74 12 E.2 String to Value Conversion ................................... 77 13 E.3 Search / Sort ........................................... 78 14 E.4 Absolute Value ......................................... 78 15 E.5 Random Numbers ........................................ 79 16 E.6 Algorithms ........................................... 79 17 F Math Library 79 18 F.1 General ............................................. 79 19 F.2 Exponential ........................................... 80 20 F.3 Logarithm ............................................ 80 21 F.4 Trigonometric .......................................... 81 22 F.5 Hyperbolic ........................................... 82 23 F.6 Error / Gamma ......................................... 83 24 F.7 Nearest Integer ......................................... 84 25 F.8 Manipulation .......................................... 85 26 G Time Keeping 86 27 G.1 Duration ............................................. 86 28 G.2 timeval .............................................. 88 29 G.3 timespec ............................................. 88 30 G.4 itimerval ............................................. 88 31 G.5 Time ............................................... 88 32 H Clock 89 33 H.1 C time .............................................. 89 34 H.2 Clock .............................................. 89 35 I Multi-precision Integers 90 36 J Rational Numbers 93 37 Index 97 1 1 Introduction 1A 2 C is a modern general-purpose concurrent programming-language, designed as an evolutionary step 3 forward for the C programming language. The syntax of C A builds from C and should look immedi- 4 ately familiar to C/C++programmers. C A adds many modern features that directly lead to increased safety 5 and productivity, while maintaining interoperability with existing C programs and achieving similar perfor- 6 mance. Like C, C A is a statically typed, procedural (non-object-oriented) language with a low-overhead 7 runtime, meaning there is no global garbage-collection, but regional garbage-collection is possible. The 8 primary new features include polymorphic routines and types, exceptions, concurrency, and modules. A 9 One of the main design philosophies of C A is to “describe not prescribe”, which means C tries to 10 provide a pathway from low-level C programming to high-level C A programming, but it does not force 11 programmers to “do the right thing”. Programmers can cautiously add C A extensions to their C programs in 12 any order and at any time to incrementally move towards safer, higher-level programming. A programmer is A 13 always free to reach back to C from C ,A for any reason, and in many cases, new C features can be locally 14 switched back to there C counterpart. There is no notion or requirement for rewriting a legacy C program in A A 15 C ;A instead, a programmer evolves a legacy program into C by incrementally incorporating C features. A A 16 As well, new programs can be written in C A using a combination of C and C features. In many ways, C 17 is to C as Scala [29] is to Java, providing a vehicle for new typing and control-flow capabilities on top of a 18 highly popular programming language allowing immediate dissemination. 19 C++ [30] had a similar goal 30 years ago, allowing object-oriented programming to be incrementally 20 added to C. However, C++currently has the disadvantages of a strong object-oriented bias, multiple legacy 21 design-choices that cannot be updated, and active divergence of the language model from C, requiring 22 significant effort and training to incrementally add C++to a C-based project. In contrast, C A has 30 years of 23 hindsight and a clean starting point. 24 Like C++, there may be both old and new ways to achieve the same effect. For example, the following 25 programs compare the C, C ,A and C++I/O mechanisms, where the programs output the same result. C C A C++ #include <stdio.h> #include <fstream> #include <iostream> using namespace std; 26 int main( void ) { int main( void ) { int main() { int x=0,y=1,z=2; int x=0,y=1,z=2; int x=0,y=1,z=2; printf( "%d %d %d\n",x,y,z); sout|x|y|z; cout<<x<<" "<<y<<" "<<z<<endl; } } } 27 While C A I/O (see Section 21, p. 49) looks similar to C++, there are important differences, such as automatic 28 spacing between variables and an implicit newline at the end of the expression list, similar to Python [26]. 29 1.1 Background 30 This document is a programmer reference-manual for the C A programming language. The manual covers the 31 core features of the language and runtime-system, with simple examples illustrating syntax and semantics 32 of