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
Code Meaning Code Meaning
%c Single Char %i Decimal, hex or octal int
%d Decimal Integer %o Octal integer
%e Floating point %s String
%f Floating point %u Unsigned int
%g Floating point %x Hexadecimal int
%h Short 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

Format Output
printf(“%d”,9876); 9876
printf(“%6d”,9876); _ _9876
printf(“%2d”,9876); 9876
printf(“%-6d”,9876); 9876_ _
printf(“%06d”,9876); 009876

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

Format Output
printf(“%7.4f”,y) 98.7654
printf(“%7.2f”,y) _ _ 98.77
printf(“%-7.2f”,y) 98.77_ _
printf(“%f”,y) 98.7654
printf(“10.2e”,y) _ __ 9.88e+01
printf(“%11.4e”,-y) -9.8765e+01
printf(“%-10.2e”,y) 9.88e+01_ _
printf(“%e”,y) 9.876540e+01

Printing Characters

  • %w.ps 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

Specification Output
%s New Delhi 110001
%20s _ _ _ _ New Delhi 110001
%20.10s _ _ _ _ _ _ _ _ _ _ _ _ New Delhi
%.5s New D
%-20.10s New Delhi _ _ _ _ _ _ _ _ _ _ _ _ _ _
%5s New Delhi 110001

Output Format Flags

Flag Meaning
Output left justified within the field.
+ Signed numeric item will be preceded by +/-
0 Causes 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

 

Latest posts by admin (see all)

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...

Close