Download
weekly c minar n.
Skip this Video
Loading SlideShow in 5 Seconds..
Weekly C-minar PowerPoint Presentation
Download Presentation
Weekly C-minar

Weekly C-minar

199 Vues Download Presentation
Télécharger la présentation

Weekly C-minar

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Weekly C-minar Week 0

  2. Today: • Steps of the compile • Basic inclusion/syntax rules • Low-cost containment • Debugging

  3. What do you want to do today? • #include • #define • typedef • struct • union • enum

  4. Compiler

  5. Basic build steps • Preprocess (highly program dependent) • Include all of the header files • Expand all macros • Generate code (templates) • Produce compiler input • Compile (slooooooow) • Verify validity of syntax • Check static semantics (function signatures, etc.) • Produce linker input (.obj files in windows) • Link (very fast) • Make machine code from the .obj files

  6. Preprocessing Directives

  7. #include • tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. • #include"path-spec" • #include<path-spec>

  8. #include"path-spec" • instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of whatever files that include (#include) that file. • The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

  9. #include<path-spec> • instructs the preprocessor to search for include files first along the path specified by the /I compiler option, • then along the path specified by the INCLUDE environment variable

  10. #include (hacks) #ifndef HELLO_WORLD_H #define HELLO_WORLD_H /* relevant headers */ #endif Why is this important?

  11. #include • Compilers don’t like multiply defined symbols or classes • Encasing your headers in this fashion will prevent headers from being included multiple times /*(one.h) */ #include <zero.h> ... /*(two.h) */ #include <one.h> ... /*(three.h) */ #include <zero.h> ... /*(inf.h) */ #include <one.h> #include <two.h> #include <three.h> /* results in multiple incls */

  12. #define identifier … • give a meaningful name to a constant in your program • Replaced/removed during preprocessing • #define identifiertoken-stringopt • Replace identifier with token-string everywhereidentifier is a token • #define identifier • Remove identifier from the file

  13. #define • Can also be used as a functional replacement • Replaces the keywords in the macro with the values substituted at usage • #define identifier[(identifieropt,...,identifieropt)] token-stringopt • Example #define LESSTHAN(LEFT, RIGHT) ( LEFT < RIGHT ) //usage if( LESSTHAN( j, k ) ) { ... }

  14. C Container and Type Keywords

  15. typedeftype-declaration synonym; • defines a synonym for the specified type-declaration • cannot use the typedef specifier inside a function definition • typedef declarations do not introduce new types — they introduce new names for existing types

  16. typedef // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"

  17. typedef • More examples typedef unsigned char BYTE; // 8-bit unsigned entity. typedef BYTE * PBYTE; // Pointer to BYTE. BYTE Ch; // Declare a variable of type BYTE. PBYTE pbCh; // Declare a pointer to a BYTE // variable.

  18. Containers • Why have ‘em? • Makes for easier definition of “objects” • Define return types for functions with multiple values • What needs are there? • one type, lots of objects contained (struct) • one type, lots of types of values it can have (union) • one type, useful for logical definition (enum) • others, of course...

  19. struct [tag] {member-list} [declarators]; • The struct keyword defines a structure type and/or a variable of a structure type • A structure type is a user-defined composite type. • It is composed of "fields" or "members" that can have different types.

  20. In C you must explicitly use the struct keyword to declare a structure. In C++ a structure is the same as a class except that its members are public by default struct keyword unnecessary once the type has been defined. struct

  21. struct • You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon.

  22. struct Example 1 struct PERSON // Declare PERSON struct type { int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7;

  23. struct Example 2 (initialization) struct POINT // Declare POINT structure { int x; // Define members x and y int y; } spot = { 20, 40 }; // Variable spot has // values x = 20, y = 40 struct POINT there; // Variable there has POINT type struct CELL // Declare CELL bit field { unsigned character : 8; // 00000000 ???????? unsigned foreground : 3; // 00000??? 00000000 unsigned intensity : 1; // 0000?000 00000000 unsigned background : 3; // 0???0000 00000000 unsigned blink : 1; // ?0000000 00000000 } screen[25][80]; // Array of bit fields

  24. union [tag] {member-list} [declarators]; • declares a union type and/or a variable of a union type • user-defined data type that can hold values of different types at different times • all of its members start at the same location in memory • can contain only one of its members at a time • is (basically) the size of the largest member

  25. union union UNKNOWN // Declare union type { char ch; int i; long l; float f; double d; } var1; // Optional declaration of union variable

  26. union • In C • you must explicitly use the union keyword to declare a union. • In C++ • union keyword unnecessary once the type has been defined. Example 1 union UNKNOWN var2; // C declaration of a union variable UNKNOWN var3; // C++ declaration of a union variable Example 2 A variable of a union type can hold one value of any type declared in the union. Use the member-selection operator (.) to access a member of a union: var1.i = 6; // Use variable as integer var2.d = 5.327; // Use variable as double

  27. enum [tag] {enum-list} [declarator]; • a user-defined type consisting of a set of named constants called enumerators • the first enumerator has a value of 0, • each successive enumerator is one larger than the value of the previous one, • unless you explicitly specify a value for a particular enumerator

  28. enum • Enumerators needn’t have unique values • name of each enumerator must be unique within the scope where the enum is defined. • An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined

  29. enum • In C • you must explicitly use the enum keyword to declare an enum. • In C++ • you can use just the tag. // Example of the enum keyword enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday } today; // Variable today has type Days int tuesday; // Error, redefinition of tuesday enum Days yesterday; // Legal in C and C++ Days tomorrow; // Legal in C++ only yesterday = monday; int i = tuesday; // Legal; i = 2 yesterday = 0; // Error; no conversion yesterday = (Days)0; // Legal, but results undefined

  30. Christopher, take over

  31. Homework: See the webpage