Chapter 27The C Programming Language.ppt
《Chapter 27The C Programming Language.ppt》由会员分享,可在线阅读,更多相关《Chapter 27The C Programming Language.ppt(44页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 27 The C Programming Language,Bjarne S 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 com
2、patible in many areas, so much of your C+ knowledge carries over.,2,Stroustrup/PPP - Dec13,Overview,C and C+ Function prototypes printf()/scanf() Arrays and strings Memory management Macros const C/C+ interoperability ABIs An example,3,Stroustrup/PPP - Dec13,C and C+,Both were “born” in the Computer
3、 Science Research Department of Bell Labs in Murray Hill, NJ,4,dmr ken bwk bs doug ,Stroustrup/PPP - Dec13,Modern C and C+ are siblings,Stroustrup/PPP - Dec13,5,C+11,C11,C+14,C and C+,In this talk, I use “C” to mean “ISO C89” Thats by far the most commonly used definition of C Classic C has mostly b
4、een replaced (though amazingly not completely) C99 is not yet widely used, C11 may be catching on Source compatibility C is (almost) a subset of C+ Example of exception: int f(int new, int class, int bool); /* ok in C */ (Almost) all constructs that are both C and C+ have the same meaning (semantics
5、) 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 incom
6、patibilities are related to C+s stricter type checking,6,Stroustrup/PPP - Dec13,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
7、 primarily aimed at and are heavily used for hard system programming tasks, such as Operating systems kernels Device drivers Embedded systems Compilers Communications systems,7,Stroustrup/PPP - Dec13,C and C+,C is arguably the most successful programming language of all time But how would you decide
8、? Number of programs written Importance of programs written Number of programmers Longevity Influence on other languages Benefits/development_cost Alternatives Fortran Cobol Lisp C+ Java PHP Python ,8,Stroustrup/PPP - Dec13,C and C+,Here we assume you know C+ and how to use it describe the differenc
9、es 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” Dont go into all the details from the book Compatibility details are imp
10、ortant, but rarely interesting,Stroustrup/PPP - Dec13,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,10,Stroustrup/PPP - Dec13,C: Functions
11、and structs Machine model (basic types and operations) Compilation and linkage model,C and C+,In C, borrowed from C+ Function prototypes (declaration and checking of function arguments) Function declaration notation: void f(int x, double y); / comments const (imperfectly) inline (imperfectly) Initia
12、lizers in for loops: for (int i = 0; /* */ Declarations after statements complex (sort of) bool (sort of) Ban on “implicit int”: int a; f() return 2; I have never seen a program that could be written better in C than in C+ I dont think such a program could exist,Stroustrup/PPP - Dec13,11,Missing in
13、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? Templat
14、es 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,12,Stroustrup/PPP - Dec13,Missing in C (from a C+ perspecti
15、ve),With no classes, templates, and exceptions, C cant 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
16、 can Write your own, use 3rd party libraries I/O streams Use stdio: printf(), getch(), etc. Regular expression Use a 3rd party library,13,Stroustrup/PPP - Dec13,C and C+,Lots of useful code is written in C Very few language features are essential In principle, you dont need a high-level language, yo
17、u 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
18、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 - Dec13,14,Functions,There can be only one function of a given name Function argument type checking i
19、s optional Use a compiler option that makes it compulsory There are no references (and therefore no pass-by-reference) pass a pointer There are no member functions There is an alternative function definition syntax,Stroustrup/PPP - Dec13,15,Function prototypes (function argument checking is optional
20、),/* 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,
21、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/u
22、nexpected results */g(p); /* error: wrong type */g(); /* error: argument missing */ ,16,Stroustrup/PPP - Dec13,printf() many peoples favorite C function,/* no iostreams use stdio */#include /* defines int printf(const char* format, ); */int main(void) printf(“Hello, worldn“);return 0; void f(double
23、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 */ ,17,Format strings,Formatting characters,Arguments to be formatted,Format string,Stroustrup/PPP - Dec13,scanf() and friends,/* the most popular input functions from : */
24、 int i = getchar(); /* note int, not char;getchar() returns EOF when it reaches end of file */ char* q = gets(p); /* read n terminated line into char array pointed to by p */* sets q to p if read succeeds; sets q to NULL if read fails */void f(int* pi, char* pc, double* pd, char* ps) /* read into va
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER27THECPROGRAMMINGLANGUAGEPPT
