The greatest mistake you can make in life is to be continually fearing you will make one

Friday, 13 August 2010

Data Input and Output-I

  • C has 6 popular data input and output library functions. They are getchar, putchar, printf, scanf, gets and puts.
  • These functions handle data transfer between computer and the input/output device (keyboard,monitor)
  • The functions getchar and putchar allow single characters to be transferred into and out of the computer.
  • The functions printf and scanf allow the transfer of single characters, numerical values and strings
  • The functions gets and puts handle input and output of strings.
  • To use these functions in a program the standard input output header file stdio.h must be included in the program.
1.Single Character Input-getchar() function:
  •  The getchar() function reads a single character from the input device(keyboard)
  • Syntax:

      char variable=getchar();
  • Example:

        char c;

    The character entered on the keyboard is read by the getchar function and assigned to the character variable c.
  • If an end of file condition is found when reading a character using getchar, the value of symbolic constant(-1) will be returned.
2.Single Character Output-putchar() function:
  • The putchar() function display a single c character on the standard output device.
  • Syntax:

  • Example:

        char c;

    The character stored in the variable c is displayed in the monitor.
Scanf function
  • scanf function used to read any numerical values, single characters or strings.
  • The function returns the number of data items that have been entered successfully.
  • Syntax:

        scanf("control string",argument list);
           control string=%conversion character.
  • Table shows the conversion character and the data item meaning

    Conversion characterMeaning
    cSingle character
    ddecimal integer
    e,f,gFloating point value
    hShort integer
    idecimal, hexa decimal or octal integer
    ooctal integer
    uunsigned decimal integer
    xHexadecimal integer
    [...]String include whitespace characters
  • The arguments may be variables or array.
  • Each variable name must be preceded by an ampersand(&). Array name should not begin with &.
  • Example

    int main()
        int no;
        float f;
        char text[50];
        scanf("%d %f %s",&no,&f,text);
  • If 2 or more data items are entered they must be separeted by whitespace characters[blank space (or) new line (or) tab].
  • While reading a string that include whitespace characters use []. specify the whitespace characters inside the []
  • Example: Read a string which contains only capital letters and blank space

    int main()
        char text[50];
        scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]",text);}
    here if the input is given as WELCOME TO C then text contain WELCOME TO C. If the input is given as Welcome To C then only the single letter W would be assigned to text
  • If the characters with in the brackets are given after circumflex(^) operator then the scanf will read all the characters except the characters in the []

    The program will read any characters(maximum 49 characters) until the newline is pressed.
Little More about scanf function:
  • It is possible to limit the number of characters read by specifying a maximum field width for that data item. To do that an unsigned integer integer indicating the field width is placed with in the control string between the percent sign(%) and the conversion character.
  • Syntax:
    %fieldwidth conversioncharacter
  • Example:
        %4d -->The decimal number can contain maximum of 4 digits
       %5f -->The float number can contain 5 digits including the dot(20.20,200.1)
  • The data item may contain fewer characters than the specified field width but cannot exceed the specified field width. If we give more characters than the specified field width the leftover characters are incorrectly interpreted as a next data item
  • Example1:

        int a,b,c;

        scanf("%2d %2d %2d",a,b,c);

    1. If the input is given as 12 34 56 then a=12 b=34 c=56.
    2. If the input is given as 123456789 then a=12 b=34 c=56
    3. If the input is 123 4 5678  then a=12 b=3 c=4
  • Example 2:
        float f;

    1. if the input is 555.55 then f=555.500000
    2. If the input is 456789.555 then f=45678.000000
    3. if the input is 55555.555 then f=55555.00000
  • If there is no whitespace present between the conversion characters in the scanf function then the whitespace characters with in the input data will be interpreted as a data item.
  • Example:
        char a,b,c;
    If x y z is given as input then a=x b= (blankspace) c=y
  • To avoid reading the blankspace use %1s instead of %c
        char a,b,c;
    If x y z is given as input then a=x b=y c=z or we can write the scanf as scanf("%c %c %c",&a,&b,&c);
  • Unrecognized characters within the control string are expected to be matched by the same characters in the input data. The unrecognized characters are read but not assigned to any variable but if the characters are not present then the scanf function will terminate.
  • Example:
        char a,b;
        scanf("%c * %c",&a,&b);
    If the input is given as a *  b then only a=a b=b. if the input is given as a b then the function is stop executing as * is not given as input. so a is assigned to a . b is Null

1 comment:

  1. int recursive(int)
    { if (n==1)return(1);
    else return(recursive(n-1)+recursive(n-1));

    //how to find complexity