260 likes | 417 Views
B Smith: Consider skipping this lecture. Boring, mostly unnecessary stuff. Make this assigned reading. Math 130 Introduction to Computing Functions–(III) Lecture # 11 or 10?? 9/22/04. Overview. scanf() challenges More Library Functions. scanf() revisited. scanf()
E N D
B Smith: Consider skipping this lecture. Boring, mostly unnecessary stuff. Make this assigned reading Math 130Introduction to ComputingFunctions–(III)Lecture # 11 or 10??9/22/04
Overview • scanf() challenges • More Library Functions B. Smith
scanf() revisited • scanf() • This function might exhibit “unexpected” behavior for character input • scanf()ends when either • it exhausts its control string, or • when the input does not meet the control spec. • scanf("%c") expects a single character B. Smith
scanf() • scanf() • returns the number of successfully matched and assigned input items • The next call to scanf()resumes searching immediately after the last character entered B. Smith
scanf() B. Smith
scanf() – unexpected behavior B. Smith
Fix 1 for scanf() int main() { /* eg2a.c */ char fkey, skey; int a; printf("Type in a character: "); a = scanf("%2c", &fkey); printf("\n scanf returned %d",a); printf("\nThe key just accepted is %d", fkey); printf("\nType in another character: "); scanf("%2c", &skey); printf("The key just accepted is %d", skey); getchar(); } B. Smith
Output B. Smith
Skip any “white spaces” and grab the next character Fix 2 for scanf() “white space” an ASCII character that does not print (tab, newline, space, etc) int main() { /* eg2b.c */ char fkey, skey; int a; printf("Type in a character: "); a = scanf(“ %c”, &fkey); printf("\n scanf returned %d",a); printf("\nThe key just accepted is %d", fkey); printf("\nType in another character: "); scanf(" %c", &skey); printf("The key just accepted is %d", skey); getchar(); } B. Smith
scanf() • Summary: • Exercise care when using scanf() to read in characters • You must explicitly deal with white space characters (‘\n’, ‘\t’,‘ ‘). • Using library string functions can help you get around many scanf() input issues • e.g., gets() will read in characters and store them as a string B. Smith
Casts Websters:Cast - to form (molten metal, plastic, etc.) into a particular shape by pouring or pressing into a mold. • Operations involving an integer and a float would “cast” into a result of type floating point. • This is an implicit data-type conversion • C allows you to explicitly specify your own data-type conversions • You can force a value of one data-type to be another type • The syntax: (data-type) expression e.g., ( int ) ( 1.0*2 + 1.8 ) The data type to which you’re casting = (int) 3.8 = 3 B. Smith
cast example int main() { float num=1234.12345678; printf("\nAs a float, the answer is %f", num); printf("\nAs a non cast integer, the answer is %d", num); printf("\nAs a casted integer, the answer is %d\n",(int) num); } B. Smith
String Library Functions • String library functions require the header file string.h • What you can do • Concatenate strings: • strcat( string1, string2 ) • Determine the position of a character within string: • strchr( string, character ) • Compare strings: • strcmp( string1, string2 ) • Set string1 equal to string2: • strcpy( string1, string ) • Determine the length of a string: • strlen( string ) • We'll discuss these in more detail soon B. Smith
Other Library Functions • The following require the header file type.h • isalpha() • determine if you have a character • isupper() • determine if a character is uppercase • isdigit() • detemine if a character is a digit 0 thru 9 • toupper(), tolower() • convert to upper/lowercase B. Smith
Library Functions- Ex B. Smith
Header Files • Header files are used to modularize a program • Recall the typical “elements” of a program… B. Smith
Example: isNegative.c #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } B. Smith
Example: isNegative.c Function Prototype #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } B. Smith
Example: isNegative.c #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Function Definition B. Smith
Example: isNegative.c #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Function Call (Must be after prototype, but can be before definition) B. Smith
Example: isNegative.c #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } Header files (filename.h) contain function prototypes and global variable declarations B. Smith
Example: isNegative.c #include <stdio.h> int isNegative (int); int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } stdio.h contains function prototypes for printf(), scanf(), and other I/O functions B. Smith
Header files • You can make your own header files with prototypes of frequently used functions: #include "myFunctions.h" • Put the functions in a corresponding C file, and include those too: #include "myFunctions.c" B. Smith
Example: isNegative.c #include <stdio.h> #include "myFunctions.h" #include "myFunctions.c" int main (void) { int number; printf ("Enter an integer: "); scanf ("%d",&number); if (isNegative(number)) { printf("Negative\n"); } else { printf("Positive\n"); } return 0; } • Note: • " "around file name for user-defined files • < > for standard system files isNegative() is declared in myFunctions.h and defined in myFunctions.c,not in this file! B. Smith
Example: myFunctions.h Example: myFunctions.c int isNegative ( int n ) { int result; if ( n<0 ) { result=1; } else { result = 0; } return result; } int isNegative ( int ); B. Smith
Next Lecture: Ch 6 Passing Addresses Random numbers Ch 7 Arrays Lab Work Lab04 due 9/24 Ex 6.1, p 226 pbms 4 and 5 Lab05 due 9/29 Ex 6.1, p 226 pbms 12 and 15 Project 3: we will discuss next class read handout on Bb due Oct 4, 11:55pm Preparation B. Smith