PROGRAMMING IN C FOR B.TECH IST YEAR.ppt

mbadepartment5 0 views 43 slides Oct 08, 2025
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

C


Slide Content

Chapter 27Chapter 27
The C Programming LanguageThe C Programming Language
Bjarne StroustrupBjarne Stroustrup

www.stroustrup.com/Programmingwww.stroustrup.com/Programming
Dennis M. Ritchie

OverviewOverview

C and C++C and C++

Function prototypesFunction prototypes

printf()/scanf()printf()/scanf()

Arrays and stringsArrays and strings

Memory managementMemory management

MacrosMacros

constconst

C/C++ interoperabilityC/C++ interoperability

ABIsABIs

An exampleAn example
33Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C and C++C and C++

Both were Both were ““bornborn”” in the Computer Science Research Department of in the Computer Science Research Department of
Bell Labs in Murray Hill, NJBell Labs in Murray Hill, NJ
44
dmr
ken
bwk
bs
doug

Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Modern C and C++ are siblingsModern C and C++ are siblings
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 55
C++11 C11
C++14

C and C++C and C++

In this talk, I use “C” to mean “ISO C89”In this talk, I use “C” to mean “ISO C89”

ThatThat’s by far the most commonly used definition of C’s by far the most commonly used definition of C

Classic C has mostly been replaced (though amazingly not completely)Classic C has mostly been replaced (though amazingly not completely)

C99 is not yet widely used, C11 may be catching onC99 is not yet widely used, C11 may be catching on

Source compatibilitySource compatibility

C is (almost) a subset of C++C is (almost) a subset of C++

Example of exception: Example of exception: int f(int new, int class, int bool);int f(int new, int class, int bool); /* /* ok in C ok in C */*/

(Almost) all constructs that are both C and C++ have the same meaning (Almost) all constructs that are both C and C++ have the same meaning
(semantics) in both languages(semantics) in both languages

Example of exception: Example of exception: sizeof('a') /* sizeof('a') /* 4 in C and 1 in C++ 4 in C and 1 in C++ */*/

Link compatibilityLink compatibility

C and C++ program fragments can be linked together in a single programC and C++ program fragments can be linked together in a single program

And very often areAnd very often are

C++ was designed to be “as close as possible to C, but no closer”C++ was designed to be “as close as possible to C, but no closer”

For ease of transitionFor ease of transition

For co-existenceFor co-existence

Most incompatibilities are related to C++Most incompatibilities are related to C++’’s stricter type checkings stricter type checking
66Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C and C++C and C++

Both defined/controlled by ISO standards committeesBoth defined/controlled by ISO standards committees

Separate committeesSeparate committees

Unfortunately, leading to incompatibilitiesUnfortunately, leading to incompatibilities

Many supported implementations in useMany supported implementations in use

Available on more platforms than any other languagesAvailable on more platforms than any other languages

Both primarily aimed at and are heavily used for hard system Both primarily aimed at and are heavily used for hard system
programming tasks, such asprogramming tasks, such as

Operating systems kernelsOperating systems kernels

Device driversDevice drivers

Embedded systemsEmbedded systems

CompilersCompilers

Communications systemsCommunications systems
77Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C and C++C and C++

C is arguably the most successful programming language of all timeC is arguably the most successful programming language of all time

But how would you decide?But how would you decide?

Number of programs writtenNumber of programs written

Importance of programs writtenImportance of programs written

Number of programmersNumber of programmers

LongevityLongevity

Influence on other languagesInfluence on other languages

Benefits/development_costBenefits/development_cost

AlternativesAlternatives

FortranFortran

CobolCobol

LispLisp

C++C++

JavaJava

PHPPHP

PythonPython

……
88Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C and C++C and C++

Here weHere we

assume you know C++ and how to use itassume you know C++ and how to use it

describe the differences between C and C++describe the differences between C and C++

describe how to program using the facilities offered by Cdescribe how to program using the facilities offered by C

Our ideal of programming and our techniques remain the same, but Our ideal of programming and our techniques remain the same, but
the tool available to express our ideas changethe tool available to express our ideas change

describe a few C describe a few C “traps and pitfalls”“traps and pitfalls”

DonDon’t go into all the details from the book’t go into all the details from the book

Compatibility details are important, but rarely interestingCompatibility details are important, but rarely interesting
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 99

C and C++C and C++

