Managing Input and Output operations in c

–  IO is not part of C syntax
–  Several functions becomes standard for IO are called Standard IO Library in C
–  The printf and scanf are library functions
–  The stdio.h file and its inclusion in the program using include directive
–  Other headers such as math.h etc

Reading and Writing Single Char

  • The ch = getchar() function and problems associated with input stream
  • The isalpha(), isdigit(), isalnum(), islower(), isupper(), isprint(), isspace(), ispunct(), etc. functions and ctype.h file
  • The putchar(<char>) function
  • putchar(‘\n’) jumps to next line

Formatted Input

  • Scanf(<control string>,arg1,arg2,agr3…)
  • Control string specifies the field format in which the data is to be entered
  • arg1, arg2 etc specifies the locations or addresses where the data is to be stored
  • Control string contains field specifications which directs the interpretation of input
  • Control String may include
    • Field or format specs consisting of conversion char (%), a data type specifier,  and optional width field
    • Blanks, tabs and newlines
  • %wd for integers, %f for float, %lf for double,
  • * as width for skipping a field
  • %ws or %wc for char strings
  • Scanf returns no of items read
%cSingle Char%iDecimal, hex or octal int
%dDecimal Integer%oOctal integer
%eFloating point%sString
%fFloating point%uUnsigned int
%gFloating point%xHexadecimal int
%hShort int%[/^..]String of words

Points for Scanf

  • All args except CS, must be pointer to vars
  • Format specs should match the args in order
  • Input data items to be separated by spaces and match the variables receiving in order
  • Invalid mismatch will terminate scanf
  • Scanf ignores line boundaries
  • Unread data items are part of next read
  • W should be large enough

Formatted Output

  • printf(“control” string, arg1,arg2…)
  • Control string may contain following
    • Chars to be printed on the screen as it is
    • Format specs for each item to be displayed
    • Escape sequences chars such as \n, \t etc
  • The args should match the specs in number order and type
  • Form of format spec is %w.p type-specs

Format Spec for Integers

printf(“%6d”,9876);_ _9876
printf(“%-6d”,9876);9876_ _

Real Number Output

  • %w.p f  or %w.p e is the format spec
  • w is minimum number of position used to display the value
  • p is number of digits displayed after decimal point
  • If E is used Output will contain E
  • If g or G, it picks up shorter f or e(E)
  • The value, when printed, is rounded to p decimal places and printed right justified in w columns
  • Leading blanks and trailing zeroes will appear as necessary
  • The default precision is 6 decimal places
  •  w >= p +7 for e or E
  • printf (“%.*f”, width, precision, number)

Format Specs for Real Numbers

printf(“%7.2f”,y)_ _ 98.77
printf(“%-7.2f”,y)98.77_ _
printf(“10.2e”,y)_ __ 9.88e+01
printf(“%-10.2e”,y)9.88e+01_ _

Printing Characters

  • for strings where w is the field width of display and p is no of chars to be printed
  • It is printed right justified !
  • %wc can print right justified in width of w columns
  • Default value of w is 1, if precedes with – sign, it prints the data right justified

Printing of Chars and Strings

%sNew Delhi 110001
%20s_ _ _ _ New Delhi 110001
%20.10s_ _ _ _ _ _ _ _ _ _ _ _ New Delhi
%.5sNew D
%-20.10sNew Delhi _ _ _ _ _ _ _ _ _ _ _ _ _ _
%5sNew Delhi 110001

Output Format Flags

Output left justified within the field.
+Signed numeric item will be preceded by +/-
0Causes leading zeros to appear
#(with o or x)Octal and hex items to be preceded with 0 or 0x
# (with e f or g)Causes a decimal point to be present in all floating point numbers. In g prevents truncation of trailing zeros


More in cse
compilation and linking process in c

compilation and linking process in c Include file Header file Your source code can use some functions and variables for...