Example: biology

Essential C - Stanford CS Ed Library

Essential CBy Nick ParlanteCopyright 1996-2003, Nick ParlanteThis Stanford CS Education document tries to summarize all the basic features of the Clanguage. The coverage is pretty quick, so it is most appropriate as review or for someonewith some programming background in another language. Topics include variables, inttypes, floating point types, promotion, truncation, operators, control structures (if, while,for), functions, value parameters, reference parameters, structs, pointers, arrays, the pre-processor, and the standard C Library most recent version is always maintained at its Stanford CS Education Library Please send your comments hope you can share and enjoy this document in the spirit of goodwill in which it is givenaway -- Nick Parlante, 4/2003, Stanford CS Education LibraryThis is document #101, Essential C, in the StanfordCS Education Library .

Stanford CS Education Library This is document #101, Essential C, in the Stanford ... which for years was the bible for all C programmers. Written by the original designers of the language. The explanations are pretty short, so this book is better as a ... basics of pointers and C, these problems are a good way to get more practice. 3 Section 1

Tags:

  Basics, Bible, Essential, C essentials

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Essential C - Stanford CS Ed Library

1 Essential CBy Nick ParlanteCopyright 1996-2003, Nick ParlanteThis Stanford CS Education document tries to summarize all the basic features of the Clanguage. The coverage is pretty quick, so it is most appropriate as review or for someonewith some programming background in another language. Topics include variables, inttypes, floating point types, promotion, truncation, operators, control structures (if, while,for), functions, value parameters, reference parameters, structs, pointers, arrays, the pre-processor, and the standard C Library most recent version is always maintained at its Stanford CS Education Library Please send your comments hope you can share and enjoy this document in the spirit of goodwill in which it is givenaway -- Nick Parlante, 4/2003, Stanford CS Education LibraryThis is document #101, Essential C, in the StanfordCS Education Library .

2 This and other educational materials are available for free This article is free to be used, reproduced, excerpted,retransmitted, or sold so long as this notice is clearly reproduced at its of 2 Where C came from, what is it like, what other resources might you look 1 Basic Types and 3 Integer types, floating point types, assignment operator, comparison operators,arithmetic operators, truncation, 2 Control 11If statement, conditional operator, switch, while, for, do-while, break, 3 Complex Data 15 Structs, arrays, pointers, ampersand operator (&), NULL, C strings, 24 Functions, void, value and reference parameters, 5 Odds and 29 Main(), the .h/.c file convention, pre-processor, 6 Advanced Arrays and 33 How arrays and pointers interact.

3 The [ ] and + operators with pointers, baseaddress/offset arithmetic, heap memory management, heap 7 Operators and Standard Library 41A summary reference of the most common operators and Library C LanguageC is a professional programmer's language. It was designed to get in one's way as little aspossible. Kernighan and Ritchie wrote the original language definition in their book, TheC Programming Language (below), as part of their research at AT&T. Unix and C++emerged from the same labs. For several years I used AT&T as my long distance carrierin appreciation of all that CS research, but hearing "thank you for using AT&T" for themillionth time has used up that good languages are forgiving.

4 The programmer needs only a basic sense of how thingswork. Errors in the code are flagged by the compile-time or run-time system, and theprogrammer can muddle through and eventually fix things up to work correctly. The Clanguage is not like C programming model is that the programmer knows exactly what they want to doand how to use the language constructs to achieve that goal. The language lets the expertprogrammer express what they want in the minimum time by staying out of their is "simple" in that the number of components in the language is small-- If two languagefeatures accomplish more-or-less the same thing, C will include only one. C's syntax isterse and the language does not restrict what is "allowed" -- the programmer can prettymuch do whatever they 's type system and error checks exist only at compile-time.

5 The compiled code runs in astripped down run-time model with no safety checks for bad type casts, bad array indices,or bad pointers. There is no garbage collector to manage memory. Instead theprogrammer mangages heap memory manually. All this makes C fast but -- Where C FitsBecause of the above features, C is hard for beginners. A feature can work fine in onecontext, but crash in another. The programmer needs to understand how the features workand use them correctly. On the other hand, the number of features is pretty most programmers, I have had some moments of real loathing for the C language. Itcan be irritatingly obedient -- you type something incorrectly, and it has a way ofcompiling fine and just doing something you don't expect at run-time.

6 However, as I havebecome a more experienced C programmer, I have grown to appreciate C's straight-to-thepoint style. I have learned not to fall into its little traps, and I appreciate its the best advice is just to be careful. Don't type things in you don't takes too much time. Have a mental picture (or a real drawing) of how your Ccode is using memory. That's good advice in any language, but in C it's and Java are more "portable" than C (you can run them on different computerswithout a recompile). Java and C++ are more structured than C. Structure is useful forlarge projects. C works best for small projects where performance is important and theprogammers have the time and skill to make it work in C.

7 In any case, C is a very popularand influential language. This is mainly because of C's clean (if minimal) style, it's lackof annoying or regrettable constructs, and the relative ease of writing a C Resources The C Programming Language, 2nd ed., by Kernighan and Ritchie. The thin bookwhich for years was the bible for all C programmers. Written by the originaldesigners of the language. The explanations are pretty short, so this book is better as areference than for beginners. and Memory -- Much more detailabout local memory, pointers, reference parameters, and heap memory than in thisarticle, and memory is really the hardest part of C and C++. List basics -- Once you understand thebasics of pointers and C, these problems are a good way to get more 1 Basic Types and OperatorsC provides a standard, minimal set of basic data types.

8 Sometimes these are called"primitive" types. More complex data structures can be built up from these basic TypesThe "integral" types in C form a family of integer types. They all behave like integers andcan be mixed together and used in similar ways. The differences are due to the differentnumber of bits ("widths") used to implement each type -- the wider types can store agreater ranges of character -- at least 8 bits. Pronounced "car". As a practical matterchar is basically always a byte which is 8 bits which is enough to store a singleASCII character. 8 bits provides a signed range of or an unsigned range char is also required to be the "smallest addressable unit" for the machine --each byte in memory has its own integer -- at least 16 bits which provides a signed range Typical size is 16 bits.

9 Not used so integer -- at least 16 bits, with 32 bits being typical. Defined to bethe "most comfortable" size for the computer. If you do not really care about therange for an integer variable, declare it int since that is likely to be an appropriatesize (16 or 32 bit) which works well for that integer -- at least 32 bits. Typical size is 32 bits which gives a signedrange of about -2 billion ..+2 billion. Some compilers support "long long" for 64 integer types can be preceded by the qualifier unsigned which disallowsrepresenting negative numbers, but doubles the largest positive number representable. Forexample, a 16 bit implementation of short can store numbers in the , while unsigned short can store You can think of pointersas being a form of unsigned long on a machine with 4 byte pointers.

10 In my opinion,it's best to avoid using unsigned unless you really need to. It tends to cause moremisunderstandings and problems than it is : Portability ProblemsInstead of defining the exact sizes of the integer types, C defines lower bounds. Thismakes it easier to implement C compilers on a wide range of hardware. Unfortunately itoccasionally leads to bugs where a program runs differently on a 16-bit-int machine thanit runs on a 32-bit-int machine. In particular, if you are designing a function that will beimplemented on several different machines, it is a good idea to use typedefs to set uptypes like Int32 for 32 bit int and Int16 for 16 bit int. That way you can prototype afunction Foo(Int32) and be confident that the typedefs for each machine will be set sothat the function really takes exactly a 32 bit int.


Related search queries