C++ is a general-purpose programming language C++ is a general-purpose programming language
with a bias towards systems programming thatwith a bias towards systems programming that

is a better Cis a better C

supports data abstractionsupports data abstraction

supports object-oriented programmingsupports object-oriented programming

supports generic programmingsupports generic programming
1010 Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13
C:C:

Functions and Functions and structstructss

Machine model (basic types and operations)Machine model (basic types and operations)

Compilation and linkage modelCompilation and linkage model

C and C++C and C++

In C, borrowed from C++In C, borrowed from C++

Function prototypes (declaration and checking of function arguments)Function prototypes (declaration and checking of function arguments)

Function declaration notation: Function declaration notation: void f(int x, double y);void f(int x, double y);

//// comments comments

constconst (imperfectly) (imperfectly)

inlineinline (imperfectly) (imperfectly)

Initializers in for loops: Initializers in for loops: for (int i = 0; /* for (int i = 0; /* … … */*/

Declarations after statementsDeclarations after statements

complexcomplex (sort of) (sort of)

boolbool (sort of) (sort of)

Ban on “implicit int”: Ban on “implicit int”: int a; f() { return 2; }int a; f() { return 2; }

……

I have never seen a program that could be written better in C than in C+I have never seen a program that could be written better in C than in C+
++

I don’t think such a program could existI don’t think such a program could exist
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 1111

Missing in C Missing in C (from a C++ perspective)(from a C++ perspective)

Classes and member functionsClasses and member functions

Use Use structstruct and global functions and global functions

Derived classes and virtual functionsDerived classes and virtual functions

Use Use structstruct, global functions, and pointers to functions, global functions, and pointers to functions

You can do OOP in C, but not cleanly, and why would you want to?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?You can do GP in C, but why would you want to?

Templates and inline functionsTemplates and inline functions

Use macrosUse macros

ExceptionsExceptions

Use error-codes, error-return values, etc.Use error-codes, error-return values, etc.

Function overloadingFunction overloading

Give each function a separate nameGive each function a separate name

new/deletenew/delete

Use Use malloc()/free()malloc()/free()

ReferencesReferences

Use pointersUse pointers

constconst in constant expressions in constant expressions

Use macrosUse macros
1212Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Missing in C Missing in C (from a C++ perspective)(from a C++ perspective)

With no classes, templates, and exceptions, C can’t With no classes, templates, and exceptions, C can’t
provide most C++ standard library facilitiesprovide most C++ standard library facilities

ContainersContainers

vectorvector, , mapmap,, set set,, string string, etc., etc.

Use arrays and pointersUse arrays and pointers

Use macros (rather than parameterization with types)Use macros (rather than parameterization with types)

STL algorithmsSTL algorithms

sort()sort(),, find() find(),, copy() copy(),, ……

Not many alternativesNot many alternatives

use use qsort() qsort() where you canwhere you can

Write your own, use 3Write your own, use 3
rdrd
party libraries party libraries

I/O streamsI/O streams

Use stdio:Use stdio: printf() printf(), , getch()getch(),, etc.etc.

Regular expressionRegular expression

Use a 3Use a 3
rdrd
party library party library
1313Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C and C++C and C++

Lots of useful code is written in CLots of useful code is written in C

Very few language features are essentialVery few language features are essential

In principle, you donIn principle, you don’t need a high-level language, you could write everything in ’t need a high-level language, you could write everything in
assembler (but why would you want to do that?)assembler (but why would you want to do that?)

Emulate high-level programming techniquesEmulate high-level programming techniques

As directly supported by C++ but not CAs directly supported by C++ but not C

Write in the C subset of C++Write in the C subset of C++

Compile in both languages to ensure consistencyCompile in both languages to ensure consistency

Use high compiler warning levels to catch type errorsUse high compiler warning levels to catch type errors

Use “lint” for large programsUse “lint” for large programs

A A “lint” is a consistency checking program“lint” is a consistency checking program

C and C++ are equally efficientC and C++ are equally efficient

If you think you see a difference, suspect differences in default optimizer or linker If you think you see a difference, suspect differences in default optimizer or linker
settingssettings
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 1414

FunctionsFunctions

There can be only one function of a given nameThere can be only one function of a given name

Function argument type checking is optionalFunction argument type checking is optional

Use a compiler option that makes it compulsoryUse a compiler option that makes it compulsory

There are no references (and therefore no pass-by-reference)There are no references (and therefore no pass-by-reference)

pass a pointerpass a pointer

There are no member functionsThere are no member functions

There is an alternative function definition syntaxThere is an alternative function definition syntax
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 1515

Function prototypesFunction prototypes
(function argument checking is optional)(function argument checking is optional)
/* /* avoid these mistakes – use a compiler option that enforces C++ rulesavoid these mistakes – use a compiler option that enforces C++ rules */*/
int g(int);int g(int);/* /* prototype – like C++ function declarationprototype – like C++ function declaration */*/
int h();int h();/* /* not a prototype – the argument types are unspecifiednot a prototype – the argument types are unspecified */*/
int f(p,b) char* p; char b;int f(p,b) char* p; char b;/* /* old-style definition – not a prototypeold-style definition – not a prototype */*/
{ /* { /* …… */ } */ }
int my_fct(int a, double d, char* p)int my_fct(int a, double d, char* p)/* /* new-style definition – a prototypenew-style definition – a prototype */*/
{{
f();f();/* /* ok by the compiler! But gives wrong/unexpected resultsok by the compiler! But gives wrong/unexpected results */*/
f(d,p);f(d,p);/* /* ok by the compiler! But gives wrong/unexpected resultsok by the compiler! But gives wrong/unexpected results */*/
h(d);h(d);/* /* ok by the compiler! But may give wrong/unexpected resultsok by the compiler! But may give wrong/unexpected results */*/
ff(d);ff(d);/* /* ok by the compiler! But may give wrong/unexpected resultsok by the compiler! But may give wrong/unexpected results */*/
g(p);g(p);/* /* error: wrong typeerror: wrong type */*/
g();g();/* /* error: argument missingerror: argument missing */*/
}}
1616Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

printf() printf() – many people– many people’’s favorite C functions favorite C function
/* /* no iostreams – use stdiono iostreams – use stdio */*/
#include<stdio.h>#include<stdio.h> /* /* defines defines int printf(const char* format, …); int printf(const char* format, …); */*/
int main(void)int main(void)
{{
printf("Hello, world\n");printf("Hello, world\n");
return 0;return 0;
}}
void f(double d, char* s, int i, char ch)void f(double d, char* s, int i, char ch)
{{
printf("double %g string %s int %i char %c\n", d, s, i, ch);printf("double %g string %s int %i char %c\n", d, s, i, ch);
printf("goof %s\n", i);printf("goof %s\n", i); /* /* uncaught uncaught
errorerror */*/
}}
1717
Format strings
Formatting characters
Arguments to be formatted
Format string
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

scanf()scanf() and friends and friends
/* /* the most popular input functions fromthe most popular input functions from <stdio.h> <stdio.h>:: */ */
int i = getchar();int i = getchar();/* /* notenote int int, not, not char char;;
getchar() getchar() returnsreturns EOF EOF when it reaches end of file when it reaches end of file */*/
char* q = gets(p);char* q = gets(p);/* /* read 'read '\n\n'' terminated line intoterminated line into char char array pointed to byarray pointed to by p p */*/
/* /* sets sets qq to to pp if read succeeds; sets if read succeeds; sets qq to to NULLNULL if read fails if read fails */*/
void f(int* pi, char* pc, double* pd, char* ps)void f(int* pi, char* pc, double* pd, char* ps)
{{/*/* read into variables whose addresses are passed as pointers: read into variables whose addresses are passed as pointers: */*/
scanf("%i %c %g %s", pi, pc, pd, ps);scanf("%i %c %g %s", pi, pc, pd, ps);
/* /* %s %s skips initial whitespace and is terminated by whitespaceskips initial whitespace and is terminated by whitespace */ */
}}
int i; char c; double d; char s[100]; f(&i, &c, &d, s); /* int i; char c; double d; char s[100]; f(&i, &c, &d, s); /* call to assign to call to assign to ii,, c c,, d d, and , and s s */*/

DonDon’’t t everever use use gets()gets() or or scanf("%s")scanf("%s")!!

Consider them poisonedConsider them poisoned

They are the source of They are the source of manymany security violations security violations

An overflow is easily arranged and easily exploitableAn overflow is easily arranged and easily exploitable

Use Use getchar()getchar()
1818Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

