SlideShare a Scribd company logo
1 of 39
Chapter 27
        The C Programming Language


                      Bjarne Stroustrup
                    www.stroustrup.com/Programming




Dennis M. Ritchie
Abstract
   This lecture gives you the briefest introduction to C
    from a C++ point of view. If you need to use this
    language, read an introductory book (e.g. K&R). This
    lecture gives you a hint what to look for.
   C is C++’s closest relative, and compatible in many
    areas, so much of your C++ knowledge carries over.




                    Stroustrup/PPP - Oct'11             2
Overview
   C and C++
   Function prototypes
   printf()/scanf()
   Arrays and strings
   Memory management
   Macros
   const
   C/C++ interoperability



                   Stroustrup/PPP - Oct'11   3
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 - Oct'11                 4
Modern C and C++ are siblings




        Stroustrup/PPP - Oct'11   5
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
   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 - Oct'11                                   6
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 - Oct'11               7
C and C++
   Here we
       assume you know C++ and how to use it
       describe the differences between C and C++
       describe how to program using the facilities offered by C
            Our ideal of programming and our techniques remain the same, but
             the tool available to express our ideas change
       describe a few C “traps and pitfalls”
       don’t go into all the details from the book
            Compatibility details are important, but rarely interesting




                            Stroustrup/PPP - Oct'11                         8
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 - Oct'11                            9
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 - Oct'11                      10
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 3rd party libraries
       Iostreams
            Use stdio: printf(), getch(), etc.
       Regular expression
            Use a 3rd party library

                              Stroustrup/PPP - Oct'11               11
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 - Oct'11                             12
Functions
   There can be only one function of a given name
   Function argument type checking is optional
   There are no references (and therefore no pass-by-reference)
   There are no member functions
   There are no inline functions (except in C99)
   There is an alternative function definition syntax




                      Stroustrup/PPP - Oct'11                      13
Function prototypes
                 (function argument checking is optional)

/* avoid these mistakes – use a compiler option that enforces C++ rules */
int g(int);       /* prototype – like C++ function declaration */
int h();          /* not a prototype – the argument types are unspecified */
int f(p,b) char* p; char b;            /* old style definition – not a prototype */
{ /* … */ }
int my_fct(int a, double d, char* p)         /* new style definition – a prototype */
{
    f();          /* ok by the compiler! But gives wrong/unexpected results */
    f(d,p);       /* ok by the compiler! But gives wrong/unexpected results */
    h(d);         /* ok by the compiler! But may give wrong/unexpected results */
    ff(d);        /* ok by the compiler! But may give wrong/unexpected results */
    g(p);         /* error: wrong type */
    g();          /* error: argument missing */
}
                              Stroustrup/PPP - Oct'11                                 14
printf() – many people’s favorite C function
                                                                      Format string

/* no iostreams – use stdio */
#include<stdio.h>      /* defines int printf(const char* format, …); */

int main(void)
{
    printf("Hello, worldn");
    return 0;                                        Arguments to be formatted
}
void f(double d, char* s, int i, char ch)
{
   printf("double %g string %s int %i char %cn", d, s, i, ch);
   printf("goof %sn", i); /* uncaught error */
}

                                   Formatting characters
Format strings
                           Stroustrup/PPP - Oct'11                                    15
scanf() and friends
/* the most popular input functions from <stdio.h>: */
int i = getchar(); /* note int, not char;
                       getchar() returns EOF when it reaches end of file */
p = gets();         /* read 'n' terminated line into char array pointed to by p */

void f(int* pi, char* pc, double* pd, char* ps)
{ /* read into variables whose addresses are passed as pointers: */
    scanf("%i %c %g %s", pi, pc, pd, ps);
    /* %s skips initial whitespace and is terminated by whitespace */
}
int i; char c; double d; char s[100]; f(&i, &c, &d, s); /* call to assign to i, c, d, and s */


    Don’t ever use gets() or scanf("%s")!
      Consider them poisoned
      They are the source of many security violations
      An overflow is easily arranged and easily exploitable
      Use getchar()


                               Stroustrup/PPP - Oct'11                                    16
