"C Fundamentals" Precept Handouts Dear COS 217 Student, During the first two weeks, the COS 217 course requires lots of reading of the King book to learn the fundamentals of C. Please see the course "Schedule" Web page for details. The reading isn't difficult, especially for a student who knows Java. But there is a lot of it. With that in mind, during this week's first precept we distributed several reference handouts under the heading "C Fundamentals." It is our hope that those handouts will help you to read the King book efficiently. There was no time during my precepts to discuss those handouts. So I thought that an overview of each handout might be helpful. Please see below. Bob Dondero C Primitive Data Types The "C Primitive Data Types" handout lists the 11 primitive data types that are available in C. For each it provides a description, its size, example variable declarations, and example literals (typical, largest, and smallest). The handout also describes the differences between C and Java with respect to data types: -- Java has types boolean and byte, but C does not. -- C has unsigned data types, but Java does not. Example: In C, assuming that an int comprises four-bytes, an int has range -2,147,483,678 to 2,147,483,677, and an unsigned int has range 0 to 4,294,967,295. -- In Java a char comprises 2 bytes. In C a char comprises 1 byte. -- In Java the sizes of all types are specified. In C a char is one byte, but the sizes of all other types are system-dependent. sizes.c Because the sizes of most data types are unspecified, C provides a "sizeof" operator. The expression sizeof(type) evaluates to the number of bytes in a datum of that type. The sizes.c program uses the sizeof operator to print the size of each data type on our system. For example, note that on our system type int comprises 4 bytes, and type double comprises 8 bytes. C Operators The "C Operators" handout lists the C operators, grouped by category. It also summarizes the differences between C and Java with respect to operators: -- Java has >>>, new, and instanceof operators, but C does not. -- C has several pointer-related operators, and the aforementioned "sizeof" operator. Java does not. -- In Java, relational (e.g. <) and logical (e.g. &&) operators evaluate to type boolean. In C, relational and logical operators evaluate to type int, not boolean. 0 means FALSE. 1 means TRUE. -- In Java you can use + or += to concatenate strings. In C you cannot. -- In Java demotions (i.e. casts from a large data type to a smaller one) are not automatic. In C demotions are performed automatically, with silent loss of the most significant bytes. That fact is irrelevant if you avoid mixed-type expressions. C Statements The "C Statements" handout lists the C statements, and describes how they differ from Java statements. The most important difference is a consequence of C's omission of a boolean data type. Because C lacks a boolean data type, a C conditional statement (if, while, do...while, for) is controlled by an int expression. If the int expression evaluates to 0, then the statement considers the expression to be logically FALSE. If the given expression evaluates to non-0, then the statement considers the expression to be logically TRUE. formattedio.c formattedio.c is a program that reads via scanf(), and writes via printf(), data of each primitive data type. Thus it shows the scanf() and printf() conversion specifications that are appropriate for each data type.