printf()printf() and and scanf()scanf() are not type safe are not type safe
double d = 0;double d = 0;
int s = 0;int s = 0;
printf("d: %d , s: %s\n", d, s);printf("d: %d , s: %s\n", d, s);/* /* compiles and runs compiles and runs */*/
/*/* the result might surprise you the result might surprise you */*/
1919
“d” for “decimal”, not “double” (use “g” for double)
“s” for “string”
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Though error-prone, Though error-prone, printf() printf() is convenient for built-in typesis convenient for built-in types

printf() printf() formats are not extensible to user-defined typesformats are not extensible to user-defined types

E.g. no E.g. no %M%M for for My_typeMy_type values values

Beware: a Beware: a printf () printf () with a user-supplied format string is a cracker toolwith a user-supplied format string is a cracker tool

Arrays and pointersArrays and pointers

Defined almost exactly as in C++Defined almost exactly as in C++

In C, you have to use them essentially all the timeIn C, you have to use them essentially all the time

because there is no because there is no vector, map, stringvector, map, string, etc., etc.

RememberRemember

An array doesnAn array doesn’t know how long it is’t know how long it is

it “decays” to a pointerit “decays” to a pointer

There is no array assignmentThere is no array assignment

use use memcpy()memcpy()

A C-style string is a zero-terminated array of A C-style string is a zero-terminated array of charchar
2020Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C-style stringsC-style strings

In C a string (called a C-string or a C-style string in C++ In C a string (called a C-string or a C-style string in C++
literature) is a zero-terminated array of charactersliterature) is a zero-terminated array of characters
char* p = "asdf";char* p = "asdf";
char s[ ] = "asdf";char s[ ] = "asdf";
2121
'a''s' 'f''d' 0p:
'a''s' 'f''d' 0s:
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C-style stringsC-style strings

Comparing stringsComparing strings
#include <string.h>#include <string.h>
if (s1 = = s2) {if (s1 = = s2) {/*/* do do s1 s1 andand s2 s2 point to the same array? point to the same array? */*/
/*/* (typically not what you want) (typically not what you want) */*/
}}
if (strcmp(s1,s2) = = 0) { if (strcmp(s1,s2) = = 0) { /* /* do do s1 s1 andand s2 s2 hold the same characters? hold the same characters? */*/
}}

Finding the length of a stringFinding the length of a string
int lgt = strlen(s);int lgt = strlen(s);/* /* note: goes through the string at run timenote: goes through the string at run time */ */
/*/* looking for the terminating 0 looking for the terminating 0 */*/

Copying stringsCopying strings
strcpy(s1,s2);strcpy(s1,s2);/* /* copy characters fromcopy characters from s2 s2 intointo s1 s1 */*/
/*/* be sure thatbe sure that s1 s1 can hold that many characters can hold that many characters */*/
/*/* and/or use and/or use strncpy strncpy */*/
2222Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C-style stringsC-style strings

The string copy function The string copy function strcpy()strcpy() is the archetypical C is the archetypical C
function (found in the ISO C standard library)function (found in the ISO C standard library)

Unless you understand the implementation below, don’t Unless you understand the implementation below, don’t
claim to understand C:claim to understand C:
char* strcpy(char *p, const char *q)char* strcpy(char *p, const char *q)
{{
while (*p++ = *q++);while (*p++ = *q++);
return p;return p;
}}

For an explanation see for example K&R or TC++PL4For an explanation see for example K&R or TC++PL4
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 2323

Standard function librariesStandard function libraries

<stdio.h><stdio.h> printf()printf(),, scanf() scanf(), etc., etc.

<string.h> <string.h> strcmp()strcmp(), etc., etc.

<ctype.c> <ctype.c> isspace()isspace(),, etc.etc.

<stdlib.h> <stdlib.h> malloc()malloc(),, etc.etc.

<math.h> <math.h> sqrt()sqrt(),, etc.etc.

Warning: By default, Microsoft tries to force you to use safer, Warning: By default, Microsoft tries to force you to use safer,
but non-standard, alternatives to the unsafe C standard library but non-standard, alternatives to the unsafe C standard library
functionsfunctions
2424Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Free store: malloc()/free()Free store: malloc()/free()
#include <stdlib.h>#include <stdlib.h>
void f(int n) {void f(int n) {
/* /* malloc() malloc() takes a number of bytes as its argument takes a number of bytes as its argument */*/
int* p = (int*)malloc(sizeof(int)*n);int* p = (int*)malloc(sizeof(int)*n);/* /* allocate an array of allocate an array of n intn ints s */*/
/* /* …… */ */
free(p);free(p);/* /* free() free() returns memory allocated byreturns memory allocated by malloc() malloc() to free store to free store */*/
}}
2525Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Free store: malloc()/free()Free store: malloc()/free()

