input-output

Managing input and output Operations

  • Three essential function of computer program are takes the inputs, process the data and produce the output
  • The input operation involves the transfer of data to the from the input device (e. key board) to computer memory & out put operation transfer the data from the computer memory to the output device (Eg. Display unit)
  • C language doesn’t provide any support for input output operation, it is performed through set of input output functions.
  • This set of standard library functions that perform input output operation is known as standard I/O library.
  • The library functions pre-defined functions are defined in a file known as header file.(eg. stdio.h, math.h, string.h etc.)
  • Each program that uses standard input output function must contain a statement #include<stdio.h>
  • The instruction #include<stdio.h> is a pre-processor directive command.
  • #include is a pre-processor directive, It tells the C compiler to include the contents of a file(here stdio.h) into the program at that place where it is written.
  • The include statement basically inserts all function prototypes BEFORE the actual compilation. Hence the name preprocessor.
  • stdio.h is a abbreviation for standard input output header file.
  • This file contains prototypes and definition of functions such as printf(), scanf(), … so that compiler knows what are their parameters and return values.
  • The <> characters around the name tell C to look in the system area for the file stdio.h. If I had given the name “stdio.h” instead it would tell the compiler to look in the current directory.
  • You can create your user defined header file(myheader.h) which can be refered in the program.

Format specifier/ Conversion specifies:

  • The library function scanf() & printf() use the conversion specifier to specify the type and size of data.
  • These are known as format specifiers and each conversion specifiers must begin with percentage sign %.
  • There are many format specifiers defined in C. Take a look at the following list:
  • Control String Data Types
    %c char
    %d or %i int
    %u unsigned int
    %hd or %hi short int
    %ld long int
    %lu unsigned long
    %f float
    %e float(With exponent for)
    %lf long float/double
    %Lf long double

Formated Input/Output Functions:

  • Formatted input refers to an input data that has been arranged in a particular format.

1. scanf() Function:

  • scanf()  pre-defined library function is used to read character, string, numeric data from the standard input device i.e keyboard.
  • The scanf() function is written as in following format:

   scanf(“Control string”, &arg1,&arg2…..&argn);

  • The control string specifies the field format in which the data is to be inputed.
  • The arguments arg1, arg2, …argn are the variables whose value are to be read from the keyboard.
  • The symbol ‘&’ represents the memory address where the variable value is to be stored.
  • The format specifier consisting of the conversion character %, a data type character(or type specifier) and an optional number specifying the field width.

Example:

  •  Here control string consist of one conversion specifier character %d, which signifies that only one integer value should be entered as input.
  • The enter value will be stored in a variable num

Input of integer Number:

  • The field specification for reading integer is %wd
  • w is an integer number that specifies the field width of the number to be read
  • After each space next characters will be taken as next input

Example: scanf(“%2d %3d”, &num1, &num2);

Input Value in memory
23 123 num1=23 num2=123
 123 23 num1=12 num2=3
 12345 123  num1=12 num2=345(unread part of 12345)
 123456 num1=12 num2=345(unread part of 123456 with %3d)
  •  If the width will not be specified then it can take the input up to the maximum value of that data type.
  • During the read of character from the keyboard we should keep in mind that the each key stroke is one one character (including enter and blank space)

Input of Real Number:

  • For giving the input for real number field width ‘w’ in not specified, so the corresponding format specifiers are used for real numbers

Example: scanf(“%f %f %f”, &r1,&r2,&r3);

For the input data 123.34     12.21E-1       234
The assigned value to the variables r1, r2, r3 are 123.339996       1.221000        234.000000

 2.printf() Function:

  • It is a pre-defined or library function used for printing the messages or value of a variable on the standard output device i.e monitor.
  • The printf() function is written as in following format:printf(“Control string”, arg1,arg2…..argn);
  • The control string or format string consist of three things
    1. Characters that will be printed on the output screen
    2. Format specifies that that defines the output format of data to be printed
    3. Escape sequence characters such as \n, \t,\b etc.
  • In control string it is specified how many values are going to be printed and their types.
  • The arguments arg1,arg2.arg3…..argn are the variables whose values are formatted and printed according to the specification in the control string.

Output of integer Numbers:

  • The format specification for integer is %wd
  • w specifies the minimum field width for the output and it is optional parameter
  • If the width of the number is greater than the specified length, it will be printed as it is overriding the minimum specification

Demo program for printf:

Output
Formatted Number is=123
Formatted Number is=  123
Formatted Number is=123
Formatted Number is=123
Formatted Number is=123
Formatted Number is=000123
Formatted Number is=  +123

Output of Real Numbers:

  • The output of real number may be displayed using the notation %w.pf
  • w indicates the minimum number of positions that are  used to display the value
  • p is an integer indicate the number of digits to be displayed after the decimal point(precision)
  • The default precision is 6 decimal places
  • The real number in exponential notation can be displayed using the format specifier as %w.pe

 

 

Output
Formatted Number is=0.000000
Formatted Number is=123.45000
Formatted Number is=123.45
Formatted Number is=123.450000
Formatted Number is=123.450
Formatted Number is=123.5
Formatted Number is=123.456789
Formatted Number is=123.4567890

Non-formated Input/Output Functions:

  • Unformatted I/O functions works only with character datatype (char)
  • The unformatted Input functions used in C are getchar(), gets().
  • The unformatted Output functions used in C are putchar(), puts().

getchar() function: It is a non formatted predefined function used to read a single character from the standard input device(keyboard)

Syntax:  variable_name = getchar();

Example:

char ch;

ch = getchar();

putchar() function: It is a non formatted predefined function used to display(write) a single character on the standard output device(Monitor).

Syntax: putchar(variable_name);

Example:

char ch= ‘A';

putchar(ch);