The C Programming Language

Similar documents
Outline. 1 About the course

September 10,

CSCE 110 PROGRAMMING FUNDAMENTALS

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

AN OVERVIEW OF C++ 1

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS201 - Introduction to Programming FAQs By

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

Topics. bool and string types input/output library functions comments memory allocation templates classes

EL2310 Scientific Programming

CS201 - Introduction to Programming Glossary By

CSE 374 Programming Concepts & Tools

EL2310 Scientific Programming

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Object-Oriented Programming

Introduction to Scientific Programming in C++

Tokens, Expressions and Control Structures

Short Notes of CS201

Final CSE 131B Spring 2004

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CS11 Advanced C++ Fall Lecture 1

Fast Introduction to Object Oriented Programming and C++

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

Programming in C and C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

OBJECT ORIENTED PROGRAMMING

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

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

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

C The new standard

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

CS

Vector and Free Store (Vectors and Arrays)

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

Introduction to C++ Introduction to C++ 1

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

Chapter 17 vector and Free Store

Interview Questions of C++

COSC 2P95. Introduction. Week 1. Brock University. Brock University (Week 1) Introduction 1 / 18

Chapter 1 & 2 Introduction to C Language

CS3157: Advanced Programming. Outline

Checking C Declarations at Link Time 1 2

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

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

Spring 2018 NENG 202 Introduction to Computer Programming

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Introduction to C++ with content from

A brief introduction to C programming for Java programmers

Programming in C and C++

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

ECE 2400 Computer Systems Programming Fall 2018 Topic 11: Transition to C++

Chapter 9 Technicalities: Classes

Chapter 17 vector and Free Store. Bjarne Stroustrup

Evolution of Programming Languages

III. Classes (Chap. 3)

Stroustrup Default comparisons N4175. Default comparisons. Bjarne Stroustrup

Chapters 1 & 2 Programming and Programs

High Performance Computing and Programming, Lecture 3

TDDE18 & 726G77. Functions

Default comparisons (R2)

Object Oriented Software Design II

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

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

Chapter 3 Objects, types, and values. Bjarne Stroustrup

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CSE 303: Concepts and Tools for Software Development

PPP Style Guide Stroustrup 1/29/2010. PPP Style Guide. Bjarne Stroustrup

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

C++: Overview and Features

Welcome to CS 106L! Ali Malik

Advanced Systems Programming

CSC231 C Tutorial Fall 2018 Introduction to C

Pointers, Dynamic Data, and Reference Types

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Exercise 6.2 A generic container class

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

6.096 Introduction to C++ January (IAP) 2009

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

The Inefficiency of C++ Fact or Fiction? Anders Lundgren IAR Systems

PROGRAMMAZIONE I A.A. 2017/2018

CS1102: What is a Programming Language?

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

C Language Programming

CS Prof J.P.Morrison

Chapter 9 Technicalities: Classes, etc. Walter C. Daugherity Lawrence Pete Petersen Bjarne Stroustrup Fall 2007

Types, Values, Variables & Assignment. EECS 211 Winter 2018

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

SWEN-250 Personal SE. Introduction to C

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

An Object Oriented Programming with C

Transcription:

The C Programming Language Bjarne Stroustrup www.stroustrup.com/programming Dennis M. Ritchie

dmr C and C++ ken bwk bs doug Both were born in the Computer Science Research Department of Bell Labs in Murray Hill, NJ Stroustrup/PPP - Dec'13 3

Modern C and C++ are siblings Kenneth Thompson & Dennis Ritchie C++11 C11 C++14 Stroustrup/PPP - Dec'13 4

C and C++ In this talk, I use C to mean ISO C89 That s by far the most commonly used definition of C Classic C has mostly been replaced (though amazingly not completely) C99 is not yet widely used, C11 may be catching on Source compatibility C is (almost) a subset of C++ Example of exception: int f(int new, int class, int bool); /* ok in C */ (Almost) all constructs that are both C and C++ have the same meaning (semantics) in both languages Example of exception: sizeof('a') /* 4 in C and 1 in C++ */ Link compatibility C and C++ program fragments can be linked together in a single program And very often are C++ was designed to be as close as possible to C, but no closer For ease of transition For co-existence Most incompatibilities are related to C++ s stricter type checking Stroustrup/PPP - Dec'13 5

C and C++ Both defined/controlled by ISO standards committees Separate committees Unfortunately, leading to incompatibilities Many supported implementations in use Available on more platforms than any other languages Both primarily aimed at and are heavily used for hard system programming tasks, such as Operating systems kernels Device drivers Embedded systems Compilers Communications systems Stroustrup/PPP - Dec'13 6

C and C++ C++ is a general-purpose programming language with a bias towards systems programming that is a better C supports data abstraction supports object-oriented programming supports generic programming C: Functions and structs Machine model (basic types and operations) Compilation and linkage model Stroustrup/PPP - Dec'13 9

C and C++ In C, borrowed from C++ Function prototypes (declaration and checking of function arguments) Function declaration notation: void f(int x, double y); // comments const (imperfectly) inline (imperfectly) Initializers in for loops: for (int i = 0; /* */ Declarations after statements complex (sort of) bool (sort of) Ban on implicit int : int a; f() { return 2; } I have never seen a program that could be written better in C than in C++ I don t think such a program could exist Stroustrup/PPP - Dec'13 10

Missing in C (from a C++ perspective) Classes and member functions Use struct and global functions Derived classes and virtual functions Use struct, global functions, and pointers to functions You can do OOP in C, but not cleanly, and why would you want to? You can do GP in C, but why would you want to? Templates and inline functions Use macros Exceptions Use error-codes, error-return values, etc. Function overloading Give each function a separate name new/delete Use malloc()/free() References Use pointers const in constant expressions Use macros Stroustrup/PPP - Dec'13 11

Missing in C (from a C++ perspective) With no classes, templates, and exceptions, C can t provide most C++ standard library facilities Containers vector, map, set, string, etc. Use arrays and pointers Use macros (rather than parameterization with types) STL algorithms sort(), find(), copy(), Not many alternatives use qsort() where you can Write your own, use 3 rd party libraries I/O streams Use stdio: printf(), getch(), etc. Regular expression Use a 3 rd party library Stroustrup/PPP - Dec'13 12

C and C++ Lots of useful code is written in C Very few language features are essential In principle, you don t need a high-level language, you could write everything in assembler (but why would you want to do that?) Emulate high-level programming techniques As directly supported by C++ but not C Write in the C subset of C++ Compile in both languages to ensure consistency Use high compiler warning levels to catch type errors Use lint for large programs A lint is a consistency checking program C and C++ are equally efficient If you think you see a difference, suspect differences in default optimizer or linker settings Stroustrup/PPP - Dec'13 13

C/C++ interoperability Works because of shared linkage model Works because a shared model for simple objects built-in types and structs/classes Optimal/Efficient No behind-the-scenes reformatting/conversions Stroustrup/PPP - Dec'13 32

Calling C from C++ Use extern "C" to tell the C++ compiler to use C calling conventions // calling C function from C++: extern "C" double sqrt(double); // link as a C function void my_c_plus_plus_fct() { double sr2 = sqrt(2); // } Stroustrup/PPP - Dec'13 33

Calling C++ from C No special action is needed from the C compiler /* call C++ function from C: */ int call_f(s* p, int i); /* call f for object pointed to by p with argument i */ struct S* make_s(int x, const char* p); /* make S( x,p) on the free store */ void my_c_fct(int i) { /* */ struct S* p = make_s(17, "foo"); int x = call_f(p,i); /* */ } Stroustrup/PPP - Dec'13 34