Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 SequenceMeaning ---------------------------------------- \aBeep \bBackspace \nNewline \tHorizontal tab \\Backslash

5 The printf conversion specifiers Specifier MeaningTypes -------------------------------------------------- %c Single Characterchar %d Signed integerint, short, long %f Floating point numberfloat, double %.xf Truncated floatfloat, double %s Character stringchar arrays %u Unsigned numberunsigned 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 –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

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


Download ppt "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."

Similar presentations


Ads by Google