Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.

Similar presentations


Presentation on theme: "Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions."— Presentation transcript:

1 Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions

2 Standard Output #include ä ä printf(“format string”, arg1, arg2,.... argN); // to the screen ä ä fprintf(file_ptr, “format string”, arg1, arg2,.... argN); // to a file ä ä sprintf(str_ptr, “format string”, arg1, arg2,.... argN); // to a string ä ä each returns the number of characters printed. Format String ä ä All but two characters printed verbatim. ä ä % character used to format and output the next argument. ä ä \ character used to print characters that can’t be typed directly. ä ä Must be at least as many arguments as % specifiers.

3 % - printf() conversion specifiers Format: %[flags][width][.precision][modifier]type_character ä ä All except type_character are optional. ä ä Conversion specifier is everything from % sign to the first type_character. ä ä [flags] - controls justification, leading spaces, sign, etc. ä ä {-, +,, #, 0} ä ä [width] - sets the minimum width of the field - may be longer. ä ä [.precision] - sets the number of digits following the decimal point. ä ä Usually used for floating points, but also affects character and integer types as well. ä ä [modifier] - combines with type_character to determine argument type. ä ä type_character - the basic type of that argument. ä ä {c, d, e, E, f, g, G, i, o, p, s, u, x, X, %} In general, look up what you need. You will tend to remember the forms you use most often.

4 \ - printf() escape sequences Format: \(character) or \(number) ä ä If a character follows the \, the action indicated by the character is performed. ä ä \n - newline ä ä \r - return w/o line feed. ä ä \” - print double quote ä ä \’ - print single quote (aka apostrophe) ä ä \a - sound bell ä ä \b - backspace ä ä \\ - print backslash ä ä \? - question mark ä ä If a number follows the \, the ASCII character of the octal number is printed.

5 Standard Input #include ä ä scanf(“format string”, arg1, arg2,.... argN); // from the keyboard ä ä scanf(file_ptr, “format string”, arg1, arg2,.... argN); // from a file ä ä sscanf(str_ptr, “format string”, arg1, arg2,.... argN); // from a string ä ä each returns the number of successful conversions. Format String ä ä Literal characters in format string can be used to skip characters. ä ä % conversion specifiers similar (but not identical) to printf(). ä ä Arguments need to be memory locations where values will be stored. Big Time Caveat ä ä If input does not adequately match format, results can be VERY unpredictable. ä ä Many programmers avoid the use of scanf() at nearly any cost. ä ä Use of fscanf() for file I/O is generally safe provided the file format is adequately constrained.

6 Common mistakes with scanf() Passing values instead of memory locations. ä ä The scanf() function read values and store them at the memory locations you supply. k = 10; scanf(“%i”, k); ä ä Tells scanf() to format the value as an integer and store it at location 10. ä ä But the memory location for variable k is almost certainly not 10. ä ä The address operator, &, returns the memory location of the specified variable. scanf(“%i”, &k); ä ä Tells scanf() to format the value as an integer and store it at the memory address used for variable k. Using %lf for doubles. ä ä printf() uses %lf for both floats and doubles because the compiler promotes all arguments of type float to double. ä ä scanf() cannot due this. It must know which type the variable is so that the number and format of the bytes stored is correct.


Download ppt "Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions."

Similar presentations


Ads by Google