printf() and scanf() are not type safe

    double d = 0;
    int s = 0;
    printf("d: %d , s: %sn", d, s);      /* compiles and runs
                                             the result might surprise you */


                                   “s” for “string”

     “d” for “decimal”, not “double”
    Though error-prone, printf() is convenient for built-in types
    printf() formats are not extensible to user-defined types
        E.g. no %M for My_type values
    Beware: a printf () with a user-supplied format string is a cracker tool

                               Stroustrup/PPP - Oct'11                          17
Arrays and pointers
   Defined almost exactly as in C++
   In C, you have to use them essentially all the time
       because there is no vector, map, string, etc.
   Remember
       An array doesn’t know how long it is
       There is no array assignment
            use memcpy()
       A C-style string is a zero-terminated array




                            Stroustrup/PPP - Oct'11       18
C-style strings
   In C a string (called a C-string or a C-style string in C++
    literature) is a zero-terminated array of characters
    char* p = "asdf";
    char s[ ] = "asdf";



      p:                           'a'    's'    'd'    'f'    0


                             s:     'a'    's'    'd'    'f'   0




                          Stroustrup/PPP - Oct'11                  19
C-style strings
   Comparing strings
    #include <string.h>
    if (s1 = = s2) {                   /* do s1 and s2 point to the same array?
                                          (typically not what you want) */
    }
    if (strcmp(s1,s2) = = 0) { /* do s1 and s2 hold the same characters? */
    }

   Finding the lengths of a string
    int lgt = strlen(s);    /* note: goes through the string at run time
                               looking for the terminating 0 */
   Copying strings
    strcpy(s1,s2);          /* copy characters from s2 into s1
                               be sure that s1 can hold that many characters */

                            Stroustrup/PPP - Oct'11                               20
C-style strings
   The string copy function strcpy() is the archetypical
    C function (found in the ISO C standard library)
   Unless you understand the implementation below,
    don’t claim to understand C:

        char* strcpy(char *p, const char *q)
        {
            while (*p++ = *q++);
            return p;
        }


   For an explanation see for example K&R or TC++PL
                       Stroustrup/PPP - Oct'11              21
Standard function libraries
   <stdio.h>     printf(), scanf(), etc.
   <string.h>   strcmp(), etc.
   <ctype.c>    isspace(), etc.
   <stdlib.h>   malloc(), etc.
   <math.h>     sqrt(), etc.


   Warning: By default, Microsoft tries to force you to use safer,
    but non-standard, alternatives to the unsafe C standard library
    functions




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




                           Stroustrup/PPP - Oct'11                            23
Free store: malloc()/free()
   Little compile-time checking
    /* malloc() returns a void*. You can leave out the cast of malloc(), but don’t
       */
    double* p = malloc(sizeof(int)*n);      /* probably a bug */
   Little run-time checking
    int* q = malloc(sizeof(int)*m); /* m ints */
    for (int i=0; i<n; ++i) init(q[i]);
   No initialization/cleanup
       malloc() doesn’t call constructors
       free() doesn’t call destructors
       Write and remember to use your own init() and cleanup()
   There is no way to ensure automatic cleanup
   Don’t use malloc()/free() in C++ programs
       new/delete are as fast and almostOct'11 better
                            Stroustrup/PPP - always                           24
Uncast malloc()
   The major C/C++ incompatibility in real-world code
       Not-type safe
       Historically a pre-standard C compatibility hack/feature
   Always controversial
       Unnecessarily so IMO

void* alloc(size_t x);      /* allocate x bytes
                              in C, but not in C++, void* converts to any T* */
