SCP 1103 Basic C Programming SEM 1 20102011
SCP 1103 Basic C Programming SEM 1 2010/2011 Week 6 Input & Output Operations
Formatted input / output • In C, data must be input to and output from a program through a file
Formatted input / output • Other then printf and scanf, C provides other function for input and output operation using library <stdio. h> & <conio. h> • Example: conio. h : clrscr stdio. h : scanf getch inport getchar feof
6. 1 Formatting Output
Formatting Output with printf – Precise output formatting • Conversion specifications: flags, field widths, precisions, etc. – Can perform • rounding, • aligning columns, • right/left justification, • inserting literal characters, • exponential format, • hexadecimal format, • fixed width and precision
Formatting Output with printf • Format of printf statement printf( format-control-string, otherarguments ); • Format control string: – describes output format – Ordinary characters - copy to output stream: – Example – printf(“this is an outputn”);
Formatting Output with printf • Format of printf statement printf( format-control-string, otherarguments ); • Other-arguments: – correspond to each conversion specification in format-control-string – Each specification begins with a percent sign (%), ends with conversion specifier
Formatting Output with printf • Format of printf statement
Formatting Output with printf • Format of printf statement – printf( format-control-string, otherarguments ); • Other-arguments: – Conversion specifications: leading with character ‘%’ – Format: %-w. p[d, f, c, s, …] • [-]: optional left justification, if exists • [w]: optional minimal width (wider if necessary). • [. ]: optional separates field w and p • [p]: optional maximum field width for a string precision of floating number • [d, f, c, s, …] : match to variable data type
Printing variable values • To print an integer: int degrees. F = 68; printf("The temperature is %d degrees. ", degrees. F); Specifier for “print an integer value” “Read value from this variable” Output: > The temperature is 68 degrees.
printf() • Format specifiers: – %c for single characters – %d for integers – %f for float/double (fractions): 1234. 56 – %g for float/double (scientific): 1. 23456 E+3 – %s for phrases or ‘strings’ (coming soon!)
Integer Conversion Specifier
Printing Character char a; printf("%c %d %x %0", a, a); A 65 41 101
Printing Integers • Integer – Whole number (no decimal point): 25, 0, -9 – Positive, negative, or zero – Only minus sign prints by default
Example 1 Output: 455 455 -455 3200000 707 455 4294966841 1 c 7 1 C 7
Printing Floating-Point Numbers • Floating Point Numbers – Have a decimal point (33. 5) – Exponential notation (computer's version of scientific notation) • 150. 3 is 1. 503 x 10² in scientific • 150. 3 is 1. 503 E+02 in exponential (E stands for exponent) • use e or E – f – print floating point with at least one digit to left of decimal – g (or G) - prints in f or e with no trailing zeros (1. 2300 becomes 1. 23)
Floating-point conversion specifiers
Example Program Output 1. 234568 e+006 -1. 234568 e+006 1. 234568 E+006 1234567. 890000 1. 23457 e+006 1. 23457 E+006
Printing Strings and Characters • c – Prints char argument – Cannot be used to print the first character of a string • s – Requires a pointer to char as an argument – Prints characters until NULL ('