1 / 26

Math 130 Introduction to Computing Functions–(III) Lecture # 11 or 10?? 9/22/04

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()

studs
Download Presentation

Math 130 Introduction to Computing Functions–(III) Lecture # 11 or 10?? 9/22/04

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Overview • scanf() challenges • More Library Functions B. Smith

  3. 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

  4. 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

  5. scanf() B. Smith

  6. scanf() – unexpected behavior B. Smith

  7. 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

  8. Output B. Smith

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Library Functions- Ex B. Smith

  16. Header Files • Header files are used to modularize a program • Recall the typical “elements” of a program… B. Smith

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

More Related