Professional Documents
Culture Documents
Outline
14.1
Introduction
14.2
Redirecting Input/Output on UNIX and DOS Systems
14.3
Variable-Length Argument Lists
14.4
Using Command-Line Arguments
14.5
Notes on Compiling Multiple-Source-File Programs
14.6
Program Termination with exit and atexit
14.7
The volatile Type Qualifier
14.8
Suffixes for Integer and Floating-Point Constants
14.9
More on Files
14.10
Signal Handling
14.11
Dynamic Memory Allocation with calloc and
realloc
14.12
The Unconditional Branch: goto
14.1 Introduction
Several advanced topics in this chapter
Operating system specific
Usually UNIX or DOS
Redirect symbol(<)
Pipe command(|)
Output of one program becomes input of another
$ firstProgram | secondProgram
Load <stdarg.h>
Use ellipsis(...) at end of parameter list
Need at least one defined parameter
Example:
double myfunction ( int i, ... );
va_end( arguments )
Helps function have a normal return
Outline
3 #include <stdio.h>
4 #include <stdarg.h>
5
6 double average( int, ... );
7
8 int main()
9 {
10
1. Load <stdarg.h>
header
1.1 Function prototype
(variable length
argument list)
11
12
printf( "%s%.1f\n%s%.1f\n%s%.1f\n%s%.1f\n\n",
13
14
15
16
average( 2, w, x ),
17
18
average( 3, w, x, y ),
19
20
average( 4, w, x, y, z ) );
21
22
return 0;
23 }
24
25 double average( int i, ... )
3.2 Initialize ap
(va_start(ap, i))
26 {
27
double total = 0;
28
int j;
29
va_list ap;
30
31
32
va_start( ap, i );
33
34
35
36
va_end( ap );
37
return total / i;
Outline
3.3 Access arguments
va_arg(ap, double)
38 }
w
x
y
z
Program Output
=
=
=
=
37.5
22.5
1.7
10.2
int argc
Number of arguments passed
char *argv[]
Array of strings
Has names of arguments in order
argv[ 0 ] is first argument
argc: 3
argv[ 0 ]: "copy"
argv[ 1 ]: "input"
argv[ 2 ]: "output"
Outline
25
26
27 }
return 0;
Example:
If integer myGlobal is defined in one file
To use it in another file you must include the statement
extern int myGlobal;
extern
States that the variable is defined in another file
Function atexit
atexit( functionToRun );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outline
1. Register function
print using atexit
2. User input
3. Output
3.1 Function definition
if ( answer == 1 ) {
printf( "\nTerminating program with "
"function exit\n" );
exit( EXIT_SUCCESS );
}
printf( "\nTerminating program by reaching"
" the end of main\n" );
return 0;
}
void print( void )
{
printf( "Executing function print at program "
"termination\nProgram terminated\n" );
}
Outline
Program Output
unsigned integer u or U
long integer l or L
unsigned long integer ul or UL
float f or F
long double l or L
Examples:
174u
467L
3451ul
Function tmpfile
Opens a temporary file in mode "wb+"
Some systems may process temporary files as text files
Function rewind
Positions file pointers to the beginning of the file
Description
Open a binary file for reading.
Create a binary file for writing. If the file already exists,
discard the current contents.
Append; open or create a binary file for writing at end-of-file.
Open a binary file for update (reading and writing).
Create a binary file for update. If the file already exists,
discard the current contents.
Append; open or create a binary file for update; all writing is
done at the end of the file
Function signal
Traps unexpected events
Header <signal.h>
Receives two arguments a signal number and a pointer to the
signal handling function
Function raise
Takes an integer signal number and creates a signal
Explanation
Abnormal termination of the program (such as a call
to abort).
An erroneous arithmetic operation, such as a divide
by zero or an operation resulting in overflow.
Detection of an illegal instruction.
Receipt of an interactive attention signal.
An invalid access to storage.
A termination request sent to the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Outline
1. Function prototype
signal set to call function
signal_handler when a
signal of type SIGINT occurs.
2. Generate random
number
2.1 raise signal if
== 25
3. Function definition
33 {
34
Outline
int response;
35
36
printf( "%s%d%s\n%s",
37
38
39
40
41
42
43
44
45
3. Function definition
46
47
if ( response == 1 )
48
49
else
50
exit( EXIT_SUCCESS );
51 }
1
11
21
31
41
51
2
12
22
32
42
52
3
13
23
33
43
53
4
14
24
34
44
54
5
15
25
35
45
55
6
16
26
36
46
56
7
17
27
37
47
57
8
18
28
38
48
58
9 10
19 20
29 30
39 40
49 50
59 60
Program Output
61 62 63 64 65 66 67 68
71 72 73 74 75 76 77 78
81 82 83 84 85 86 87 88
91 92 93
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes
94 95 96
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes
97 98 99 100
69
79
89
70
80
90
Outline
Program Output
or 2 = no )? 1
or 2 = no )? 1
goto statement
Changes flow control to first statement after specified label
A label is an identifier followed by a colon (i.e. start:)
Quick escape from deeply nested loop
goto start;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
int count = 1;
Outline
1. Initialize variable
1.1 Labels
start:
if ( count > 10 )
goto end;
printf( "%d
++count;
goto start;
/* label */
2. Loop
", count );
end:
putchar( '\n' );
3. Print
/* label */
return 0;
}
3
10
Program Output