Little compile-time checkingLittle compile-time checking
/*/* malloc()malloc() returns a returns a void*. void*. You can leave out the cast of malloc(), but donYou can leave out the cast of malloc(), but don’t ’t */*/
double* p = malloc(sizeof(int)*n);double* p = malloc(sizeof(int)*n);/* /* probably a bug probably a bug */*/

Little run-time checkingLittle run-time checking
int* q = malloc(sizeof(int)*m); /* int* q = malloc(sizeof(int)*m); /* m intm ints s */*/
for (int i=0; i<n; ++i) init(q[i]); /* for (int i=0; i<n; ++i) init(q[i]); /* initialize initialize intints (Eh?) s (Eh?) */*/

No initialization/cleanupNo initialization/cleanup

malloc() malloc() doesndoesn’t call constructors’t call constructors

free()free() doesn doesn’t call destructors’t call destructors

Write and remember to use your own Write and remember to use your own init() init() and and cleanup()cleanup()

There is no way to ensure automatic cleanupThere is no way to ensure automatic cleanup

Don’t useDon’t use malloc()/free() malloc()/free() in C++ programsin C++ programs

new/delete new/delete are as fast and almost always betterare as fast and almost always better
2626Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Uncast Uncast malloc()malloc()

The major C/C++ incompatibility in real-world codeThe major C/C++ incompatibility in real-world code

Not-type safeNot-type safe

Historically a pre-standard C compatibility hack/featureHistorically a pre-standard C compatibility hack/feature

Always controversialAlways controversial

Unnecessarily so IMOUnnecessarily so IMO
void* malloc(size_t x);void* malloc(size_t x);/*/* allocate x bytesallocate x bytes */ */
/*/*in C, but not in C++, in C, but not in C++, void* void* converts to any converts to any T* T* */*/
void f (int n)void f (int n)
{{
int* p = malloc(n*sizeof(int)); int* p = malloc(n*sizeof(int)); /*/* ok in C; error in C++ ok in C; error in C++ */*/
int* q = (int*)malloc(n*sizeof(int)); int* q = (int*)malloc(n*sizeof(int)); /*/* ok in C and C++ ok in C and C++ */*/
/* /* … */… */
}}
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 2727

void*void*

Why does Why does void*void* convert to convert to T*T* in C but not in C++? in C but not in C++?

C needs it to save you from casting the result of C needs it to save you from casting the result of malloc()malloc()

C++ does not: use C++ does not: use newnew

Why is a Why is a void*void* to to T*T* conversion not type safe? conversion not type safe?
void f()void f()
{{
char i = 0;char i = 0;
char j = 0;char j = 0;
char* p = &i;char* p = &i;
void* q = p;void* q = p;
int* pp = q;int* pp = q;/* /* unsafe, legal C; error in C++unsafe, legal C; error in C++ */*/
*pp = -1;*pp = -1; /*/* overwrite memory starting at &ioverwrite memory starting at &i */*/
}}
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 2828

// Comments// Comments

introduced by Bjarne Stroustrup into C++ from C’s ancestor introduced by Bjarne Stroustrup into C++ from C’s ancestor
BCPL when he got really fed up with typing BCPL when he got really fed up with typing /*/* … … */*/
commentscomments

are accepted by most C dialects including the C99 and C11are accepted by most C dialects including the C99 and C11
2929Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

constconst
// // in C, ain C, a const const is never a compile time constant is never a compile time constant
const int max = 30;const int max = 30;
const int x;const int x;// // const const not initialized: ok in C (error in C++)not initialized: ok in C (error in C++)
void f(int v)void f(int v)
{{
int a1[max];int a1[max];// // error: array bound not a constant (error: array bound not a constant (max max is not a constant!)is not a constant!)
int a2[x];int a2[x]; // // error: array bound not a constanterror: array bound not a constant ( (here you see why)here you see why)
switch (v) {switch (v) {
case 1:case 1:
// // ……
case max:case max: // // error: case label not a constanterror: case label not a constant
// // ……
}}
}}
3030Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Instead of Instead of constconst use macros use macros
#define max 30#define max 30
void f(int v)void f(int v)
{{
int a1[max];int a1[max];// // okok
switch (v) {switch (v) {
case 1:case 1:
// // ……
case max:case max: // // okok
// // ……
}}
}}
3131Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Beware of macrosBeware of macros
#include "my_header.h"#include "my_header.h"
// // ……
int max(int a, int b) { return a>=b?a:b; }int max(int a, int b) { return a>=b?a:b; }// // error: “obscure error message”error: “obscure error message”

