1 / 13

CSCI 130

CSCI 130. Basic Input and Output Chapter 9. The printf ( ) function. Printf(“<br>The value of x is %d”, x); Displayed to screen (assume x = 12): The value of x is 12 Two variables are passed to the printf function the information inclosed in quotes the name of the variable (x).

davis
Download Presentation

CSCI 130

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. CSCI 130 Basic Input and Output Chapter 9

  2. The printf ( ) function • Printf(“\nThe value of x is %d”, x); • Displayed to screen (assume x = 12): • The value of x is 12 • Two variables are passed to the printf function • the information inclosed in quotes • the name of the variable (x)

  3. 3 components of the format string • printf(“\nThe value of x is %d”, x); • Literal text - displayed exactly as it appears • The value of x is • Escape sequence - \x (x is a specific char) • \n • Conversion specifier - %x (x is a specific char) • tells printf how to interpret the variables • %d

  4. Common Escape Sequences • Sequence Meaning • ---------------------------------------- • \a Beep • \b Backspace • \n Newline • \t Horizontal tab • \\ Backslash

  5. The printf conversion specifiers Specifier Meaning Types -------------------------------------------------- %c Single Character char %d Signed integer int, short, long %f Floating point number float, double %.xf Truncated float float, double %s Character string char arrays %u Unsigned number unsigned int, unsigned short unsigned long

  6. Example Conversion Specifiers int x = 4, z = 5; float y = 3.16677; printf(“x = %d , y = %f \n”, x, y); printf(“y truncated to 2 places: %.2f \n”, y); printf(“x + z = %d”, (x + z)); Output: x = 4 , y = 3.16677 y truncated to 2 places: 3.16 x + z = 9

  7. Using the printf statement • Remember to include stdio.h file • #include <stdio.h> • Code Warrior automatically includes stdio.h in the main file • Put one statement per printf • don’t overuse \n • remember to put \n at end if necessary

  8. The puts( ) function • Used to display text to the screen • Can use escape sequences • New line character automatically inserted at end of puts statement • Must include stdio.h file • Cannot use conversion specifiers • cannot display numeric variables

  9. Using the puts ( ) function • puts( ) is similar to printf( ) • puts(“Hello World”); is equivalent to • printf(“Hello World”); • puts ( ) enters a carriage control at end • puts(“Hello”); • puts(“World”); • Output: • Hello • World

  10. The scanf ( ) function • Allows user to enter numeric input • Assigns data to one or more variables • Uses same conversion specifiers as printf() • Must include stdio.h file • & is address-of operator (covered in CIS 131) • scanf(“%d”, &x);

  11. Example of scanf ( ) • Sample uses of scanf( ) • int x; • float y; • scanf(“%d”, &x); • scanf(“%d%f”, &x, &y);

  12. Data input • scanf( ) users whitespace to separate input • Example: • All the following response to scanf(“%d%f”, &x, &y) will work as expected • 10 3.1 • 10 3.1 • 10 <enter> 3.1 <enter>

  13. Using the scanf ( ) function • Do not forget to use the address-of operator (ampersand - &) • Use printf( ) or puts( ) in conjunction with scanf( ) • clarifies to user what input is required

More Related