1 / 19

Chapter 4 Managing Input and Output Operations

Chapter 4 Managing Input and Output Operations. PROGRAMMING IN C. Chapter 4. C hasn’t any built-in input/output statements as part of its syntax. All of input/output operations are carried out through standard input/output functions. e.g. printf( )

laszlo
Download Presentation

Chapter 4 Managing Input and Output Operations

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. Chapter 4Managing Input and Output Operations PROGRAMMING IN C

  2. Chapter 4 • C hasn’t any built-in input/output statements as part of its syntax. • All of input/output operations are carried out through standard input/output functions. e.g. printf( ) • scanf( ) getchar( ) gets( )printf( ) putchar( ) puts( ) # include <stdio.h># include "stdio.h" standard input and output

  3. getchar( ) & putchar( ) • Form: variable = getchar(); putchar(character); #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); } #include <stdio.h> main() { char c ; c = getchar ( ); putchar ( c ); putchar ( getchar () ); printf ( "%c", getchar () ); putchar ('D'); printf("%d", getchar()); } abc a b c D 10

  4. printf( ) – Formatted Output • Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am); • Control string • The characters that will be outputted as they appear. e.g. price amount = $ , . • Escape sequence characters. e.g. \n • Format specifications. e.g. %f, %d

  5. printf( ) – Formatted Output • Form: printf("control string", arg1, …, argn); e.g. printf("price=$%.2f\n, amount=%d.", pr*am, am); • The outputted expression list: (arg1, …, argn) • There can be no any expression or more than one expression. They are separated by comma. • In spite of what type these expressions are, they are always outputted in specified format. printf("%c, %d", 97, 'a'); Output: a, 97 printf("%d, %u", 32767+1, 32767+1); Output: -32768, 32768

  6. printf( ) – Formatted Output printf ("%f",123.45); printf ("%c, %c",65, 'A'); printf ("%u, %U",-3, 'A'); printf ("%e",12.3); printf ("%s","Hello!"); printf ("%x, %X",-3, 'A'); printf ("%d, %i",-3, 'A'); printf ("%g",123.450); printf ("%%"); printf ("%o, %O",-3, 'A'); • Format specifications Output: 123.450000 Output: 123.45 Output: 1.230000e+01 Output: A, A Output: Hello! Output: % Output: 65533, %U Output: 177775, %O Output: fffd, 41 Output: -3, 65

  7. printf( ) – Formatted Output • Accessorial format specifications printf ("%3d", 12); printf ("%3f", 12.3); printf ("%.1f", 12.36); printf ("%.1f, %.1f", 1.25, 1.251); printf ("%4.2s", "abc"); printf ("%ld, %d", 65536, 65536); printf ("%2c, %-2c", 'A', 'A'); Output: 12 Output: 12.300000 Output: 12.4 Output: 1.2, 1.3 Output:  ab Output: 65536, 0 Output: A, A

  8. scanf( ) – Formatted Input • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • format specifications %d, %i, %o, %x, %u, %c, %s, %f, %e, %gthe same as those in printf function.

  9. scanf( ) – Formatted Input e.g. scanf("%d%*d%d", &a, &b); input:1234567 result:12a, 67b e.g. scanf("%3d%*2d%f", &i, &f); input:123456.789 result: 123i, 6.789f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Accessorial format specifications e.g. scanf("%3d%f", &i, &f); input:12345.6789 result: 123i, 45.6789f

  10. scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input:1234567.89 result:12n1, 345n2, 67.89 f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Separators • When it read in the integers or real numbers, it defaults blank spaces, tabs or newlines as separators.

  11. scanf( ) – Formatted Input e.g. scanf("%d, %d", &a, &b); input:12, 345 result:12a, 345b input: 12345 result:12a, nothingb e.g. scanf("a=%d, b=%d", &a, &b); input:a=12, b=345 result:12a, 345b input: 12,345 result: nothinga, nothingb • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Separators • It also can specify separators, that is those characters except format specifications.In this way, the input must accord with the specified format, otherwise the input most probably is read in by errors.

  12. scanf( ) – Formatted Input • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • Separators • When it read in a character, the blank space, tab and newline character are no longer separators, but as a valid character. e.g. scanf("%d%c", &i, &ch); input:12a result:12i, '' ch

  13. scanf( ) – Formatted Input e.g. scanf("%d%d%f", &n1, &n2, &f); input:1234567.89 result:12n1, 345n2, 67.89 f • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • How to judge a data is finished? • If “scanf” read an integer of real number, when it encounters a blank space, or tab, or newline character, it considers the number is finished.

  14. scanf( ) – Formatted Input e.g. scanf("%3d%f", &i, &f); input:12345.6789 result: 123i, 45.6789f e.g. scanf("%d%c%f", &a, &b, &c); input:123a456o.78 result: 123a, 'a'b, 456.0c • Form: scanf ("control string", arg1, …, argn);e.g. scanf("%d,%c", &num, &ch); • How to judge a data is finished? • When the input data achieves the specified field wide, “scanf” considers the data is finished. • When “scanf” encounters an illegal character, it considers the data is finished.

  15. Program 1 • Read in 2 numbers, exchange them, then output them. • Step1: Declare 3 float variables – x, y and temp. • Step2: Read 2 real numbers into x and y. • Step3: Exchange them: x -> temp (temp = x;) y -> x (x = y;) temp -> y (y = temp;) • Step4: Output the value of x and y.

  16. Program 1 Please input x and y: 4 5.6  Before exchange: x=4.00, y=5.60 After exchange: x=5.60, y=4.00 main() { float x, y, temp; printf ("Please input x and y:\n") ; scanf ( "%f%f", &x, &y ); printf ("Before exchange: x=%.2f, y=%.2f\n", x, y); temp = x ; x = y; y = temp; printf ("After exchange: x=%.2f, y=%.2f", x, y); }

  17. Program 2 • Read in a lowercase letter, convert it into its uppercase equivalent, and then output the uppercase and its ASCII value. • Step1: Declare a char type variable ch. • Step2: Read in a lowercase letter into ch. • Step3: Convert it into its uppercase equivalent. ch = ch – 32; or ch = ch – ('a' - 'A'); • Step4: Output ch and its ASCII value.

  18. Program 2 Please input a lowercase: The lowercase is m, and its ASCII value is 109 The uppercase equivalent is M, and its ASCII value is 77 m  #include <stdio.h> main() { char ch; printf("Please input a lowercase:") ; ch = getchar(); printf("The lowercase is %c, and its ASCII value is %d\n", ch, ch); ch = ch - 32; printf("The uppercase equivalent is:%c, and its ASCII value is %d\n", ch, ch); }

  19. Homework • Review Questions • Programming Exercises

More Related