weekly c-minar
DESCRIPTION
Weekly C-minar. Week 0. Today:. Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging. What do you want to do today?. #include #define typedef struct union enum. Compiler. Basic build steps. Preprocess (highly program dependent) - PowerPoint PPT PresentationTRANSCRIPT
Weekly C-minar
Week 0
Today:
• Steps of the compile
• Basic inclusion/syntax rules
• Low-cost containment
• Debugging
What do you want to do today?
• #include
• #define
• typedef
• struct
• union
• enum
Compiler
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
Preprocessing Directives
#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>
#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.
#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
#include (hacks)
#ifndef HELLO_WORLD_H#define HELLO_WORLD_H
/* relevant headers */
#endif
Why is this important?
#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*/
#define identifier …
• give a meaningful name to a constant in your program
• Replaced/removed during preprocessing
• #define identifier token-stringopt
– Replace identifier with token-string everywhere identifier is a token
• #define identifier– Remove identifier from the file
#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 )//usageif( LESSTHAN( j, k ) ) { ... }
C Container and Type Keywords
typedef type-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
typedef// Example of the typedef keywordtypedef 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])();"
typedef
• More examplestypedef 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.
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...
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.
struct
• 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
• 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.
structExample 1struct 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;
structExample 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 = 40struct POINT there; // Variable there has POINT typestruct 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
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
union
union UNKNOWN // Declare union type{ char ch; int i; long l; float f; double d;} var1; // Optional declaration of union
variable
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 variableUNKNOWN 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 integervar2.d = 5.327; // Use variable as double
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
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
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 keywordenum 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 = 2yesterday = 0; // Error; no conversionyesterday = (Days)0; // Legal, but results undefined
Christopher, take over
Homework:
See the webpage