void f (int n)
{
   int* p = alloc(n*sizeof(int));                 /* ok in C; error in C++ */
   int* q = (int*)alloc(n*sizeof(int));           /* ok in C and C++ */
   /* … */
}                         Stroustrup/PPP - Oct'11                               25
void*
   Why does void* convert to T* in C but not in C++?
       C needs it to save you from casting the result of malloc()
       C++ does not: use new
   Why is a void* to T* conversion not type safe?
    void f()
      {
        char i = 0;
        char j = 0;
        char* p = &i;
        void* q = p;
        int* pp = q;       /* unsafe, legal C; error in C++ */
        *pp = -1;          /* overwrite memory starting at &i */
      }
                           Stroustrup/PPP - Oct'11                   26
Comments
   // comments were introduced by Bjarne Stroustrup into C++
    from C’s ancestor BCPL when he got really fed up with typing
    /* … */ comments
   // comments are accepted by most C dialects including the new
    ISO standard C (C99)




                      Stroustrup/PPP - Oct'11                  27
const
// in C, a const is never a compile time constant
const int max = 30;
const int x;        // const not initialized: ok in C (error in C++)

void f(int v)
{
   int a1[max]; // error: array bound not a constant (max is not a constant!)
   int a2[x];   // error: array bound not a constant (here you see why)
   switch (v) {
   case 1:
         // …
   case max:    // error: case label not a constant
         // …
   }
}                        Stroustrup/PPP - Oct'11                           28
Instead of const use macros
#define max 30

void f(int v)
{
   int a1[max]; // ok
   switch (v) {
   case 1:
         // …
   case max:    // ok
         // …
   }
}



                        Stroustrup/PPP - Oct'11   29
Beware of macros
#include "my_header.h"
// …
int max(int a, int b) { return a>=b?a:b; } // error: “obscure error message”

   As it happened my_header.h contained the macro max from the previous
    slide so what the compiler saw was
          int 30(int a, int b) { return a>=b?a:b; }
   No wonder it complained!
   There are tens of thousands of macros in popular header files.
   Always define macros with ALL_CAPS names, e.g.
         #define MY_MAX 30
    and never give anything but a macro an ALL_CAPS name
   Unfortunately, not everyone obeys the ALL_CAPS convention


                          Stroustrup/PPP - Oct'11                        30
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 - Oct'11         31
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 sr = sqrt(2);
                // …
       }

                          Stroustrup/PPP - Oct'11                        32
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 - Oct'11                             33
Word counting example (C++ version)

#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    map<string,int> m;
    string s;
    while (cin>>s) m[s]++;         // use getline() if you really want lines
    for(map<string,int>::iterator p = m.begin(); p!=m.end(); ++p)
         cout << p->first << " : " << p->second << "n";
}
                        Stroustrup/PPP - Oct'11                                34
Word counting example (C version)
// word_freq.c
// Walter C. Daugherity

#include <stdio.h>
#include <stdlib.h>
 #include <string.h>

#define MAX_WORDS 1000 /* max unique words to count */
#define MAX_WORD_LENGTH 100

#define STR(s) #s            /* macros for scanf format */
#define XSTR(s) STR(s)

typedef struct record{
    char word[MAX_WORD_LENGTH + 1];
    int count;
} record;
                          Stroustrup/PPP - Oct'11            35
Word counting example (C version)
int main()
{
    // … read words and build table …
    qsort(table, num_words, sizeof(record), strcmp);
    for(iter=0; iter<num_words; ++iter)
           printf("%s %dn",table[iter].word,table[iter].count);
    return EXIT_SUCCESS;
}




                         Stroustrup/PPP - Oct'11                   36
Word counting example (most of main)
record table[MAX_WORDS + 1];
int num_words = 0;
char word[MAX_WORD_LENGTH + 1];
int iter;
while(scanf("%" XSTR(MAX_WORD_LENGTH) "s", word) != EOF) {
   for(iter = 0; iter < num_words && strcmp(table[iter].word, word); ++iter);
   if(iter == num_words) {
       strncpy(table[num_words].word, word, MAX_WORD_LENGTH + 1);
       table[num_words++].count = 1;
   }
   else table[iter].count++;
   if(num_words > MAX_WORDS){
       printf("table is fulln");
       return EXIT_FAILURE;
   }
}
                        Stroustrup/PPP - Oct'11                                 37