As it happened As it happened my_header.hmy_header.h contained the macro contained the macro maxmax from the previous slide from the previous slide
so what the compiler saw wasso what the compiler saw was
int 30(int a, int b) { return a>=b?a:b; }int 30(int a, int b) { return a>=b?a:b; }

No wonder it complained!No wonder it complained!

There are tens of thousands of macros in popular header files.There are tens of thousands of macros in popular header files.

Always define macros with Always define macros with ALL_CAPSALL_CAPS names, e.g. names, e.g.
#define MY_MAX 30#define MY_MAX 30

NNever give anything but a macro an ever give anything but a macro an ALL_CAPSALL_CAPS name name

Unfortunately, not everyone obeys the ALL_CAPS conventionUnfortunately, not everyone obeys the ALL_CAPS convention
3232Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

C/C++ interoperabilityC/C++ interoperability

Works because of shared linkage modelWorks because of shared linkage model

Works because a shared model for simple objectsWorks because a shared model for simple objects

built-in types and structs/classesbuilt-in types and structs/classes

Optimal/EfficientOptimal/Efficient

No behind-the-scenes reformatting/conversionsNo behind-the-scenes reformatting/conversions
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 3333

Calling C from C++Calling C from C++

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

Calling C++ from CCalling C++ from C

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

ABIsABIs

Application Binary InterfaceApplication Binary Interface

An interface we can use without recompiling the implementationAn interface we can use without recompiling the implementation

The problemThe problem
struct file {struct file {
mode_t f_mode;mode_t f_mode;
loff_t f_pos;loff_t f_pos;
unsigned short f_flags;unsigned short f_flags;
unsigned short f_count;unsigned short f_count;
unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
struct file *f_next, *f_prev; int f_owner; /* pid or -pgrp where SIGIO should be sent */struct file *f_next, *f_prev; int f_owner; /* pid or -pgrp where SIGIO should be sent */
struct inode * f_inode;struct inode * f_inode;
struct file_operations * f_op;struct file_operations * f_op;
unsigned long f_version;unsigned long f_version;
void *private_data; /* needed for tty driver, and maybe others */void *private_data; /* needed for tty driver, and maybe others */
};};
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 3636

ABIABI

A solution:A solution:

Access exclusively through functionsAccess exclusively through functions

For exampleFor example
FILE* fopen(const char* name, const char* mode);FILE* fopen(const char* name, const char* mode);
printf(FILE*, const char* format, …);printf(FILE*, const char* format, …);
int fclose(FILE*);int fclose(FILE*);

And And NEVERNEVER use that FILE directly, just pass the FILE* use that FILE directly, just pass the FILE*
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 3737

ABIABI

C++ alternativesC++ alternatives

Use a functional ABI (exactly like C)Use a functional ABI (exactly like C)

Use a pure abstract classUse a pure abstract class
struct Device {struct Device {
virtual void open() = 0;virtual void open() = 0;
virtual void close() = 0;virtual void close() = 0;
virtual Status read_into(char*) = 0;virtual Status read_into(char*) = 0;
virtual Status write_from(const char*) = 0;virtual Status write_from(const char*) = 0;
// …// …
};};

ABIs; why not?ABIs; why not?

PerformancePerformance

FlexibilityFlexibility
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 3838

