130 likes | 240 Views
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).
E N D
CSCI 130 Basic Input and Output Chapter 9
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 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
Common Escape Sequences • Sequence Meaning • ---------------------------------------- • \a Beep • \b Backspace • \n Newline • \t Horizontal tab • \\ Backslash
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
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
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
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
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
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);
Example of scanf ( ) • Sample uses of scanf( ) • int x; • float y; • scanf(“%d”, &x); • scanf(“%d%f”, &x, &y);
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>
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