Word counting example (C version)
   Comments
       In (some) colloquial C style (not written by BS)
       It’s so long and complicated! (my first reaction – BS)
       See, you don’t need any fancy and complicated language features!!!
        (not my comment – BS)
       IMHO not a very good problem for using C
            Not an atypical application, but not low-level systems programming
       It’s also C++ except that in C++, the argument to qsort() should be
        cast to its proper type:
            (int (*)(const void*, const void*))strcmp
       What are those macros doing?
       Maxes out at MAX_WORD words
       Doesn’t handle words longer than MAX_WORD_LENGTH
       First reads and then sorts
            Inherently slower than the colloquial C++ version (which uses a map)
                               Stroustrup/PPP - Oct'11                              38
More information
   Kernighan & Ritchie: The C Programming Language
        The classic
   Stroustrup: TC++PL, Appendix B: Compatibility
        C/C++ incompatibilities, on my home pages
   Stroustrup: Learning Standard C++ as a New Language.
        Style and technique comparisons
        www.research.att.com/~bs/new_learning.pdf
   Lots of book reviews: www.accu.org




                            Stroustrup/PPP - Oct'11        39

More Related Content

What's hot

C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYRajeshkumar Reddy
 
(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_iNico Ludwig
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List KataOlve Maudal
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREC and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREjatin batra
 
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...Kamiya Toshihiro
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.PVS-Studio
 
02 c++g3 d
02 c++g3 d02 c++g3 d
02 c++g3 dmahago
 
13 graph classes_2
13 graph classes_213 graph classes_2
13 graph classes_2rohikhan
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1Rumman Ansari
 

What's hot (20)

Course1
Course1Course1
Course1
 
C
CC
C
 
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDYC LANGUAGE UNIT-1 PREPARED BY MVB REDDY
C LANGUAGE UNIT-1 PREPARED BY MVB REDDY
 
(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i(8) cpp abstractions separated_compilation_and_binding_part_i
(8) cpp abstractions separated_compilation_and_binding_part_i
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTREC and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
C and C ++ Training in Ambala ! BATRA COMPUTER CENTRE
 
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
C vs c++
C vs c++C vs c++
C vs c++
 
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.
Celebrating 30-th anniversary of the first C++ compiler: let's find bugs in it.
 
02 c++g3 d
02 c++g3 d02 c++g3 d
02 c++g3 d
 
C vs c++
C vs c++C vs c++
C vs c++
 
Basic c
Basic cBasic c
Basic c
 
13 graph classes_2
13 graph classes_213 graph classes_2
13 graph classes_2
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 

Viewers also liked

Building Stronger Learning Communities
Building Stronger Learning CommunitiesBuilding Stronger Learning Communities
Building Stronger Learning CommunitiesTravis McNaughton
 
Mapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasMapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasnelviscassiani
 
1st Day Assembly Muir Lake School 2012
1st Day Assembly Muir Lake School 20121st Day Assembly Muir Lake School 2012
1st Day Assembly Muir Lake School 2012Travis McNaughton
 
Building 1-to-1 BYOD Learning Communities
Building 1-to-1 BYOD Learning Communities Building 1-to-1 BYOD Learning Communities
Building 1-to-1 BYOD Learning Communities Travis McNaughton
 
Building 1-to-1 BYOD Learning Communities Parent Presentation
Building 1-to-1 BYOD Learning Communities Parent PresentationBuilding 1-to-1 BYOD Learning Communities Parent Presentation
Building 1-to-1 BYOD Learning Communities Parent PresentationTravis McNaughton
 
Twitter For Parents Engaged In Education
Twitter For Parents Engaged In Education Twitter For Parents Engaged In Education
Twitter For Parents Engaged In Education Travis McNaughton
 
Mapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasMapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasnelviscassiani
 
Collaborate, Create, Learn Team Presentation 2012
Collaborate, Create, Learn Team Presentation 2012Collaborate, Create, Learn Team Presentation 2012
Collaborate, Create, Learn Team Presentation 2012Travis McNaughton
 
Transforming teaching & learning in 1 to-1 learning environments
Transforming teaching & learning in 1 to-1 learning environmentsTransforming teaching & learning in 1 to-1 learning environments
Transforming teaching & learning in 1 to-1 learning environmentsTravis McNaughton
 
Windows Operating System
Windows Operating SystemWindows Operating System
Windows Operating SystemTarun Nayak
 

Viewers also liked (12)

Building Stronger Learning Communities
Building Stronger Learning CommunitiesBuilding Stronger Learning Communities
Building Stronger Learning Communities
 
Mapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasMapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indias
 
Portfolio Travis McNaughton
Portfolio Travis McNaughtonPortfolio Travis McNaughton
Portfolio Travis McNaughton
 
1st Day Assembly Muir Lake School 2012
1st Day Assembly Muir Lake School 20121st Day Assembly Muir Lake School 2012
1st Day Assembly Muir Lake School 2012
 
Building 1-to-1 BYOD Learning Communities
Building 1-to-1 BYOD Learning Communities Building 1-to-1 BYOD Learning Communities
Building 1-to-1 BYOD Learning Communities
 
Building 1-to-1 BYOD Learning Communities Parent Presentation
Building 1-to-1 BYOD Learning Communities Parent PresentationBuilding 1-to-1 BYOD Learning Communities Parent Presentation
Building 1-to-1 BYOD Learning Communities Parent Presentation
 
Twitter For Parents Engaged In Education
Twitter For Parents Engaged In Education Twitter For Parents Engaged In Education
Twitter For Parents Engaged In Education
 
Mapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indiasMapa del sistema natural de cartagena de indias
Mapa del sistema natural de cartagena de indias
 
Collaborate, Create, Learn Team Presentation 2012
Collaborate, Create, Learn Team Presentation 2012Collaborate, Create, Learn Team Presentation 2012
Collaborate, Create, Learn Team Presentation 2012
 
Transforming teaching & learning in 1 to-1 learning environments
Transforming teaching & learning in 1 to-1 learning environmentsTransforming teaching & learning in 1 to-1 learning environments
Transforming teaching & learning in 1 to-1 learning environments
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
 
Windows Operating System
Windows Operating SystemWindows Operating System
Windows Operating System
 

Similar to C plus plus

27_C.ppt
27_C.ppt27_C.ppt
27_C.pptADITHU2
 
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF CTHE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF Cbhargavi804095
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184Sumit Saini
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1SURBHI SAROHA
 
1 2 programming
1 2 programming1 2 programming
1 2 programmingazimuthal
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
C++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassC++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassYogendra Rampuria
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++oggyrao
 

Similar to C plus plus (20)

27_C.ppt
27_C.ppt27_C.ppt
27_C.ppt
 
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF CTHE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
THE C PROGRAMMING LANGUAGE PPT CONTAINS THE BASICS OF C
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
C++ language
C++ languageC++ language
C++ language
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
C
CC
C
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
1 2 programming
1 2 programming1 2 programming
1 2 programming
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
4.languagebasics
4.languagebasics4.languagebasics
4.languagebasics
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
c.ppt
c.pptc.ppt
c.ppt
 
C++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassC++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of Class
 
Go1
Go1Go1
Go1
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 

Recently uploaded

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

C plus plus

  • 1. Chapter 27 The C Programming Language Bjarne Stroustrup www.stroustrup.com/Programming Dennis M. Ritchie
  • 2. Abstract  This lecture gives you the briefest introduction to C from a C++ point of view. If you need to use this language, read an introductory book (e.g. K&R). This lecture gives you a hint what to look for.  C is C++’s closest relative, and compatible in many areas, so much of your C++ knowledge carries over. Stroustrup/PPP - Oct'11 2
  • 3. Overview  C and C++  Function prototypes  printf()/scanf()  Arrays and strings  Memory management  Macros  const  C/C++ interoperability Stroustrup/PPP - Oct'11 3
  • 4. 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 - Oct'11 4
  • 5. Modern C and C++ are siblings Stroustrup/PPP - Oct'11 5
  • 6. 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  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 - Oct'11 6
  • 7. 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 - Oct'11 7
  • 8. C and C++  Here we  assume you know C++ and how to use it  describe the differences between C and C++  describe how to program using the facilities offered by C  Our ideal of programming and our techniques remain the same, but the tool available to express our ideas change  describe a few C “traps and pitfalls”  don’t go into all the details from the book  Compatibility details are important, but rarely interesting Stroustrup/PPP - Oct'11 8
  • 9. 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 - Oct'11 9
  • 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 - Oct'11 10
  • 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 3rd party libraries  Iostreams  Use stdio: printf(), getch(), etc.  Regular expression  Use a 3rd party library Stroustrup/PPP - Oct'11 11
  • 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 - Oct'11 12
  • 13. Functions  There can be only one function of a given name  Function argument type checking is optional  There are no references (and therefore no pass-by-reference)  There are no member functions  There are no inline functions (except in C99)  There is an alternative function definition syntax Stroustrup/PPP - Oct'11 13
  • 14. Function prototypes (function argument checking is optional) /* avoid these mistakes – use a compiler option that enforces C++ rules */ int g(int); /* prototype – like C++ function declaration */ int h(); /* not a prototype – the argument types are unspecified */ int f(p,b) char* p; char b; /* old style definition – not a prototype */ { /* … */ } int my_fct(int a, double d, char* p) /* new style definition – a prototype */ { f(); /* ok by the compiler! But gives wrong/unexpected results */ f(d,p); /* ok by the compiler! But gives wrong/unexpected results */ h(d); /* ok by the compiler! But may give wrong/unexpected results */ ff(d); /* ok by the compiler! But may give wrong/unexpected results */ g(p); /* error: wrong type */ g(); /* error: argument missing */ } Stroustrup/PPP - Oct'11 14
  • 15. printf() – many people’s favorite C function Format string /* no iostreams – use stdio */ #include<stdio.h> /* defines int printf(const char* format, …); */ int main(void) { printf("Hello, worldn"); return 0; Arguments to be formatted } void f(double d, char* s, int i, char ch) { printf("double %g string %s int %i char %cn", d, s, i, ch); printf("goof %sn", i); /* uncaught error */ } Formatting characters Format strings Stroustrup/PPP - Oct'11 15
  • 16. scanf() and friends /* the most popular input functions from <stdio.h>: */ int i = getchar(); /* note int, not char; getchar() returns EOF when it reaches end of file */ p = gets(); /* read 'n' terminated line into char array pointed to by p */ void f(int* pi, char* pc, double* pd, char* ps) { /* read into variables whose addresses are passed as pointers: */ scanf("%i %c %g %s", pi, pc, pd, ps); /* %s skips initial whitespace and is terminated by whitespace */ } int i; char c; double d; char s[100]; f(&i, &c, &d, s); /* call to assign to i, c, d, and s */  Don’t ever use gets() or scanf("%s")!  Consider them poisoned  They are the source of many security violations  An overflow is easily arranged and easily exploitable  Use getchar() Stroustrup/PPP - Oct'11 16
  • 17. printf() and scanf() are not type safe double d = 0; int s = 0; printf("d: %d , s: %sn", d, s); /* compiles and runs the result might surprise you */ “s” for “string” “d” for “decimal”, not “double”  Though error-prone, printf() is convenient for built-in types  printf() formats are not extensible to user-defined types  E.g. no %M for My_type values  Beware: a printf () with a user-supplied format string is a cracker tool Stroustrup/PPP - Oct'11 17
  • 18. Arrays and pointers  Defined almost exactly as in C++  In C, you have to use them essentially all the time  because there is no vector, map, string, etc.  Remember  An array doesn’t know how long it is  There is no array assignment  use memcpy()  A C-style string is a zero-terminated array Stroustrup/PPP - Oct'11 18
  • 19. C-style strings  In C a string (called a C-string or a C-style string in C++ literature) is a zero-terminated array of characters char* p = "asdf"; char s[ ] = "asdf"; p: 'a' 's' 'd' 'f' 0 s: 'a' 's' 'd' 'f' 0 Stroustrup/PPP - Oct'11 19
  • 20. C-style strings  Comparing strings #include <string.h> if (s1 = = s2) { /* do s1 and s2 point to the same array? (typically not what you want) */ } if (strcmp(s1,s2) = = 0) { /* do s1 and s2 hold the same characters? */ }  Finding the lengths of a string int lgt = strlen(s); /* note: goes through the string at run time looking for the terminating 0 */  Copying strings strcpy(s1,s2); /* copy characters from s2 into s1 be sure that s1 can hold that many characters */ Stroustrup/PPP - Oct'11 20
  • 21. C-style strings  The string copy function strcpy() is the archetypical C function (found in the ISO C standard library)  Unless you understand the implementation below, don’t claim to understand C: char* strcpy(char *p, const char *q) { while (*p++ = *q++); return p; }  For an explanation see for example K&R or TC++PL Stroustrup/PPP - Oct'11 21
  • 22. Standard function libraries  <stdio.h> printf(), scanf(), etc.  <string.h> strcmp(), etc.  <ctype.c> isspace(), etc.  <stdlib.h> malloc(), etc.  <math.h> sqrt(), etc.  Warning: By default, Microsoft tries to force you to use safer, but non-standard, alternatives to the unsafe C standard library functions Stroustrup/PPP - Oct'11 22
  • 23. Free store: malloc()/free() #include<stdlib.h> void f(int n) { /* malloc() takes a number of bytes as its argument */ int* p = (int*)malloc(sizeof(int)*n); /* allocate an array of n ints */ /* … */ free(p); /* free() returns memory allocated by malloc() to free store */ } Stroustrup/PPP - Oct'11 23
  • 24. Free store: malloc()/free()  Little compile-time checking /* malloc() returns a void*. You can leave out the cast of malloc(), but don’t */ double* p = malloc(sizeof(int)*n); /* probably a bug */  Little run-time checking int* q = malloc(sizeof(int)*m); /* m ints */ for (int i=0; i<n; ++i) init(q[i]);  No initialization/cleanup  malloc() doesn’t call constructors  free() doesn’t call destructors  Write and remember to use your own init() and cleanup()  There is no way to ensure automatic cleanup  Don’t use malloc()/free() in C++ programs  new/delete are as fast and almostOct'11 better Stroustrup/PPP - always 24
  • 25. Uncast malloc()  The major C/C++ incompatibility in real-world code  Not-type safe  Historically a pre-standard C compatibility hack/feature  Always controversial  Unnecessarily so IMO void* alloc(size_t x); /* allocate x bytes in C, but not in C++, void* converts to any T* */ void f (int n) { int* p = alloc(n*sizeof(int)); /* ok in C; error in C++ */ int* q = (int*)alloc(n*sizeof(int)); /* ok in C and C++ */ /* … */ } Stroustrup/PPP - Oct'11 25
  • 26. void*  Why does void* convert to T* in C but not in C++?  C needs it to save you from casting the result of malloc()  C++ does not: use new  Why is a void* to T* conversion not type safe? void f() { char i = 0; char j = 0; char* p = &i; void* q = p; int* pp = q; /* unsafe, legal C; error in C++ */ *pp = -1; /* overwrite memory starting at &i */ } Stroustrup/PPP - Oct'11 26
  • 27. Comments  // comments were introduced by Bjarne Stroustrup into C++ from C’s ancestor BCPL when he got really fed up with typing /* … */ comments  // comments are accepted by most C dialects including the new ISO standard C (C99) Stroustrup/PPP - Oct'11 27
  • 28. const // in C, a const is never a compile time constant const int max = 30; const int x; // const not initialized: ok in C (error in C++) void f(int v) { int a1[max]; // error: array bound not a constant (max is not a constant!) int a2[x]; // error: array bound not a constant (here you see why) switch (v) { case 1: // … case max: // error: case label not a constant // … } } Stroustrup/PPP - Oct'11 28
  • 29. Instead of const use macros #define max 30 void f(int v) { int a1[max]; // ok switch (v) { case 1: // … case max: // ok // … } } Stroustrup/PPP - Oct'11 29
  • 30. Beware of macros #include "my_header.h" // … int max(int a, int b) { return a>=b?a:b; } // error: “obscure error message”  As it happened my_header.h contained the macro max from the previous slide so what the compiler saw was int 30(int a, int b) { return a>=b?a:b; }  No wonder it complained!  There are tens of thousands of macros in popular header files.  Always define macros with ALL_CAPS names, e.g. #define MY_MAX 30 and never give anything but a macro an ALL_CAPS name  Unfortunately, not everyone obeys the ALL_CAPS convention Stroustrup/PPP - Oct'11 30
  • 31. 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 - Oct'11 31
  • 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 sr = sqrt(2); // … } Stroustrup/PPP - Oct'11 32
  • 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 - Oct'11 33
  • 34. Word counting example (C++ version) #include<map> #include<string> #include<iostream> using namespace std; int main() { map<string,int> m; string s; while (cin>>s) m[s]++; // use getline() if you really want lines for(map<string,int>::iterator p = m.begin(); p!=m.end(); ++p) cout << p->first << " : " << p->second << "n"; } Stroustrup/PPP - Oct'11 34
  • 35. Word counting example (C version) // word_freq.c // Walter C. Daugherity #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WORDS 1000 /* max unique words to count */ #define MAX_WORD_LENGTH 100 #define STR(s) #s /* macros for scanf format */ #define XSTR(s) STR(s) typedef struct record{ char word[MAX_WORD_LENGTH + 1]; int count; } record; Stroustrup/PPP - Oct'11 35
  • 36. Word counting example (C version) int main() { // … read words and build table … qsort(table, num_words, sizeof(record), strcmp); for(iter=0; iter<num_words; ++iter) printf("%s %dn",table[iter].word,table[iter].count); return EXIT_SUCCESS; } Stroustrup/PPP - Oct'11 36
  • 37. Word counting example (most of main) record table[MAX_WORDS + 1]; int num_words = 0; char word[MAX_WORD_LENGTH + 1]; int iter; while(scanf("%" XSTR(MAX_WORD_LENGTH) "s", word) != EOF) { for(iter = 0; iter < num_words && strcmp(table[iter].word, word); ++iter); if(iter == num_words) { strncpy(table[num_words].word, word, MAX_WORD_LENGTH + 1); table[num_words++].count = 1; } else table[iter].count++; if(num_words > MAX_WORDS){ printf("table is fulln"); return EXIT_FAILURE; } } Stroustrup/PPP - Oct'11 37
  • 38. Word counting example (C version)  Comments  In (some) colloquial C style (not written by BS)  It’s so long and complicated! (my first reaction – BS)  See, you don’t need any fancy and complicated language features!!! (not my comment – BS)  IMHO not a very good problem for using C  Not an atypical application, but not low-level systems programming  It’s also C++ except that in C++, the argument to qsort() should be cast to its proper type:  (int (*)(const void*, const void*))strcmp  What are those macros doing?  Maxes out at MAX_WORD words  Doesn’t handle words longer than MAX_WORD_LENGTH  First reads and then sorts  Inherently slower than the colloquial C++ version (which uses a map) Stroustrup/PPP - Oct'11 38
  • 39. More information  Kernighan & Ritchie: The C Programming Language  The classic  Stroustrup: TC++PL, Appendix B: Compatibility  C/C++ incompatibilities, on my home pages  Stroustrup: Learning Standard C++ as a New Language.  Style and technique comparisons  www.research.att.com/~bs/new_learning.pdf  Lots of book reviews: www.accu.org Stroustrup/PPP - Oct'11 39