Word counting example Word counting example (C++ version)(C++ version)
#include <map>#include <map>
#include <string>#include <string>
#include <iostream>#include <iostream>
using namespace std;using namespace std;
int main()int main()
{{
map<string,int> m;map<string,int> m;
for (string s; cin>>s; )for (string s; cin>>s; )
m[s]++;m[s]++;
for(const auto& p : m)for(const auto& p : m)
cout << p.first << " : " << p.second << "\n";cout << p.first << " : " << p.second << "\n";
}}
3939Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Word counting example Word counting example (C version)(C version)
// // word_freq.c word_freq.c
// // Walter C. Daugherity Walter C. Daugherity
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>#include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_WORDS 1000 /*#define MAX_WORDS 1000 /* max unique words to count max unique words to count */*/
#define MAX_WORD_LENGTH 100#define MAX_WORD_LENGTH 100
#define STR(s) #s#define STR(s) #s /* /* macros for scanf format macros for scanf format */*/
#define XSTR(s) STR(s)#define XSTR(s) STR(s)
typedef struct record {typedef struct record {
char word[MAX_WORD_LENGTH + 1];char word[MAX_WORD_LENGTH + 1];
int count;int count;
} record;} record;
4040Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Word counting example Word counting example (C version)(C version)
int main()int main()
{{
// // … read words and build table …… read words and build table …
qsort(table, num_words, sizeof(record), strcmp);qsort(table, num_words, sizeof(record), strcmp);
for (iter=0; iter<num_words; ++iter)for (iter=0; iter<num_words; ++iter)
printf("%s %d\n",table[iter].word,table[iter].count);printf("%s %d\n",table[iter].word,table[iter].count);
return EXIT_SUCCESS; return EXIT_SUCCESS;
}}
4141Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13

Word counting example Word counting example (most of main)(most of main)
record table[MAX_WORDS + 1];record table[MAX_WORDS + 1];
int num_words = 0;int num_words = 0;
char word[MAX_WORD_LENGTH + 1];char word[MAX_WORD_LENGTH + 1];
int iter;int iter;
while (scanf("%" XSTR(MAX_WORD_LENGTH) "s", word) != EOF) {while (scanf("%" XSTR(MAX_WORD_LENGTH) "s", word) != EOF) {
for (iter = 0; iter < num_words && strcmp(table[iter].word, word); ++iter);for (iter = 0; iter < num_words && strcmp(table[iter].word, word); ++iter);
if (iter == num_words) {if (iter == num_words) {
strncpy(table[num_words].word, word, MAX_WORD_LENGTH + 1);strncpy(table[num_words].word, word, MAX_WORD_LENGTH + 1);
table[num_words++].count = 1;table[num_words++].count = 1;
}}
else table[iter].count++;else table[iter].count++;
if (num_words > MAX_WORDS){if (num_words > MAX_WORDS){
printf("table is full\n");printf("table is full\n");
return EXIT_FAILURE;return EXIT_FAILURE;
}}
}}
4242Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13
“too clever by half”

Word counting example Word counting example (C version)(C version)

In (some) colloquial C style In (some) colloquial C style (not written by BS)(not written by BS)

ItIt’s so long and complicated! How do I know it’s correct?’s so long and complicated! How do I know it’s correct?

My first reaction – BSMy first reaction – BS

See, you donSee, you don’t need any fancy and complicated language features!!!’t need any fancy and complicated language features!!!

not my comment – BSnot my comment – BS

IMHO not a very good problem for using CIMHO not a very good problem for using C

Not an atypical application, but not low-level systems programmingNot an atypical application, but not low-level systems programming

ItIt’s also C++ except the argument to ’s also C++ except the argument to qsort() qsort() should be cast to its proper type:should be cast to its proper type:

(int (*)(const void*, const void*))strcmp // (int (*)(const void*, const void*))strcmp // cast needed in C++cast needed in C++

What are those macros doing? What are those macros doing?

Maxes out at Maxes out at MAX_WORDMAX_WORD words words

DoesnDoesn’t handle words longer than ’t handle words longer than MAX_WORD_LENGTHMAX_WORD_LENGTH

First reads and then sortsFirst reads and then sorts

Inherently slower than the colloquial C++ version (which uses a Inherently slower than the colloquial C++ version (which uses a mapmap))
Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13 4343

More informationMore information

Kernighan & Ritchie: The C Programming LanguageKernighan & Ritchie: The C Programming Language

The classicThe classic

Stroustrup: TC++PL4, Chapter 44: CompatibilityStroustrup: TC++PL4, Chapter 44: Compatibility

C/C++ incompatibilities, on my home pagesC/C++ incompatibilities, on my home pages

Stroustrup: Learning Standard C++ as a New Language.Stroustrup: Learning Standard C++ as a New Language.

Style and technique comparisonsStyle and technique comparisons

www.research.att.com/~bs/new_learning.pdfwww.research.att.com/~bs/new_learning.pdf

Lots of book reviews: www.accu.orgLots of book reviews: www.accu.org
4444Stroustrup/PPP - Dec'13Stroustrup/PPP - Dec'13