CH 14 Flashcards
Writing and using Functions
What headers?
stdio. h
ctype. h
2 #include ctype.h
declares ,functions ,testing ,mapping ,characters.
1 #include studio.h
defines three variable types,
macros,functions for input and output
Functions for Later
// forward declaration 5 int can_print_it( char ch); 6 void print_letters( char arg[]);
Define can_print_it, which simply returns the truth value (0 or 1) of
isalpha(ch) || isblank(ch) back to its caller, print_letters.
32 int can_print_it( char ch)
33 {
34 return isalpha(ch) || isblank(ch);
35 }
Finally, make the whole chain of functions go.
37 int main( int argc, char * argv[]) 38 { 39 print_arguments(argc, argv); 40 return 0 ; 41 }
void?
A pointer to void can be converted to any pointer type
Every program is starting with what definition?
main(int argc, char *argv[])
minimum possible value of argc
is 1 which is the execution command itself
argv[0] holds
argv[1] holds
argv[0] the string ./mymainprogram
argv[1] is next argument
For loop covering print_arguments
Define the print_arguments function, supply it with the command line info
Start the counter on argc
Redefine the print_letters function
8 void print_arguments( int argc, char * argv[]) 9 { 10 int i = 0 ; 11 12 for (i = 0 ; i < argc; i ++ ) { 13 print_letters(argv[i]); 14 } 15 }
For loop covering print_letters
Define the print_arguments function, supply it with a array variable
Start a counter for that array variable
spit the array variable into a char variable
pass the variable through the can_print_it function
then print the result
17 void print_letters( char arg[]) 18 { 19 int i = 0 ; 20 21 for (i = 0 ; arg[i] != '\0' ; i ++ ) { 22 char ch = arg[i]; 23 24 if (can_print_it(ch)) { 25 printf( "'%c' == %d " , ch, ch); 26 } 27 } 28 29 printf( "\n" ); 30 }
forward declarations means
TYPE VAR_NAME( TYPE OTHER_VAR);
Are these two Variables
5 int can_print_it
6 void print_letters
or functions called from the header?
Variables
isalpha(int c)
Explanation: Even though it accepts an int, it really deals with characters. Based on the ASCII value of the character it will determine if it is an alphabetical character or not.