ANSI C Changes. Jonathan Hoyle Eastman Kodak 10/5/00

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

Variables. Data Types.

Introduction to C++ Systems Programming

Chapter 1. C Pocket Reference

Tokens, Expressions and Control Structures

Basic Types, Variables, Literals, Constants

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Appendix. Compatibility. B.1 Introduction. You go ahead and follow your customs, and I ll follow mine. C. Napier

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

edunepal_info

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Programming in C and C++

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Chapter 15 - C++ As A "Better C"

MISRA-C:2012 Standards Model Summary for C / C++

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

6.096 Introduction to C++ January (IAP) 2009

HP C Language Reference Manual

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Types, Variables, and Constants

A Fast Review of C Essentials Part I

Compiler Construction. Lecture 10

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CMPE-013/L. Introduction to C Programming

Pointers, Dynamic Data, and Reference Types

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

COMP322 - Introduction to C++

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CS Programming In C

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

Outline. 1 About the course

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Programming Multiple. Choice

CS3157: Advanced Programming. Outline

Object-Oriented Programming

Presented By : Gaurav Juneja

Structure of this course. C and C++ Past Exam Questions. Text books

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Motivation was to facilitate development of systems software, especially OS development.

Programming in C and C++

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Motivation was to facilitate development of systems software, especially OS development.

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Programming. C++ Basics

Programming in C++ 4. The lexical basis of C++

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

The New C Standard (Excerpted material)

The C Programming Language

Axivion Bauhaus Suite Technical Factsheet MISRA

Short Notes of CS201

BLM2031 Structured Programming. Zeyneb KURT

ZiLOG Z8 Encore! Compiler Compliance With ANSI STANDARD C

Introduction to Computing Lecture 01: Introduction to C

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CS201 - Introduction to Programming Glossary By

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CSCE 110 PROGRAMMING FUNDAMENTALS

UNIT- 3 Introduction to C++

Review of the C Programming Language for Principles of Operating Systems

1.1 Introduction to C Language. Department of CSE

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Advanced Systems Programming

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

Have examined process Creating program Have developed program Written in C Source code

C The Language. Topics K&R C. A Brief History Of C

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

AIMS Embedded Systems Programming MT 2016

Programming with C++ Language

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

Stream Computing using Brook+

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Advanced use of the C language

Escher C/C++ Verifier 7.0. Reference Manual

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

Introduce C# as Object Oriented programming language. Explain, tokens,

Contents of Lecture 3

1d: tests knowing about bitwise fields and union/struct differences.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CSE 333 Lecture 2 Memory

The New C Standard (Excerpted material)

VARIABLES AND CONSTANTS

BCA-105 C Language What is C? History of C

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

CSCI 171 Chapter Outlines

CMSC 246 Systems Programming

5.Coding for 64-Bit Programs

Transcription:

ANSI C Changes Jonathan Hoyle Eastman Kodak 10/5/00

ANSI C Changes Introduction Changes to C in conformance to C++ New additions to C friendly to C++ New additions to C unfriendly to C++ What has not changed in C Conclusion

Introduction

Timeline 1969 - Ken Thompson creates Unix, B from BCPL 1970 - Thompson & Ritchie evolve B to C 1978 - K&R s The C Programming Language 1982 - ANSI forms a committee on standardizing C 1988 - K&R updated for ANSI Draft 1989 - ANSI approves C (called C89 ) 1990 - ISO approves C (called C90 ) 1995 - New committee formed for C9X 1999 - ISO approval (called C99 ) 2000 - ANSI approves C99

Timeline

Why Should We Care About C? Because I brought doughnuts. C is a subset of C++ ANSI C++ compilers are usually ANSI C We may inherit C code We may still have to support C s public interface (libraries, etc.) There are some very useful features in C99 which we may want to use even in C++.

Changes to C in Conformance with C++

C++ style comments /* Old style comments */ DoSomeStuff(); // New style comments now in C! DoSomeMoreStuff();

No implicit int and prototypes Implicit int variables and function prototypes no longer supported. const x = 0; //C90: OK; C99,C++: Error void foo() { } // No implicit int bar() prototype x = bar(); //C90: OK; C99,C++: Error

