77 Input and Output l Variety of IO functions in C Standard Library l Must

77 input and output l variety of io functions in c

This preview shows page 77 - 83 out of 83 pages.

77 Input and Output l Variety of I/O functions in C Standard Library . l Must include <stdio.h> to use them. l printf("%d\n", counter); l String contains characters to print and formatting directions for variables. l This call says to print the variable counter as a decimal integer, followed by a linefeed ( \n ). l scanf("%d", &startPoint); l String contains formatting directions for looking at input. l This call says to read a decimal integer and assign it to the variable startPoint . (Don't worry about the & yet.)
Image of page 77
78 Output Examples l This code: l printf("%d is a prime number.\n", 43); l printf("43 plus 59 in decimal is %d.\n", 43+59); l printf("43 plus 59 in hex is %x.\n", 43+59); l printf("43 plus 59 as a character is %c.\n", 43+59); l produces this output: l 43 is a prime number. l 43 + 59 in decimal is 102. l 43 + 59 in hex is 66. l 43 + 59 as a character is f.
Image of page 78
79 More About Output l Can print arbitrary expressions, not just variables. l printf("%d\n", startPoint - counter); l Print multiple expressions with a single statement. l printf("%d %d\n", counter, startPoint - counter); l Different formatting options: l %d decimal integer l %x hexadecimal integer l %c ASCII character l %f floating-point number
Image of page 79
80 Output Examples (continued) l Formatting instructions can contain additional information: l Min Field Width . Precision l Min Field Width The minimum number of spaces the number is allow to occupy. l Precision Float: Num of digits to the right of decimal point l Int: Min Number of digits to be printed l String: Number of chars from string to print l int iv = 12345; l printf("%2.3d\n", (iv)); l printf("%10d\n", (iv)); l printf("%10.5f\n", (3.1456123)); l printf("%10.2f\n", (3.1456123)); l printf("%.2f\n", (3.1456123)); 12345 12345 3.14561 3.15 3.15
Image of page 80
81 Examples of Input l Many of the same formatting characters are available for user input. l scanf("%c", &nextChar); l reads a single character and stores it in nextChar l scanf("%f", &radius); l reads a floating point number and stores it in radius l scanf("%d %d", &length, &width); l reads two decimal integers (separated by whitespace), stores the first one in length and the second in width l Must use ampersand ( & ) for variables being modified. (Explained later when we talk about pointers) l Exactly how this matching is done will be covered later .
Image of page 81
82 Comments l Begins with /* and ends with */ l Can span multiple lines. l Cannot have a comment within a comment. l Comments are not recognized within a string. l example: "my/*don't print this*/string" would be printed as: my/*don't print this*/string l As before, use comments to help reader, not to confuse or to restate the obvious
Image of page 82
83 main Function l Every C program must have a function called main() . l This is the code that is executed when the program is run. l The code for the function lives within brackets: l main() l { l /* code goes here */ l }
Image of page 83

You've reached the end of your free preview.

Want to read all 83 pages?

  • Fall '08
  • Staff
  • Clock rate, Central processing unit, l༆ , Badri Nath

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes