Professional Documents
Culture Documents
Chapter 20 - C Legacy Code Topics: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 20 - C Legacy Code Topics: 2003 Prentice Hall, Inc. All Rights Reserved
20.1 Introduction
• Usage
– Access arguments with macro va_arg
• First argument is va_list
• Second is the expected type of variable
• Returns the value
• myArg = va_arg( myList, double );
– Can use different data types for different arguments
– Run macro va_end
• va_end( myList );
• Upcoming example
– Program to copy input file to output file
– copy input output
– Read a character from file input and write to file output
• Stop when no more characters to read (EOF)
• Function exit
– Forces program to end
– Usually takes EXIT_SUCCESS or EXIT_FAILURE
• Symbolic constants (#define)
• exit(EXIT_SUCCESS);
– Returns value to environment, indicating success or failure
• Exact value varies with system
• Function atexit
– Takes pointer to function (function name)
• atexit( myFunction )
• Functions must take void, return void
– Registers function to run when program ends successfully
• When exit called, or when main terminates
• atexit does not terminate the program
– Can register up to 32 functions
• Use multiple atexit calls
• Called in reverse order of registration
• volatile qualifier
– Indicates variable may be altered outside of program
– Variable not under control of program
• Compiler cannot perform certain optimizations
• Signal
– Unexpected event, can terminate program
• Interrupts (ctrl-c)
• Illegal instructions
• Floating-point exceptions (division by zero)
• Function signal traps unexpected signals
– <csignal>
– Takes signal number (symbolic constants defined)
– Takes pointer to function (function name)
• Signal handler passed signal number
– May be required to call signal again inside handler
• Depends on system
• Reinitialize handler after it handles signal
2003 Prentice Hall, Inc. All rights reserved.
28
Signal Explanation
• Function raise
– Takes signal number
– Creates signal
1 2 3 4
Interrupt signal (2) received.
Do you wish to continue (1 = yes or 2 = no)? 2
• Unstructured programming
– Use when performance crucial
• Using break to exit loop
– goto statement
• goto label;
• Program jumps to first statement after label
• Label is an identifier and colon (start:)
– Quick escape from deeply nested loop
• goto start;
20.12 Unions
• Union
– Memory that contains a variety of objects
• Data members share space
• Only contains one data member at a time
– Conserves storage
– Only the last data member defined can be accessed
– Declaration same as class or struct
union Number {
int x;
float y;
} ;
Union myObject;
20.12 Unions
• Union operations
– Assignment to union of same type: =
– Taking address: &
– Accessing union members: .
– Accessing members using pointers: ->
20.12 Unions
• Anonymous unions
– No type name
– Does not create a type; creates an unnamed object
• Contains only public data members
– Data members accessed like normal variables
• Use name, no . or -> required
– If declared globally, must be static
– Example
union {
int integer1;
double double1;
char *charPtr;
integer1 = 3;
1 2
3.3 4.4
Anonymous union