Intermixed declarations & statements void foo() { // Part 1 int a, b, c; PerformStuff(a, b, c); } // Part 2 int x, y, z; PerformStuff(x, y, z);

Conditional Expression Declarations for (int n = 0; n < 10; n++) { DoLoop(n); }

Conditional Expression Declarations int n = 100; for (int n = 0; n < 10; n++) { DoLoop(n); } printf( %d, n); // Prints 100

Inline functions inline int Clamp(int inparm) { return (inparm < 0)? 0 : inparm; } void foo() { }... a = Clamp(x); b = Clamp(y); c = Clamp(z);

bool type ANSI C adds the _Bool keyword and type It is an integer type holding (at least) 1 bit It is an arithmetic type <stdbool.h> includes the definitions: #define bool _Bool #define true (_Bool) 1 #define false (_Bool) 0 C++ will provide an empty <stdbool.h>

Alternate punctuation tokens Requires the <iso646.h> header file (already supplied by C++): and && not_eq!= and_eq &= or bitand & or_eq = bitor xor ^ compl ~ xor_eq ^= not!

Digraph Punctuation Tokens Two character token replacements: <: [ :> ] <% { %> } %: # %:%: ## // Function definition using digraphs: %:include <string.h> void PCopy(char d<::>,const char s<::>) <% strncpy(d, &s<:1:>, s<:0:>); %>

New Features friendly to C++

long long (64-bit integer type) long long x; unsigned long long y; x = -9000000000000000000LL; y = 18000000000000000000ULL;

#include <stdint.h> // New types: int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t int_leastn_t int_fastn_t intmax_t intptr_t uint_leastn_t uint_fastn_t uintmax_t uintptr_t

Variable Length Arrays // Create a variable sized 2D matrix void foo(int x, int y) { double thematrix[x][y]; }...

Predefined func identifier // func returns function name void foo() { LogThis( FILE ); // main.c LogThis( LINE ); // 27 LogThis( func ); // foo... }

Variable Argument Macros #ifdef DEBUG #define DPr(...) printf( VA_ARGS ) #else #define DPr(...) #endif //DEBUG DPr( Hello, World! ); DPr( Error = %d, 10); DPr( %d\t%5.3f\t, 1999, 3.14159);

Designated Initializers int a[5] = { 2, 3, 5, 7, 11 }; int a[10] = { [0] = 100, [3] = 200 }; struct B { int x; double y; }; struct B b = {.x = 3,.y = 2.71828 };

restrict-qualified pointers ANSI C adds the new keyword restrict Restricted ptr s have sole access to data Designate non-overlapping pointers Allows for compiler optimization Used for: function prototypes fields in struct definitions return types for memory allocation

restrict-qualified pointers void AddFloat(float *restrict sumptr, const float *ptr1, const float *ptr2, int numitems) { for (int i = 0; i < numitems; i++) sumptr[i] = ptr1[i] + ptr2[i]; } // sumptr should not overlap ptr1, ptr2

Other sundry changes... Flexible array members: struct S { int count; // fixed member int myarray[]; // flexible member }; Compound literals: char *String = Hello, World! float *iptr = (float [2]) {0.5, 2.7};

Other sundry changes... Hexadecimal floating point literals: float pi = 0x3.243F6A88; New _Pragma operator: _Pragma(directive) // #pragma directive Empty Macro arguments: #define Add(x,y,z) (x + y + z) Add(,,1); // becomes: ( + + 1); enum list with trailing comma: enum T { t0, t1, t2, };

New Features unfriendly to C++

complex arithmetic type ANSI C adds _Complex and _Imaginary <complex.h> includes the definitions: #define complex _Complex #define imaginary _Imaginary #define I imaginary identity ANSI C++ s templated complex<> type is incompatible with ANSI C s complex C++ s complex.h header file conflicts with ANSI C s of the same name.

complex arithmetic type // ANSI C complex function double complex square(double complex z) { return (z * z); } // ANSI C++ complex function complex<double> square(complex<double> z) { return (z * z); }

complex arithmetic type // Suggested solution #include <complex.h> #ifdef cplusplus typedef complex<float> complex_float; typedef complex<double>complex_double; #else typedef float complex complex_float; typedef double complex complex_double; #endif // cplusplus

C/C++ clog identifier conflict ANSI C defines clog() in <complex.h> as the complex logarithm function. ANSI C++ already defined clog in <iostream.h> as standard error logging. // Possible conflict in clog usage void foo() { } clog << clog(2.71828) << endl;

Type-generic math functions Math functions (formerly defined with double s) have a dynamic return type. Affects only functions defined in <math.h>, such as sin(), cos(), sqrt(), etc. sin(3.14159); //returns a double sin(3.14159f); //returns a float sin(3.14159l); //returns long double Macro-based (requires <tgmath.h>) May conflict with C++ s overloading rules

What has not changed in ANSI C

Unchanged in C K&R-style prototypes still legal (deprecated): void foo(a, b) int a; int b; {... } goto s still not deprecated sizeof ( a ) == sizeof(int), not sizeof(char) Hello is still a char *, not a const char *

Unchanged in C Duplicate typedef s still illegal in C enum constants are still signed int s Tentative definitions still legal in C: int x; // tentative defn int x = 1; // explicit defn String initializers without null-termination: char s[6] = Hello ; // legal C/C++ char s[5] = Hello ; // legal C only

Conclusion

Conclusion Best features of C99: Variable length arrays Variable argument macros New integer types restrict-qualified pointers Worst features of C99: incompatible features with C++ K&R prototypes still legal goto s not deprecated

Resources ANSI/ISO C 1999 Specification: http://www.jonhoyle.com/ansi/c.pdf ANSI/ISO C++ 1998 Specification: http://www.jonhoyle.com/ansi/cplusplus.pdf ISO C99 Rationale: http://www.jonhoyle.com/ansi/cplusplus.pdf Incompatibilities between ISO C & ISO C++: http://home.flash.net/~dtribble/text/cdiffs.htm

Q & A